Need help to understand this please

Michael-M

Trader
May 8, 2020
9
0
17
35
I need some advice for the below code if you please,

Period_MA is an external variable and I set it to 20, but when I am testing my code by changing
MQL4:
for(x=i; x<i+Period_MA; x++)
to
MQL4:
for(x=i; x<=i+Period_MA; x++)
the moving average disappear from the chart
MQL4:
   int Counted_Bars = IndicatorCounted();
   int Uncounted_Bars = Bars - Counted_Bars;
   Uncounted_Bars++;
 
   for(int i=0; i<=Uncounted_Bars; i++)
     {
      double Total = 0;
         for(x=i; x<i+Period_MA; x++)
           {
            Total = Total + iClose(NULL,0,x);
           }
 
       Closing_Price_Array[i] = Total / Period_MA;
     }
Noting that when I add the equal sign to the previously stated line of code , I get the moving average to widen like below, so I just need to know how it was calculated just by adding an equal sign so that will be the outcome

Thank you
1619191356601.png
 
Last edited by a moderator:
You need to set the code language to MQL4/MQL5 for it to display without issues.

Why do you use iClose instead of Close? The latter is much faster.

I don't know why you are experiencing this issue, but if you shared the .mq4 file, I could probably check it.
 
just incase the code should be
MQL4:
for(x=i;x<i+Period_MA; x++)
But I am trying to understand how an equal sign could make that difference, how it has been calculated
 
When you use the "strict less" (<), the moving average doesn't disappear - it tracks the Close price rather closely. When you change it to "less or equal" (<=), the MA gets moved up because your sum (Total) increases by one more Close value but you continue to divide it by the unchanged Period_MA. For example, if Period_MA = 11, you sum 12 values of Close and then divide it by 11.
 
When you use the "strict less" (<), the moving average doesn't disappear - it tracks the Close price rather closely. When you change it to "less or equal" (<=), the MA gets moved up because your sum (Total) increases by one more Close value but you continue to divide it by the unchanged Period_MA. For example, if Period_MA = 11, you sum 12 values of Close and then divide it by 11.
Yes, you are right but isn't the variance that this one candle has affected the moving average huge!