Let's learn MQL4 online together

Read the article again and again bro. Especially this bit

[B]The Explanation[/B]

Let’s work through the code above . . . line by line, Order by Order . . .
Let’s assume we have the following Orders that we want to close, they all have the same magic number and Symbol as our EA so we want our code to close them all:

[TH=“bgcolor: #CACACA, align: center”]Position[/TH]
[TH=“bgcolor: #CACACA, align: center”]Ticket Number[/TH]

0
111

1
222

2
333

3
444

4
555

[U]1st run through the loop:[/U]
the initial value of PositionIndex is 0 so the order at position 0 is selected, ticket number 111, this order is successfully deleted and the remaining Orders change position as follows:

[TH=“bgcolor: #CACACA, align: center”]Position[/TH]
[TH=“bgcolor: #CACACA, align: center”]Ticket Number[/TH]

0
222

1
333

2
444

3
555

[U]2nd run through the loop:[/U]
now the value of PositionIndex is 1 so the order at position 1 is selected, ticket number 333, this order is successfully deleted and the remaining Orders change position as follows:

[TH=“bgcolor: #CACACA, align: center”]Position[/TH]
[TH=“bgcolor: #CACACA, align: center”]Ticket Number[/TH]

0
222

1
444

2
555

[U]3rd run through the loop:[/U]
now the value of PositionIndex is 2 so the order at position 2 is selected, ticket number 555, this order is successfully deleted and the remaining Orders change position as follows:

[TH=“bgcolor: #CACACA, align: center”]Position[/TH]
[TH=“bgcolor: #CACACA, align: center”]Ticket Number[/TH]

0
222

1
444

[U]4th run through the loop:[/U]
now the value of PositionIndex is 3 OrderSelect() tries to select the Order at position 3 and [B]fails[/B], the continue takes execution of the code to the next value in the loop . .
[U]
5th and last run through the loop:[/U]
now the value of PositionIndex is 4 OrderSelect() tries to select the Order at position 4 and [B]fails[/B], the continue takes execution of the code to the next value in the loop . . . the loop has finished.

We are now left with 2 Orders, tickets 222 and 444 which should have been closed but were not . . . next, how to resolve this issue.

It explains it far better than I will. When just working with orders the

PositionIndex++

is a valid format to loop through orders.

The problem arises when you start deleted/closing orders. Your code has a number of those functions, so the solution is to use

PositionIndex--

Thank you for your reply. I understand it now.

So the article states that when you increase PositionSize with ++, then you will skip the order-step in the order pool. Since orders are being sorted starting from index 0 every time another position has been closed, the oldest position will change from index 1 to index 0. So if the loop increase by ++, then it skips the ‘sorting to index 0’ process. This can be easily solved by changing ++ to --.

Thank you again for the explanation. It’s a very very good troubleshooting knowledge for fixing bugs.

How is everyone doing with their MQL-projects?
If you haven’t started MQL learning yet. I suggest to start with Dandy tutorials on: LESSON PROJECTS - JimDandyForex.com. Download his EA’s and read the code, try to understand why he would code it this way and then try to modify it step by step to tweak it to your taste.
If you’re a beginner, I’d like to know which beginner MQL projects you finished. (MA crossover probably)
If you’re more advanced, I’d like to know about your intruiging project.

[B]Question about MQL IDE:[/B]

  1. How much better is Scite over MetaEditor? Scite-Mql

I’m still looking for an alternative to MetaEditor + StrategyTester and this is doesn’t fit my requirement.

leme answer ,double :nerd_face:

Though I mostly use MQL5 for downloading including indicators, EAs, signals etc, I am open to learning new things with equal and unparalleled excitement.

Learning to code requires more effort than you read about them. Once you are done with grasping all that you could have, the next step is to practice and lots of practice.

1 Like