mql5
//+------------------------------------------------------------------+
//| FVG_MT5.mq5 |
//| Copyright 2024, YourNameHere |
//| https://www.yourwebsite.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024"
#property link "https://www.yourwebsite.com"
#property version "1.00"
#property description "Fair Value Gap (FVG) Indicator for Gold Trading"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots 4 // 改为4个绘图,2个用于边框,2个用于填充
//--- 输入参数
input int LookBackBars = 100; // 回看柱数
input int FVGSizePoints = 30; // FVG最小点数
input bool ShowBullishFVG = true; // 显示看涨FVG
input bool ShowBearishFVG = true; // 显示看跌FVG
input color BullishColor = clrLime; // 看涨FVG颜色
input color BearishColor = clrRed; // 看跌FVG颜色
input int FVGWidth = 3; // FVG线宽
input bool FillFVG = true; // 填充FVG区域
input int FillOpacity = 50; // 填充透明度(0-100)
//--- 指标缓冲区
double bullishHighBuffer[];
double bullishLowBuffer[];
double bearishHighBuffer[];
double bearishLowBuffer[];
double bullishFillHighBuffer[];
double bullishFillLowBuffer[];
double bearishFillHighBuffer[];
double bearishFillLowBuffer[];
//--- 颜色变量
color bullishFillColor;
color bearishFillColor;
//+------------------------------------------------------------------+
//| 自定义函数:计算透明度颜色 |
//+------------------------------------------------------------------+
color SetColorOpacity(color clr, int opacity)
{
// 将透明度(0-100)转换为ARGB值(0-255)
int alpha = (int)((100 - MathMin(MathMax(opacity, 0), 100)) * 2.55);
return ColorToARGB(clr, alpha);
}
//+------------------------------------------------------------------+
//| 自定义函数:计算黄金点数值 |
//+------------------------------------------------------------------+
double GoldPointValue()
{
return 0.01; // 黄金通常使用0.01作为最小变动单位
}
//+------------------------------------------------------------------+
//| 自定义函数:将点数转换为价格值 |
//+------------------------------------------------------------------+
double PointsToPrice(int points)
{
return points * GoldPointValue();
}
//+------------------------------------------------------------------+
//| 初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
// 设置边框缓冲区
SetIndexBuffer(0, bullishHighBuffer, INDICATOR_DATA);
SetIndexBuffer(1, bullishLowBuffer, INDICATOR_DATA);
SetIndexBuffer(2, bearishHighBuffer, INDICATOR_DATA);
SetIndexBuffer(3, bearishLowBuffer, INDICATOR_DATA);
// 设置填充缓冲区
SetIndexBuffer(4, bullishFillHighBuffer, INDICATOR_DATA);
SetIndexBuffer(5, bullishFillLowBuffer, INDICATOR_DATA);
SetIndexBuffer(6, bearishFillHighBuffer, INDICATOR_DATA);
SetIndexBuffer(7, bearishFillLowBuffer, INDICATOR_DATA);
// 计算填充颜色
bullishFillColor = SetColorOpacity(BullishColor, FillOpacity);
bearishFillColor = SetColorOpacity(BearishColor, FillOpacity);
// 设置看涨FVG边框属性
PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_HISTOGRAM);
PlotIndexSetInteger(0, PLOT_LINE_COLOR, BullishColor);
PlotIndexSetInteger(0, PLOT_LINE_WIDTH, FVGWidth);
PlotIndexSetString(0, PLOT_LABEL, "Bullish FVG Border");
PlotIndexSetInteger(0, PLOT_SHOW_DATA, true);
// 设置看涨FVG填充属性
PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_HISTOGRAM);
PlotIndexSetInteger(1, PLOT_LINE_COLOR, bullishFillColor);
PlotIndexSetInteger(1, PLOT_LINE_WIDTH, 1);
PlotIndexSetInteger(1, PLOT_SHIFT, 0);
PlotIndexSetString(1, PLOT_LABEL, "Bullish FVG Fill");
PlotIndexSetInteger(1, PLOT_SHOW_DATA, true);
// 设置看跌FVG边框属性
PlotIndexSetInteger(2, PLOT_DRAW_TYPE, DRAW_HISTOGRAM);
PlotIndexSetInteger(2, PLOT_LINE_COLOR, BearishColor);
PlotIndexSetInteger(2, PLOT_LINE_WIDTH, FVGWidth);
PlotIndexSetString(2, PLOT_LABEL, "Bearish FVG Border");
PlotIndexSetInteger(2, PLOT_SHOW_DATA, true);
// 设置看跌FVG填充属性
PlotIndexSetInteger(3, PLOT_DRAW_TYPE, DRAW_HISTOGRAM);
PlotIndexSetInteger(3, PLOT_LINE_COLOR, bearishFillColor);
PlotIndexSetInteger(3, PLOT_LINE_WIDTH, 1);
PlotIndexSetInteger(3, PLOT_SHIFT, 0);
PlotIndexSetString(3, PLOT_LABEL, "Bearish FVG Fill");
PlotIndexSetInteger(3, PLOT_SHOW_DATA, true);
// 设置绘图显示属性
for(int i = 0; i < 4; i++)
{
PlotIndexSetDouble(i, PLOT_EMPTY_VALUE, EMPTY_VALUE);
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 计算函数 |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
if(rates_total < 3) return(0);
// 计算起始位置
int start;
if(prev_calculated == 0)
{
start = LookBackBars;
// 清空所有缓冲区
ArrayInitialize(bullishHighBuffer, EMPTY_VALUE);
ArrayInitialize(bullishLowBuffer, EMPTY_VALUE);
ArrayInitialize(bearishHighBuffer, EMPTY_VALUE);
ArrayInitialize(bearishLowBuffer, EMPTY_VALUE);
ArrayInitialize(bullishFillHighBuffer, EMPTY_VALUE);
ArrayInitialize(bullishFillLowBuffer, EMPTY_VALUE);
ArrayInitialize(bearishFillHighBuffer, EMPTY_VALUE);
ArrayInitialize(bearishFillLowBuffer, EMPTY_VALUE);
}
else
{
start = prev_calculated - 1;
}
// 限制计算范围
start = MathMax(start, 2);
// 计算FVG
for(int i = start; i < rates_total && !IsStopped(); i++)
{
// 重置所有缓冲区
bullishHighBuffer[i] = EMPTY_VALUE;
bullishLowBuffer[i] = EMPTY_VALUE;
bearishHighBuffer[i] = EMPTY_VALUE;
bearishLowBuffer[i] = EMPTY_VALUE;
bullishFillHighBuffer[i] = EMPTY_VALUE;
bullishFillLowBuffer[i] = EMPTY_VALUE;
bearishFillHighBuffer[i] = EMPTY_VALUE;
bearishFillLowBuffer[i] = EMPTY_VALUE;
// 检测看涨FVG
if(ShowBullishFVG && i >= 1 && high[i] < low[i-1])
{
double gapSize = low[i-1] - high[i];
if(gapSize >= PointsToPrice(FVGSizePoints))
{
bullishHighBuffer[i] = low[i-1];
bullishLowBuffer[i] = high[i];
if(FillFVG)
{
bullishFillHighBuffer[i] = low[i-1];
bullishFillLowBuffer[i] = high[i];
}
}
}
// 检测看跌FVG
if(ShowBearishFVG && i >= 1 && low[i] > high[i-1])
{
double gapSize = low[i] - high[i-1];
if(gapSize >= PointsToPrice(FVGSizePoints))
{
bearishHighBuffer[i] = low[i];
bearishLowBuffer[i] = high[i-1];
if(FillFVG)
{
bearishFillHighBuffer[i] = low[i];
bearishFillLowBuffer[i] = high[i-1];
}
}
}
// 检测三根K线的FVG模式
if(i >= 2)
{
// 看涨三K模式
if(ShowBullishFVG &&
close[i-2] < open[i-2] && // 第一根阴线
low[i-1] > high[i-2] && // 形成向上缺口
close[i] > open[i]) // 第三根阳线
{
double gapSize = low[i-1] - high[i-2];
if(gapSize >= PointsToPrice(FVGSizePoints))
{
bullishHighBuffer[i-1] = low[i-1];
bullishLowBuffer[i-1] = high[i-2];
if(FillFVG)
{
bullishFillHighBuffer[i-1] = low[i-1];
bullishFillLowBuffer[i-1] = high[i-2];
}
}
}
// 看跌三K模式
if(ShowBearishFVG &&
close[i-2] > open[i-2] && // 第一根阳线
high[i-1] < low[i-2] && // 形成向下缺口
close[i] < open[i]) // 第三根阴线
{
double gapSize = low[i-2] - high[i-1];
if(gapSize >= PointsToPrice(FVGSizePoints))
{
bearishHighBuffer[i-1] = low[i-2];
bearishLowBuffer[i-1] = high[i-1];
if(FillFVG)
{
bearishFillHighBuffer[i-1] = low[i-2];
bearishFillLowBuffer[i-1] = high[i-1];
}
}
}
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
//| 去初始化函数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// 清理工作
Comment("");
}
|