Indicator values calculates after several ticks

 

Hi,

I am sure I am doing something wrong, but I did my research and couldn't find the answer.

Well, it is my first time creating an indicator, and I am confused with the results; the idea on my indicator is simple: I check the candle to see if it is bullish and closing above the high of the previous candle, and the buffer is the current high, and the opposite way for low. Technically, it doesn't make sense as I am new to this. I thought it would be a simple example to put in place, a kind of envelope of high and low. 

My issue, is that when launching the indicator, it takes several tick to have the required results, which I would like to have as soon as I launch the indicator. I am sure I am missing something so asking for your kind help on this.

Below is my code and a gif of how the indicator is behaving. 

Thanks in advance.



#property copyright "Copyright 2024, Grap the Trade"
#property link      "https://www.google.com"
#property version   "1.00"
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
#property indicator_buffers 2
#property indicator_plots   2
//--- plot High
#property indicator_label1  "PHigh"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDodgerBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Low
#property indicator_label2  "PLow"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

double PHighBuffer[];
double PLowBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,PHighBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,PLowBuffer,INDICATOR_DATA);
   
   ArraySetAsSeries(PHighBuffer,true);
   ArraySetAsSeries(PLowBuffer,true);

   IndicatorSetString(INDICATOR_SHORTNAME,"Simple High Low Indicator");
   PlotIndexSetString(0,PLOT_LABEL,"High");
   PlotIndexSetString(1,PLOT_LABEL,"Low");
   

   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[])
  {   
   ArraySetAsSeries(high,true);
   ArraySetAsSeries(low,true);
   ArraySetAsSeries(open,true);
   ArraySetAsSeries(close,true);

   for(int i = 1; i < rates_total -1 ; i++)
     {   
         PHighBuffer[i] = PHighBuffer[i+1];
         PLowBuffer[i] = PLowBuffer[i+1];
         
         if((close[i] > high[i+1] && close[i] > open[i]))
            { 
               PHighBuffer[i] = high[i];
            }
   
         if((close[i] < low[i+1] && close[i] < open[i]))
            { 
               PLowBuffer[i] = low[i];
            }                       
     }
     
   return(rates_total);
  }
 

Try this code.

for(int i = rates_total - 2 ; i >= 0; i--)

It is more efficient to calculate the data in the order of oldest to newest data.

 
Nagisa Unada #:
for(int i = rates_total - 2 ; i >= 0; i--)

Much appreciated, you are a KING :), I am really having the pleasure to write on this forum and really happy to have people that are willing to help. Thanks

Reason: