Incorrect Chart candle high & Low after running for a while.

 

Hello,

The indicator picture below, is a simple indicator that places a dot above each candle high (green dot) and low (red dot). The indicator is working with real time data only, meaning as the indicator is forming, the high and low of that candle is used in placing an dot arrow above and below it.

The indicator works well for while (some hours or a day it could be longer), but for some reason, the error occurs where the arrows are not correct, where a low dot can be seen above a high or a high dot below a candle low.

  • The values are initialized for every new bar added to the chart.
  • From the indicator, the real time historical values are needed, hence the indicator plot buffer is not initialized, just set to the bar to begin plotting from
  • The computer is set, to always be on, no sleep mode or anything like that.
  • High dot values are gotten by adding a pip or two to the bar high. It occurred when no pip was added that is using just the high of the bar.
  • Low dot values are gotten by adding a pip or two to the bar low.
  • All calculation are done in "OnCalculate()" handler function. 
Any suggestion on what can be the cause would be appreciated, thanks.
     

    You already know what comes next ...

    Your Indicator code is faulty and the only possible way to offer you help is if you show your code.

    And even then, it depends on the code's complexity and if someone is willing to debug your code for you.

     
    Fernando Carreiro #:

    You already know what comes next ...

    Your Indicator code is faulty and the only possible way to offer you help is if you show your code.

    And even then, it depends on the code's complexity and if someone is willing to debug your code for you.

    So, I thought initially, as the real indicator is studing volatility among pairs, that I why I wrote a simple indicator just placing a dot above highs and below the lows, as the candles are forming (nothing fancy), and yet after a while, the incorrect output is still seen.

     
    Thank-god Avwerosuoghene Odukudu #: So, I thought initially, as the real indicator is studing volatility among pairs, that I why I wrote a simple indicator just placing a dot above highs and below the lows, as the candles are forming (nothing fancy), and yet after a while, the incorrect output is still seen.
    Simple or not, the answer is the same ... code is faulty!
     
    Thank-god Avwerosuoghene Odukudu: Any suggestion on what can be the cause would be appreciated, thanks.

    Do you really expect an answer? There are no mind readers here and our crystal balls are cracked, so we can't see your machine.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

    We can't see your broken code.

    Always post all relevant code (using Code button) or attach the source file.

     

    It looks like you are using something else (maybe iHigh()/iLow() or High[]/Low[]) instead of arrays that are passed by reference to the OnCalculate() function.

    https://www.mql5.com/en/forum/475546

     
    Thank-god Avwerosuoghene Odukudu: Hello, The indicator picture below...

    Interestingly, your last 10 bars appear to have the right calculation/display. If 10 bars is your intended lookback period, then eliminate the preceding superfluous dots with ...

    PLOT_DRAW_BEGIN (Plot settings: PlotIndexSetInteger - Creating application programs - MQL5 Programming for Traders - MetaTrader 5 algorithmic/automatic trading language manual).

    SetIndexDrawBegin

    Sets the bar number from which the drawing of the given indicator line must start

    Post edited by moderator

    SetIndexDrawBegin - Custom Indicators - MQL4 Reference
    SetIndexDrawBegin - Custom Indicators - MQL4 Reference
    • docs.mql4.com
    SetIndexDrawBegin - Custom Indicators - MQL4 Reference
     
    @Ryan L Johnson #: Interestingly, your last 10 bars appear to have the right calculation/display. If 10 bars is your intended lookback period, then eliminate the preceding superfluous dots with ...

    You are incorrectly answering a MQL4 question (In the MQL4 section) with MQL5 details. I have edited your post accordingly.

     
    Fernando Carreiro #:

    You are incorrectly answering a MQL4 question (In the MQL4 section) with MQL5 details. I have edited your post accordingly.

    Thanks, Fernando.Now I don't have to go through the trouble of editing it. I moved onto MT5 years ago and forgot how MQL5 code advancement left MQL4 in the dust. My bad.🙃

     
    Thank-god Avwerosuoghene Odukudu:
      Any suggestion on what can be the cause would be appreciated, thanks.

        I can't find the edit button on the original post, to include the below.

        The code in question

        #property indicator_buffers 2
        #property strict
        #property indicator_chart_window
        
        #property indicator_type1 DRAW_ARROW
        #property indicator_width1 2
        #property indicator_label1 "Up"
        #property indicator_color1 clrGreen
        
        #property indicator_type2 DRAW_ARROW
        #property indicator_width2 2
        #property indicator_label2 "Down"
        #property indicator_color2 clrRed
        
        
        //+------------------------------------------------------------------+
        //|      Global Parameters.                                          |
        //+------------------------------------------------------------------+
        bool  active;                                         // Active Signal
        int   AwUp = 159,                                     // Up arrow
              AwDn = 159;                                     // Down arrow
        double Up[],                                          // Up buffer
               Down[],                                        // Down buffer
               lastHigh,                                      // Last candle high
               lastLow;                                       // Last candle low
        
        
        //+------------------------------------------------------------------+
        //| Custom indicator initialization function                         |
        //+------------------------------------------------------------------+
        int OnInit()
          {
        //--- indicator buffers mapping
           int idx = 0;                                       // Index
        
        //--- Set High plot index and properties
           if(!SetIndexBuffer(idx, Up))
              return INIT_FAILED;
           SetIndexArrow(idx, AwUp);
           SetIndexEmptyValue(idx++, EMPTY_VALUE);
           ArraySetAsSeries(Up, true);
        
        //--- Set Low plot index and properties
           if(!SetIndexBuffer(idx, Down))
              return INIT_FAILED;
           SetIndexArrow(idx, AwDn); 
           SetIndexEmptyValue(idx, EMPTY_VALUE);
           ArraySetAsSeries(Down, true);
        
        //--- Set the indicator name and output precision
           IndicatorDigits(_Digits);
           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 limit = rates_total-prev_calculated,
               idx = 0;                                       // index of last bar
           if(prev_calculated==0)
             {
              for(int d=0; d<2; d++)
                 SetIndexDrawBegin(d, rates_total);
              return rates_total-1;
             }
        
        //--- Reset parameters and initialized Buffers used
           if(limit>0)
             {
              active = false;
              Up[idx] = EMPTY_VALUE;
              Down[idx] = EMPTY_VALUE;
              lastLow = lastHigh = 0;
             }
        
        //--- New candle added
           if(!active)
             {
              Up[idx] = lastHigh = high[idx];
              Down[idx] = lastLow = low[idx];
              active = true;
             }
        
        //--- Adjust the high and low for current candle
           else
             {
              //--- Adjust, if there is a change in the Candle high value
              if(lastHigh!=high[idx])
                {
                 Up[idx] = high[idx];
                 lastHigh = high[idx];
                }
        
              //--- Adjust, if there is a change in the Candle low value
              if(lastLow!=low[idx])
                {
                 Down[idx] = low[idx];
                 lastLow = low[idx];
                }
             }
        
        //--- return value of prev_calculated for next call
           return(rates_total);
          }
        //+------------------------------------------------------------------+
        

        I have included comments, just incase.

        My initial guess was this was as a result of the computer entering sleep mode (https://www.mql5.com/en/forum/457978),  but it still occurs, when the system sleep is disabled, and let to run for a while (hours or a day or more).

        Also, this is the result (display output) as seen in the main indicator, I first noticed this behaviour.

        Incorrect Values

        Thanks for your time.

        MT4 hang after Sleep Mode
        MT4 hang after Sleep Mode
        • 2023.11.24
        • Frieder Kauber
        • www.mql5.com
        Hello at all, I have programmed a quite extensive EA, multicurrency with many built-in indicators and graphical elements...
         
        William Roeder #:

        Do you really expect an answer? There are no mind readers here and our crystal balls are cracked, so we can't see your machine.
             How To Ask Questions The Smart Way. (2004)
                  Be precise and informative about your problem

        We can't see your broken code.

        Always post all relevant code (using Code button) or attach the source file.

        Hello, Good day,

        the code is nothing complex, the problem occurs after a while of running on the chart. I did not see anyone actually, running the code in a chart or two for hours, to generate the issues, hence why am looking for suggestions, if someone has faced something similar before.