Need favour understanding how to loop through candles - how cycles work in MQL4

Jixo man

Trader
Jul 26, 2020
46
2
14
Good morning mql4 programers . Please I have read mql4 documentation on custom indicator and the use conditional operator: "for".... I understand that , we initialise the counter and if a condition is true a set of code is run. But I don't still don't understand. Please help.
 

Enivid

Administrator
Staff member
Nov 30, 2008
15,969
798
144
Odessa
www.earnforex.com
Operator for initiates a cycle. For example, you want to print all numbers from 1 to 10. The straightforward way to do that is to just put 10 lines of Print() functions:
MQL4:
Print("1");
Print("2");
// And so on...
Print("10");
But it is blunt and inefficient. Imagine if you wanted to print all numbers from 1 to 1,000,000? So in programming, you have cycles to repeat actions multiple times. Using a cycle with a for operator, you can print the numbers from 1 to 10 much easier:
MQL4:
for (int i = 1; i <= 10; i++) Print(i);
When the cycle starts, the variable i is assigned the value of "1". On each iteration of the cycle, the command Print(i) is executed. At the end of each iteration, the command i++ is executed (i++ is the same as i = i + 1). When i gets to "11", the cycle is finished - before it gets to Print("11").

I hope this helps.
 

Jixo man

Trader
Jul 26, 2020
46
2
14
Operator for initiates a cycle. For example, you want to print all numbers from 1 to 10. The straightforward way to do that is to just put 10 lines of Print() functions:
MQL4:
Print("1");
Print("2");
// And so on...
Print("10");
But it is blunt and inefficient. Imagine if you wanted to print all numbers from 1 to 1,000,000? So in programming, you have cycles to repeat actions multiple times. Using a cycle with a for operator, you can print the numbers from 1 to 10 much easier:
MQL4:
for (int i = 1; i <= 10; i++) Print(i);
When the cycle starts, the variable i is assigned the value of "1". On each iteration of the cycle, the command Print(i) is executed. At the end of each iteration, the command i++ is executed (i++ is the same as i = i + 1). When i gets to "11", the cycle is finished - before it gets to Print("11").

I hope this helps.

Firstly, this is really like a family here. Thanks for the quick responses. Just like checking for total orders in an instrument. I understand that. But I just want to
wrap it from Indicator_counted .
That's if there are 1000bars on a chart, I know On_calculate is 1000bars and previously_calculated is 9990 after the first tick. But do I loop from that 1000bars to the last candle checking for a moving average cross. Although I do copy paste code . I just wait to understand
 

Jixo man

Trader
Jul 26, 2020
46
2
14
So
Firstly, this is really like a family here. Thanks for the quick responses. Just like checking for total orders in an instrument. I understand that. But I just want to
wrap it from Indicator_counted .
That's if there are 1000bars on a chart, I know On_calculate is 1000bars and previously_calculated is 9990 after the first tick. But do I loop from that 1000bars to the last candle checking for a moving average cross. Although I do copy paste code . I just wait to understand

Some typo errors there. What I am saying is in the one dimensional array say MacdBuffer holding all the close of a slow moving average, how do you look for information on that array with the "for" operator. Or Maybe I am not asking the correct questions. Well one thing is certain in three months I must process in Mql4
 

Enivid

Administrator
Staff member
Nov 30, 2008
15,969
798
144
Odessa
www.earnforex.com
This will print all values for MacdBuffer array inside OnCalculate() function in MQL4 from the oldest value to the newest value:
MQL4:
for (int i = rates_total - 1; i >= 0; i--)
{
   Print(MacdBuffer[i]);
}

This will skip previously calculated bars but will always recalculate the latest (current) bar:
MQL4:
int counted_bars = prev_calculated > 0 ? prev_calculated - 1 : 0;
int limit = rates_total - 1 - counted_bars ;
for (int i = limit - 1; i >= 0; i--)
{
   Print(MacdBuffer[i]);
}

This will also work correctly in MQL5 if the array is set as series.
 
  • Like
Reactions: Jixo man

Jixo man

Trader
Jul 26, 2020
46
2
14
This will print all values for MacdBuffer array inside OnCalculate() function in MQL4 from the oldest value to the newest value:
MQL4:
for (int i = rates_total - 1; i >= 0; i--)
{
   Print(MacdBuffer[i]);
}

This will skip previously calculated bars but will always recalculate the latest (current) bar:
MQL4:
int counted_bars = prev_calculated > 0 ? prev_calculated - 1 : 0;
int limit = rates_total - 1 - counted_bars ;
for (int i = limit - 1; i >= 0; i--)
{
   Print(MacdBuffer[i]);
}

This will also work correctly in MQL5 if the array is set as series.
Thanks sir. I am beginning to get the login. Thank you . I am greatful.