求助,用MT5编程。分配4个数组,为什么只有3个数组有数据?

 

求助,想计算一下均线和布林的值。但发现分配了4个数组,只有3个数组有数据。求指点!!刚学MQL5

#property copyright "Copyright 2023, MetaQuotes Ltd."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_plots   4

//--- plot ma

#property indicator_label1  "ma"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrFuchsia

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot up

#property indicator_label2  "up"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrRed

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot low

#property indicator_label3  "low"

#property indicator_type3   DRAW_LINE

#property indicator_color3  clrRed

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot md

#property indicator_label4  "md"

#property indicator_type4   DRAW_LINE

#property indicator_color4  clrRed

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- input parameters

input int      ma=26;

input int      boll=20;

//--- indicator buffers

double         maBuffer[];

double         upBuffer[];

double         lowBuffer[];

double         mdBuffer[];

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,maBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,upBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,lowBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,mdBuffer,INDICATOR_DATA);

//变数组序列为时间序列

   ArraySetAsSeries(maBuffer,true);

   ArraySetAsSeries(upBuffer,true);

   ArraySetAsSeries(lowBuffer,true);

   ArraySetAsSeries(mdBuffer,true);


//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

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[])

  {

//计算均线值

   int ma_s = iMA(NULL,PERIOD_CURRENT,ma,0,MODE_SMA,PRICE_CLOSE);

   CopyBuffer(ma_s,0,0,rates_total,maBuffer);

//布林上中下三轨数值

   int buli = iBands(NULL,PERIOD_CURRENT,20,0,2,PRICE_CLOSE);

   CopyBuffer(buli,1,0,rates_total,upBuffer);

   CopyBuffer(buli,2,0,rates_total,lowBuffer);

   CopyBuffer(buli,3,0,rates_total,mdBuffer);

   printf("均线:%.6f,上轨:%.5f,下轨:%.5f,中轨:%.5f。",maBuffer[1],upBuffer[1],lowBuffer[1],mdBuffer[1]);

   return(rates_total);

  }

Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
  • 2023.03.18
  • www.mql5.com
MQL5: язык торговых стратегий для MetaTrader 5, позволяет писать собственные торговые роботы, технические индикаторы, скрипты и библиотеки функций
 
CopyBuffer(buli,3,0,rates_total,mdBuffer);
布林带有3个数据吗,那也应该从0开始,0.1.2
 
同意楼上的意见。是数据读取问题。

iBands 的缓冲区只有3个数,

可以参见 MQL5参考 / 技术指标 / iBands里面有Demo_iBands.mq5 ,可以借鉴。 

 iBands  缓冲区代码如下 : 0 - BASE_LINE, 1 - UPPER_BAND, 2 - LOWER_BAND。

所以 3 没有对应的数据,理论上你用3,取到的数应该是不确定的数据,如果系统分配内存的时候,所有内存都初始化成0的话,大概率是取到0,否则就是前面程序的遗留数据,那就是不确定的。
 平日没写过指标,所以是临时查阅的,我也对函数的具体参数和功能不够了解。建议你仔细研究测试一下参考文件里面的那个例子。
英语好的话,直接读英语的,汉语翻译的不准确,有时候理解起来反倒比英语还难。

MQL5文档: 技术指标 / iBands
MQL5文档: 技术指标 / iBands
  • www.mql5.com
iBands - 技术指标 - MQL5参考 - 参考MetaTrader 5的算法/自动交易语言
 

是的布林带buffer从0到2而已