请写出MT4的分型指标Fractals 源代码
时间: 2023-09-04 10:07:19 浏览: 681
以下是MT4 Fractals指标的源代码:
```
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Lime
extern int BarsToCount = 2; // Fractal calculation period
double ExtMapBuffer1[];
double ExtMapBuffer2[];
void CalculateFractals(int shift)
{
if (shift < BarsToCount) return;
double hi = High[shift];
double lo = Low[shift];
bool fractal_high = true;
bool fractal_low = true;
for (int i = 1; i <= BarsToCount; i++)
{
if (High[shift - i] > hi)
{
fractal_high = false;
}
if (Low[shift - i] < lo)
{
fractal_low = false;
}
}
if (fractal_high)
{
ExtMapBuffer1[shift - BarsToCount] = hi;
}
else
{
ExtMapBuffer1[shift - BarsToCount] = 0;
}
if (fractal_low)
{
ExtMapBuffer2[shift - BarsToCount] = lo;
}
else
{
ExtMapBuffer2[shift - BarsToCount] = 0;
}
}
int init()
{
SetIndexBuffer(0, ExtMapBuffer1, INDICATOR_DATA);
SetIndexBuffer(1, ExtMapBuffer2, INDICATOR_DATA);
return(0);
}
int start()
{
int limit = Bars - BarsToCount - 1;
for(int i = limit; i >= 0; i--)
{
CalculateFractals(i);
}
return(0);
}
```
这个指标使用`BarsToCount`参数来计算最近几个价格周期内的分型。如果最高价在指定周期内是极大值,那么就标记为顶部分型;如果最低价在指定周期内是极小值,那么就标记为底部分型。这些标记存储在两个指标缓冲区`ExtMapBuffer1`和`ExtMapBuffer2`中,分别用红色和绿色表示。
阅读全文