previous days high/low

 
//+------------------------------------------------------------------+
//|                                         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?

double pips;

//--- indicator buffers
double buff_prevdayhigh[];
double buff_prevdaylow[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   pips=Point; //.00001 or .0001. .001 .01.
   if(Digits==3 || Digits==5)
     {
      pips*=10;
     }
   else if(Digits==2)
     {
      pips=1;
     }

   SetIndexBuffer(0,buff_prevdayhigh);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexLabel(0,"previous days high");

   SetIndexBuffer(1,buff_prevdaylow);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexLabel(1,"previous days low");

//----
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//----
   Comment(" ");
  }
//+------------------------------------------------------------------+
//| 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

  {
   if(ShowPrevDayHL!=off && _Period<=PERIOD_H1)
      DrawPrevDayHighLow();

   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void DrawPrevDayHighLow()
  {
//-- finding the high and low of the previous day  
   double prevdayhigh=iHigh(NULL,PERIOD_D1,1);
   double prevdaylow=iLow(NULL,PERIOD_D1,1);

   int candleshift=0;
   if(ShowPrevDayHL==previousday)
      candleshift=1;
      
//--- finding the first candle for start drawing
   int drawfirstcandle=iBarShift(NULL,0,iTime(NULL,PERIOD_D1,candleshift));

//--- starts drawing the lines from the first candle of today or yesterday until the current candle
   for(int i=drawfirstcandle;i>=0;i--)
     {
      buff_prevdayhigh[i]=prevdayhigh;
      buff_prevdaylow[i]=prevdaylow;
     }
  }
//+------------------------------------------------------------------+

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

 
Dan:

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);
  }

//+------------------------------------------------------------------+
 
  1. 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.

  2.    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

  3. 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

  4. 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.)
 
Petr Nosek:

Perhaps I've understood you well. Try this code:

Thanks, I'll try this code and see the result tomorrow.

 
whroeder1:
  1. 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.

  2. 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

  3. Please don't post a link to or attach an image, just insert the image.
              Messages Editor

  4. 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 :-)