Trying to plot previous day high/low as buffer

 

Hello, just switched to MQL5, can someone help me understand why this behaviour happens?

image


As per title, I'm trying to plot previous day high/low. Here's the code:

//+------------------------------------------------------------------+
//|                                              Previous day HL.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
#property indicator_type1   DRAW_LINE
#property indicator_type2   DRAW_LINE
#property indicator_color1  Gold
#property indicator_color2  Gold

// Declare the buffers
double prev_high[];
double prev_low[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

   SetIndexBuffer(0, prev_high,INDICATOR_DATA);
  
   SetIndexBuffer(1, prev_low,INDICATOR_DATA);  
//---
   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 lookback = 1000;

   for(int iBar = Bars(Symbol(),PERIOD_CURRENT)-1-MathMax(lookback, prev_calculated); iBar >= 0; --iBar)
     {

      int shift = iBarShift(Symbol(),PERIOD_D1,time[iBar],false) + 1 ;

      prev_high[iBar] = iHigh(Symbol(),PERIOD_D1,shift);
      prev_low[iBar] = iLow(Symbol(),PERIOD_D1,shift);
  
     }


//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Thank's. 

 
"ArraySetAsSeries" must be set to true.
 
Nagisa Unada #:
"ArraySetAsSeries" must be set to true.

Where? 

 

ArraySetAsSeries(prev_high, true);

ArraySetAsSeries(prev_low, true);

ArraySetAsSeries(time, true);

 
Nagisa Unada #:

ArraySetAsSeries(prev_high, true);

ArraySetAsSeries(prev_low, true);

ArraySetAsSeries(time, true);

Thank's. Does this has to go inside the OnCalculate or on OnInit? 

Also, could you explain why this step is necessary? On mt4 it works perfectly without having to do this.

Thank's so much :) 

 

The first two are in Oninit, the rest in OnCalculate.

The data array in MT5 is the reverse of that in MT4. That is, array number 0 is the oldest data.
 
Nagisa Unada #:

The first two are in Oninit, the rest in OnCalculate.

The data array in MT5 is the reverse of that in MT4. That is, array number 0 is the oldest data.

I get it, thank you. 

But having ArraySetAsSeries(time, true); inside OnCalculate would make it run every new data, isn't it redundant?

Thank's. 

 

Also, why when I first load mt5 (with indicator already on chart) I have this?

I have to manually change timeframe in order to fix it. 

//+------------------------------------------------------------------+
//|                                              Previous day HL.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
#property indicator_type1   DRAW_LINE
#property indicator_type2   DRAW_LINE

// Declare the buffers
double prev_high[];
double prev_low[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

   SetIndexBuffer(0, prev_high,INDICATOR_DATA);

   SetIndexBuffer(1, prev_low,INDICATOR_DATA);

   PlotIndexSetInteger(0,PLOT_LINE_COLOR,clrMagenta);
   PlotIndexSetInteger(1,PLOT_LINE_COLOR,clrMagenta);

   ArraySetAsSeries(prev_high, true);
   ArraySetAsSeries(prev_low, 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 lookback = 1000;

   for(int iBar = Bars(Symbol(),PERIOD_CURRENT)-MathMax(lookback, prev_calculated); iBar >= 0; --iBar)
     {
      ArraySetAsSeries(time, true);

      int shift = iBarShift(Symbol(),PERIOD_D1,time[iBar],false) +1 ;

         prev_high[iBar] = iHigh(Symbol(),PERIOD_D1,shift);
         prev_low[iBar] = iLow(Symbol(),PERIOD_D1,shift);

     }


//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Scopri le nuove opportunità di MetaTrader 5 con la community e i servizi MQL5
Scopri le nuove opportunità di MetaTrader 5 con la community e i servizi MQL5
  • 2023.09.03
  • www.mql5.com
MQL5: il linguaggio delle strategie di trading integrato nella piattaforma di trading MetaTrader 5 permette di creare i propri robot di trading, indicatori tecnici, script e librerie di funzioni
 
int lookback = 1000;
ArraySetAsSeries(time, true);

   for(int iBar = Bars(Symbol(),PERIOD_CURRENT)-MathMax(lookback, prev_calculated); iBar >= 0; --iBar)
     {
      //ArraySetAsSeries(time, true);

Nothing is going wrong. It is working properly.
 
Nagisa Unada #:
Nothing is going wrong. It is working properly.
//+------------------------------------------------------------------+
//|                                              Previous day HL.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
#property indicator_type1   DRAW_LINE
#property indicator_type2   DRAW_LINE

input ENUM_TIMEFRAMES tf_ = PERIOD_D1;

// Declare the buffers
double prev_high[];
double prev_low[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

   SetIndexBuffer(0, prev_high,INDICATOR_DATA);

   SetIndexBuffer(1, prev_low,INDICATOR_DATA);

   PlotIndexSetInteger(0,PLOT_LINE_COLOR,clrMagenta);
   PlotIndexSetInteger(1,PLOT_LINE_COLOR,clrMagenta);

   ArraySetAsSeries(prev_high, true);
   ArraySetAsSeries(prev_low, 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 lookback = 1000;
   ArraySetAsSeries(time, true);
   for(int iBar = Bars(Symbol(),PERIOD_CURRENT)-MathMax(lookback, prev_calculated); iBar >= 0; --iBar)
     {
      int shift = iBarShift(Symbol(),tf_,time[iBar],false) +1 ;

      prev_high[iBar] = iHigh(Symbol(),tf_,shift);
      prev_low[iBar] = iLow(Symbol(),tf_,shift);

     }


//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Please try it, load on a 1minute chart, it will work properly. Then close mt5 and reopen it, you'll get the same thing I showed on my last screenshot.

Let me know, thank's. 

 

I see, it certainly will.

However, I do not know about this.
Reason: