请帮我写一段MT4的BOLL缩口后又开口的报警指标代码,请每段代码后面标注注释
时间: 2024-05-09 20:14:19 浏览: 104
以下是一段MT4的BOLL缩口后又开口的报警指标代码:
```
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 LimeGreen
#property indicator_color2 Red
extern int boll_period = 20;
extern double boll_deviation = 2.0;
extern int ma_method = MODE_SMA;
extern int shift = 0;
double ma_buffer[];
double std_dev_buffer[];
double upper_band[];
double lower_band[];
int init()
{
SetIndexBuffer(0, upper_band);
SetIndexBuffer(1, lower_band);
SetIndexStyle(0, DRAW_LINE);
SetIndexStyle(1, DRAW_LINE);
SetIndexLabel(0, "Upper Bollinger Band");
SetIndexLabel(1, "Lower Bollinger Band");
SetIndexDrawBegin(0, boll_period + shift);
SetIndexDrawBegin(1, boll_period + shift);
IndicatorDigits(Digits + 1);
return(0);
}
int start()
{
int limit = Bars - IndicatorCounted();
if (limit > 0)
{
ArrayResize(ma_buffer, limit);
ArrayResize(std_dev_buffer, limit);
for (int i = 0; i < limit; i++)
{
ma_buffer[i] = iMA(NULL, 0, boll_period, 0, ma_method, PRICE_CLOSE, i + shift);
std_dev_buffer[i] = boll_deviation * iStdDev(NULL, 0, boll_period, 0, ma_method, PRICE_CLOSE, i + shift);
upper_band[i] = ma_buffer[i] + std_dev_buffer[i];
lower_band[i] = ma_buffer[i] - std_dev_buffer[i];
if (i > 0)
{
// Bollinger band is narrowing down
if (std_dev_buffer[i] < std_dev_buffer[i - 1])
{
Alert("Bollinger band is narrowing down");
}
// Bollinger band is opening up
if (std_dev_buffer[i] > std_dev_buffer[i - 1])
{
Alert("Bollinger band is opening up");
}
}
}
}
return(0);
}
```
代码注释如下:
```
#property indicator_chart_window // 指标将显示在图表窗口中
#property indicator_buffers 2 // 定义2个指标缓冲区
#property indicator_color1 LimeGreen // 第1个指标缓冲区颜色为绿色
#property indicator_color2 Red // 第2个指标缓冲区颜色为红色
// 定义输入参数
extern int boll_period = 20; // Bollinger带周期
extern double boll_deviation = 2.0; // Bollinger带偏差
extern int ma_method = MODE_SMA; // 移动平均线计算方法
extern int shift = 0; // 移动平均线偏移量
// 定义指标缓冲区
double ma_buffer[];
double std_dev_buffer[];
double upper_band[];
double lower_band[];
// 初始化指标
int init()
{
SetIndexBuffer(0, upper_band);
SetIndexBuffer(1, lower_band);
SetIndexStyle(0, DRAW_LINE);
SetIndexStyle(1, DRAW_LINE);
SetIndexLabel(0, "Upper Bollinger Band");
SetIndexLabel(1, "Lower Bollinger Band");
SetIndexDrawBegin(0, boll_period + shift);
SetIndexDrawBegin(1, boll_period + shift);
IndicatorDigits(Digits + 1);
return(0);
}
// 计算指标
int start()
{
int limit = Bars - IndicatorCounted();
if (limit > 0)
{
ArrayResize(ma_buffer, limit);
ArrayResize(std_dev_buffer, limit);
for (int i = 0; i < limit; i++)
{
ma_buffer[i] = iMA(NULL, 0, boll_period, 0, ma_method, PRICE_CLOSE, i + shift);
std_dev_buffer[i] = boll_deviation * iStdDev(NULL, 0, boll_period, 0, ma_method, PRICE_CLOSE, i + shift);
upper_band[i] = ma_buffer[i] + std_dev_buffer[i];
lower_band[i] = ma_buffer[i] - std_dev_buffer[i];
if (i > 0)
{
// Bollinger带正在缩小
if (std_dev_buffer[i] < std_dev_buffer[i - 1])
{
Alert("Bollinger band is narrowing down");
}
// Bollinger带正在扩大
if (std_dev_buffer[i] > std_dev_buffer[i - 1])
{
Alert("Bollinger band is opening up");
}
}
}
}
return(0);
}
```
阅读全文