Вопрос про индикаторные буферы

 

Здравствуйте. При создании индикатора столкнулся с непонятной, для меня, проблемой. Для демонстрации возьмем следующий код:

//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- indicator buffers
double         Label1Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Label1Buffer);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,159);
   
   Label1Buffer[1] = Close[1];
//---
   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[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

 При прикреплении получаем выход за пределы массива.

 

Проверил размер массива, он нулевой. Переместил проблемную строку в OnCalculate и все заработало.

//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- indicator buffers
double         Label1Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Label1Buffer);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,159);
   
//---
   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[])
  {
//---
   
//--- return value of prev_calculated for next call
   Label1Buffer[1] = Close[1];
   
   return(rates_total);
  }
//+------------------------------------------------------------------+

 Почему так происходит? Мой индикатор (не тот, который в примере) производит вычисления один раз за несколько баров. Неужели я не могу просчитать значения для исторических данных в OnInit? Складывается впечатление, что SetIndexBuffer срабатывает после первого исполнения функции OnInit. В справочнике об этом ничего не сказано.

 
Maks_Knyazev:

Мой индикатор (не тот, который в примере) производит вычисления один раз за несколько баров. Неужели я не могу просчитать значения для исторических данных в OnInit? Складывается впечатление, что SetIndexBuffer срабатывает после первого исполнения функции OnInit. В справочнике об этом ничего не сказано.

Работать с индикаторными буферами действительно лучше в OnCalculate - именно здесь они выравниваются по тайм-серии. 

Кто мешает вам в функции OnCalculate проверить prev_calculated == 0 или вообще завести статическую переменную для оценки первого запуска:

static bool isStart=true;

if (isStart) {

 isStart=false;

 .... 

 
Не стоит работать с рыночными данными в ините.
Причина обращения: