# Need help to understand this please

#### Michael-M

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 Last edited by a moderator:

#### Michael-M

For some reason when I save the post the code appear incomplete

Last edited:

#### Enivid

Staff member
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.

#### Michael-M

Here is the Mq4 file

And thank you so much for your support, next time I will choose the language while I insert the code

#### Attachments

• 2.3 KB Views: 4

#### Michael-M

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

#### Enivid

Staff member
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.

#### Michael-M

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!

Replies
2
Views
788
Replies
2
Views
654
Replies
4
Views
779
Replies
6
Views
4K
Replies
8
Views
6K