ArraySetAsSeries(prev_high, true);
ArraySetAsSeries(prev_low, true);
ArraySetAsSeries(time, true);
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 first two are in Oninit, the rest in OnCalculate.
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); } //+------------------------------------------------------------------+
- 2023.09.03
- www.mql5.com
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.
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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello, just switched to MQL5, can someone help me understand why this behaviour happens?
As per title, I'm trying to plot previous day high/low. Here's the code:
Thank's.