Trying to plot previous day high/low as buffer - page 2

 
Nagisa Unada #:

I see, it certainly will.

However, I do not know about this.

Thank's for trying and giving your feedback.


Maybe someone else can jump in and offer any solution? Thank's 

 

Just to give an idea, you can use iTime to get the time for the previous day and avoid overcomplication of your loop


double prevDayClose(){

    datetime prevDay = iTime(_Symbol, PERIOD_D1, 1); // Get the time of the previous day
    int prevBar = iBarShift(_Symbol, PERIOD_D1, prevDay); // Get the bar index of the previous day

    double closePrice = iClose(_Symbol, PERIOD_D1, prevBar); // Get the close price of the previous day

    return closePrice;
}
make separate functions for iHigh and iLow as it suits
 
phade #:

Just to give an idea, you can use iTime to get the time for the previous day and avoid overcomplication of your loop


make separate functions for iHigh and iLow as it suits

Hello thank's but I do not understand how your suggestion should be useful for this problem 

 
ironhak #:

Hello thank's but I do not understand how your suggestion should be useful for this problem 


It will display the previous day high in a blue line buffer, and the previous day low in a red line buffer:


//+------------------------------------------------------------------+
//|                                          PreviousDayHighLow.mq5  |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
#property indicator_label1 "Previous Day High"
#property indicator_label2 "Previous Day Low"
#property indicator_color1 clrBlue
#property indicator_color2 clrRed
#property indicator_type1 DRAW_LINE
#property indicator_type2 DRAW_LINE
#property indicator_style1 STYLE_SOLID
#property indicator_style2 STYLE_SOLID
#property indicator_width1 2
#property indicator_width2 2


// Define buffers
double HighBuffer[];
double LowBuffer[];

// global vars
datetime timeToUse;
bool valuesCalculated = false;
ENUM_TIMEFRAMES prevTimeFrame = PERIOD_CURRENT;
  
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   // Set the indicator label
   IndicatorSetString(INDICATOR_SHORTNAME, "PrevDayHighLow");

   // Create buffers
   SetIndexBuffer(0, HighBuffer);
   SetIndexBuffer(1, LowBuffer);
   
   ResetBuffers();
   
   // Return initialization result
   return(INIT_SUCCEEDED);
}
  

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[]){
                
                
    // Check if the timeframe has changed
   if (ChartPeriod() != prevTimeFrame){
   
      prevTimeFrame = ChartPeriod();
      ResetBuffers(); // Reset indicator buffers when the timeframe changes
      valuesCalculated = false; // Reset the flag
   }
                
                
   timeToUse = iTime(_Symbol, PERIOD_D1, 1); // Get the time of the previous day
 
   // Calculate and store the previous day's high and low in the buffers
   if (!valuesCalculated){
   
      for (int i = 0; i < rates_total; i++){
      
         HighBuffer[i] = prevDayHigh(timeToUse);
         LowBuffer[i] = prevDayLow(timeToUse);
      }
      
      valuesCalculated = true; // Set the flag to true
   }
         
   return rates_total;
}
  
  
void ResetBuffers(){
   ArrayInitialize(HighBuffer, 0); // Reset the HighBuffer to 0
   ArrayInitialize(LowBuffer, 0);  // Reset the LowBuffer to 0
}


double prevDayHigh(const datetime prevDay){

    int prevBar = iBarShift(_Symbol, PERIOD_D1, prevDay); // Get the bar index of the previous day
    double highPrice = iHigh(_Symbol, PERIOD_D1, prevBar); // Get the high price of the previous day
    return highPrice;
}


double prevDayLow(const datetime prevDay){

    int prevBar = iBarShift(_Symbol, PERIOD_D1, prevDay); // Get the bar index of the previous day
    double lowPrice = iLow(_Symbol, PERIOD_D1, prevBar); // Get the low price of the previous day
    return lowPrice;
}


It isn't keeping a trend of historic highs and lows of previous days, but I thought your only use case was to focus on the previous day highs and lows (before today)

 
phade #:


It will display the previous day high in a blue line buffer, and the previous day low in a red line buffer:



It isn't keeping a trend of historic highs and lows of previous days, but I thought your only use case was to focus on the previous day highs and lows (before today)

Thank you vert much!!!

One question: why on MT4 this does not happen? I mean, i never had to create such a function to reinitialise the indicator. 


Thank's 

 
ironhak #:

Thank you vert much!!!

One question: why on MT4 this does not happen? I mean, i never had to create such a function to reinitialise the indicator. 

to be honest I'm not sure

Here is a new version of what I did which has more functionality with inputs:

https://www.mql5.com/en/code/46324


but I think I can  develop it more so that it can also plot the previous candle open and close

Plot the previous candlestick highs and lows of the selected period
Plot the previous candlestick highs and lows of the selected period
  • www.mql5.com
It will automatically plot a colored horizontal line on the previous highs and lows of the selected period, and it will make the lines consistent on this price when switching timeframes.
 
phade #:

to be honest I'm not sure

Here is a new version of what I did which has more functionality with inputs:

https://www.mql5.com/en/code/46324


but I think I can  develop it more so that it can also plot the previous candle open and close

Good! Thank you :) 

Reason: