Indicatore doesn't update correctly!!

 

Hi everyone,

I need support because I'm noticing a problem with my custom indicator.

The indicator provides me with a KPI that updates for each new bar.

I based an expert on it who, based on the value of the previous bar compared to the current one, must do or not do operations.

I had the doubt that correct values were not being generated, in fact this was the case, by printing the KPI as soon as the new bar was triggered I noticed that the values returned by the indicator were not correct.

Following the code, can you help me understand what's wrong?


Cutting pieces of code (OnCalculate Function) for convenience.


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 rates_use = Bars_Plot; 
           
       // SOME ARRAYSETRESIZE USING rates_use
           // SOME ARRAYRESIZE USING rates_use
       
       int limit = rates_use-prev_calculated; 
            
            for(int i = limit-1; i>=0; i--) 
               {  
                  RefreshRates();
                  //// SOME CALCULATIONS 
               }
               
      return(rates_use);
      
     }


Many thanks guys!!!

 
Sidi Mohamed El Alaoui: Following the code, can you help me understand what's wrong?
  1. First run prev_calculated is zero, so you process all bars. After that, you only process Bars-Bars_Plot most recent bars. Remove your variable and process only changed bars.
              How to do your lookbacks correctly #9#14 & #19 (2016)

  2. Your loop is processing in as-series order. Are you using any of those arrays? Did you set them to as-series?

    In MT5, you must set the direction.

    To define the indexing direction in the time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[] arrays, call the ArrayGetAsSeries() function. In order not to depend on defaults, call the ArraySetAsSeries() function for the arrays to work with.
              Event Handling / OnCalculate - Reference on algorithmic/automated trading language for MetaTrader 5

    Always post all relevant code (using Code button) or attach the source file.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

  3. Why are you calling RefreshRates?
 
William Roeder # :
  1. First run prev_calculated is zero, so you process all bars. After that, you only process Bars-Bars_Plot most recent bars. Remove your variable and process only changed bars.
               How to do your lookbacks correctly #9#14 & #19 (20 16 )

  2. Your loop is processing in as-series order. Are you using any of those arrays? Did you set them to as-series?

    In MT5, you must set the direction.

    Always post all relevant code (using Code button ) or attach the source file.
          How To Ask Questions The Smart Way . (20 04 )
               Be precise and informative about your problem

  3. Why are you calling RefreshRates ?

Hi William,

Thanks for your feedback, I'll try to update you by points.

I'll start by saying that in this message I'll post the file with the entire code and then the version containing the comments you reported (Fixed Version). The indicator I'm trying to make works on MT4.

Answering you in points:

  1. Can you tell me if the fixed version is correct?
  2. The code shows the fact that I set them as Series.
  3. RefreshRates is an error on my copy and paste, it has been removed.

I await your kind feedback.

A thousand thanks

 
There are many example indicators and also why start with mql4 in 2024 its like Windows 7...
Reason: