Hello,
I wann use a little indicator for drawing the previous days high and low as lines. I can choose to draw the lines only for today or for yesterday also.
The attached screenshot shows the problem which occurs when a new day (candle) is coming. If the day changes, it draws lines for 3 days (as seen in my screenshot). What am I doing wrong, that it fills the arrays for 3 days instead of 2?
When I change the timeframe on the chart (new initialization) everything works fine. But I don't wanna initialize the indicator everyday.
Thanks a lot
Daniel
Perhaps I've understood you well. Try this code:
//+------------------------------------------------------------------+ //| PrevDaysHighLow_TEST.mq4 | //+------------------------------------------------------------------+ #property copyright "15.01.2018, Daniel" #property link "http://www.mql4.com" #property version "1.0" #property description "Draws the low/high of the previous day." #property indicator_buffers 8 //used for drawing #property indicator_chart_window #property indicator_color1 Red #property indicator_color2 Red #property indicator_width1 2 #property indicator_width2 2 #property strict //+------------------------------------------------------------------+ enum EnumDrawPrevDayHL { today,//show only today previousday,//show with pevious day off//none }; extern EnumDrawPrevDayHL ShowPrevDayHL=previousday;//Show high and low of the previous day? //--- indicator buffers double buff_prevdayhigh[]; double buff_prevdaylow[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { SetIndexBuffer(0,buff_prevdayhigh); SetIndexStyle(0,DRAW_LINE); SetIndexEmptyValue(0,0.0); SetIndexLabel(0,"previous days high"); SetIndexBuffer(1,buff_prevdaylow); SetIndexStyle(1,DRAW_LINE); SetIndexEmptyValue(1,0.0); SetIndexLabel(1,"previous days low"); //---- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate (const int rates_total, // size of input time series const int prev_calculated, // bars handled in previous call const datetime& time[], // Time const double& open[], // Open const double& high[], // High const double& low[], // Low const double& close[], // Close const long& tick_volume[], // Tick Volume const long& volume[], // Real Volume const int &spread[]) // Spread { static datetime timeD1=iTime(_Symbol,PERIOD_D1,0); // Input is set to off or timeframe is higher than H1 ==> do nothing if(ShowPrevDayHL==off || _Period>PERIOD_H1) return(rates_total); // Initialize arrays if(prev_calculated<1) { ArrayInitialize(buff_prevdayhigh,0.0); ArrayInitialize(buff_prevdaylow,0.0); } int startShift,endShift; // A new day if(timeD1<iTime(_Symbol,PERIOD_D1,0)) { // Delete history startShift=iBarShift(_Symbol,_Period,iTime(_Symbol,PERIOD_D1,ShowPrevDayHL==today?1:2)); endShift=iBarShift(_Symbol,_Period,iTime(_Symbol,PERIOD_D1,ShowPrevDayHL==today?0:1)); for(int i=startShift;i>endShift;i--) { buff_prevdayhigh[i]=0.0; buff_prevdaylow[i]=0.0; } // Set new time D1 timeD1=iTime(_Symbol,PERIOD_D1,0); } // Draw indicator startShift=prev_calculated<1?iBarShift(_Symbol,_Period,iTime(_Symbol,PERIOD_D1,ShowPrevDayHL==today?0:1)):(rates_total-prev_calculated-1); for(int i=startShift;i>-1;i--) { buff_prevdayhigh[i]=iHigh(_Symbol,PERIOD_D1,iBarShift(_Symbol,PERIOD_D1,time[i])+1); buff_prevdaylow[i]=iLow(_Symbol,PERIOD_D1,iBarShift(_Symbol,PERIOD_D1,time[i])+1); } return(rates_total); } //+------------------------------------------------------------------+
-
Why did you post your MT4 question in the
Root /
MT5 Indicators section
instead of the
MQL4 section, (bottom of the Root page?)
General rules and best pratices of the Forum. - General - MQL5 programming forum
Next time post in the correct place. The moderators will likely move this thread there soon. double prevdayhigh=iHigh(NULL,PERIOD_D1,1); double prevdaylow=iLow(NULL,PERIOD_D1,1);
On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors.
Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum- Dan: The attached screenshot shows the problem which occurs when a new day (candle) is coming. If the day changes, it draws lines for 3 days (as seen in my screenshot). What am I doing wrong, that it fills the arrays for 3 days instead of 2?Please don't post a link to or attach an image, just insert the image.
Messages Editor - It's not drawing 3 days. It drew 2 days, then a new day starts and it draws today. Result is three days. Old lines aren't going to magically disappear, (which is what your "change the timeframe" is doing.)
Perhaps I've understood you well. Try this code:
Thanks, I'll try this code and see the result tomorrow.
-
Why did you post your MT4 question in the
Root /
MT5 Indicators section
instead of the
MQL4 section, (bottom of the Root page?)
General rules and best pratices of the Forum. - General - MQL5 programming forum
Next time post in the correct place. The moderators will likely move this thread there soon. - On MT4: Unless the current chart is that specific pair/TF referenced, you must handle
4066/4073 errors.
Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum - Please don't post a link to or attach an image, just insert the image.
Messages Editor - It's not drawing 3 days. It drew 2 days, then a new day starts and it draws today. Result is three days. Old lines aren't going to magically disappear, (which is what your "change the timeframe" is doing.)
Thanks whroeder1 for advices. Next time I try to follow the instructions :-)

- 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,
I wann use a little indicator for drawing the previous days high and low as lines. I can choose to draw the lines only for today or for yesterday also.
The attached screenshot shows the problem which occurs when a new day (candle) is coming. If the day changes, it draws lines for 3 days (as seen in my screenshot). What am I doing wrong, that it fills the arrays for 3 days instead of 2?
When I change the timeframe on the chart (new initialization) everything works fine. But I don't wanna initialize the indicator everyday.
Thanks a lot
Daniel