How to keep previous buffer if condition is not met?? - page 2

 
 limit=rates_total-prev_calculated;
   if(prev_calculated>0)
      limit++;

    for(int i=limit-1; i>=0; i--)

You never update bar one (last received tick) when a new bar starts.
          How to do your lookbacks correctly #9#14 & #19

 
Fernando Carreiro #:

No. The parameters passed by the OnCalculate are high[] and low[], not High[] nor Low[]. Why you ask?

Because the Indicator may be placed on an "Offline Chart" in the case of MT4 ("Custom Symbol" in the case of MT5) and in those cases iHigh, iLow, High[] and Low[] will ALL return the wrong data. Also, even if the indicator is on a normal chart, there is no guarantee that the data presented by the OnCalculate parameters are synchronised with the chart data. They may in fact be out of sync at certain times.

The correct way is to always to use the data passed by the event handler function.

Now for the code. Your logic seems suspect, so I changed it a little to what I believe you want, but you will have to review it to see if that is the case.


Thank's, now it work how it's supposed to, just needed to change the additions and differences operations inside if statement. 

I've further question about coding, if you are ok with answering, of course. 

  1. Why did you typed "high[]" instead of "High[]"? What's the difference?
  2. Could you please explain me, or point me a place where it's explained, how the structure of for loop works? I know how it works, I'm interested in understanding why you did this:
    • int iMaxIndex = rates_total - 1;
               
      // Main loop — fill in the arrays with data values
      
      for( int i = rates_total - ( prev_calculated < 1 ? 1 : prev_calculated ); i >= 0; i-- )
      

  • Instead of this:
    for ( int indx = 0; indx <= Bars - 1; indx++ ) 
    

Thank's for your guidance, I didn't knew I can create buffers and using their index instead of having to #define them every time. 

 
ironhak #: Thank's, now it work how it's supposed to, just needed to change the additions and differences operations inside if statement. I've further question about coding, if you are ok with answering, of course. 

  1. Why did you typed "high[]" instead of "High[]"? What's the difference?
  2. Could you please explain me, or point me a place where it's explained, how the structure of for loop works? I know how it works, I'm interested in understanding why you did this:
  1. "high[]" is a parameter passed in the OnCalulate() event handler (please see documentation), and "High[]" is an internal predefined variable for MQL4 (not available on MQL5). The two are different things.
  2. The structure of the loop is just part of understanding how arrays are indexed and how the OnCalulate() event handler works (please click the links and follow up on the documentation).

Try to study more examples in the CodeBase you so can see the different ways people code their indicators.

And read William's post #11 and follow up on the links provided.

 
Fernando Carreiro #:
  1. "high[]" is a parameter passed in the OnCalulate() event handler (please see documentation), and "High[]" is an internal predefined variable for MQL4 (not available on MQL5). The two are different things.
  2. The structure of the loop is just part of understanding how arrays are indexed and how the OnCalulate() event handler works (please click the links and follow up on the documentation).

Try to study more examples in the CodeBase you so can see the different ways people code their indicators.

And read William's post #11 and follow up on the links provided.

ok, now I get it. Another question if you will. 

Why sometimes you call variables "dbVariabileName" and other times "g_adbVariableName"? I get that "db" stands for double, why "g_a..."? Thank's, I was curious because i see this notation on several codes.

Edit:

Also, there is any difference between doing this:

   SetIndexStyle(0,DRAW_LINE, STYLE_SOLID, 3, clrDodgerBlue);
   SetIndexLabel(0,"High Line");

And doing this:

      #property indicator_label1    "High Line"
      #property indicator_type1     DRAW_LINE
      #property indicator_color1    clrDodgerBlue
      #property indicator_style1    STYLE_SOLID
      #property indicator_width1    3

Personally I find more convenient "my" method, but I saw in your code that you removed and did everything trought #property.. what the difference? Thank's. 

 
ironhak #: Also, there is any difference between doing this: And doing this: Personally I find more convenient "my" method, but I saw in your code that you removed and did everything trought #property.. what the difference?

So, that the user has the option of changing the colours, line width and style. If you do it in the code, it will be "hardwired" and the user will not be able to change it in the properties.

ironhak #: Why sometimes you call variables "dbVariabileName" and other times "g_adbVariableName"? I get that "db" stands for double, why "g_a..."? Thank's, I was curious because i see this notation on several codes.

Forum on trading, automated trading systems and testing trading strategies

iCustom giving unexpected values

Fernando Carreiro, 2022.05.16 17:28

By the way, if you wondering why I use those "weird" prefixes in my variable names, it is to help identify them more easily.

I use "i_" for inputs and "g_" for global variables. I use "a" for arrays, "h" for handles, "n" for integer based numbers, "db" for double floating point, and several others not present in this code.

So "g_adbAtrBuffer" is a global variable of an array of double precision floating point for the ATR buffer, ...

and "i_nAtrPeriod" is a input variable of an integer numeric for the ATR period.

This is not necessary. It is just the coding style I am adopting. You should use what ever style you feel comfortable using that helps you read your code more easily.
 
Fernando Carreiro #:

So, that the user has the option of changing the colours, line width and style. If you do it in the code, it will be "hardwired" and the user will not be able to change it in the properties.

Thank's very much Fernando, thank's to your help I was able to learn several new things. 

Wish you a good day. 

 
ironhak #: Thank's very much Fernando, thank's to your help I was able to learn several new things. Wish you a good day. 

You are welcome!

 
Fernando Carreiro #:

You are welcome!

Hi, sorry, but I’ve another question lol 
What if I’m inside OnCalculate and I need to access previous high of another TF? In that case it would make sense to use iHigh(), is that the case?

Tanks 
 
ironhak #: Hi, sorry, but I’ve another question lol. What if I’m inside OnCalculate and I need to access previous high of another TF? In that case it would make sense to use iHigh(), is that the case?

The best method is to build higher time-frame data from the current data provided by OnCalculate(). This will guarantee that it will work correctly with Offline Charts on MT4 (Custom Symbols on MT5).

However, if you choose to ignore the use of Offline Charts, then you can use iHigh() but you will also have to use iBarShift() to synchronise the indexing into the data.

iBarShift - Timeseries and Indicators Access - MQL4 Reference
iBarShift - Timeseries and Indicators Access - MQL4 Reference
  • docs.mql4.com
iBarShift - Timeseries and Indicators Access - MQL4 Reference
 
Fernando Carreiro #:

The best method is to build higher time-frame data from the current data provided by OnCalculate(). This will guarantee that it will work correctly with Offline Charts on MT4 (Custom Symbols on MT5).

However, if you choose to ignore the use of Offline Charts, then you can use iHigh() but you will also have to use iBarShift() to synchronise the indexing into the data.

Hi, can you point me an example to this? 

Im asking because I made a script which calculate the average difference of highs and lows on a chosen TF for all the available candles. The script used iHigh(Symbol(), chosenTF, i) - iLow(Symbol(), chosenTF, i) where i goes from current bar to first bar avi able and at the end the script just reports delta/i.

The point is that if For example I want average candle size of 1Day, then if I attach the script to two different timeframes of the same pair I will get very differente result… which of course shouldn’t be the case.

Hope you understood, it would be helpful if you point me some places where I can find more about this.

Thanks again for your kindness.