You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Please use the </> button to insert your code.
My apologies... Here it is in proper format..
OrdersTotal() not correct...
I have been surprised by looping code sequence which uses OrdersTotal() not giving correct results (observed with two different brokers).
I am using MT4 version 1090 on a Linux Ubuntu-MATE 16.04 desktop, running WINE 3.0
Here is what I have been using...
Here it is:
I have noticed with two different brokers that the OrdersTotal() value does not always agree with what is shown in the 'Trade' tab of the MT4 broker. Initially, I thought that it was the broker causing OrdersTotal() to not function properly. When I noticed on the 2nd broker, I began to wonder if MT4 had an internal 'issue', or if my code was wrong OR if this was a problem with MT4 properly synchronizing with the server....?
After reading this forum thread, I wonder if I would have better results by altering the for..loop to read:
OR, is there a flag or line of code that will ensure that the OrdersTotal() is properly synchronized during an OnTick() event?Any clarification regarding this would be very helpful and would be greatly appreciated!
This is one of the most common errors I see, probably due in part to the likes of garbage such as Expert Advisor Builder. So I thought it was time for a thread dedicated to the subject so that it can be linked to for future reference.
The Problem
Let's take a simple example; we want a function to close all open orders for our EA, there are plenty of examples but let's create one from scratch.
We need a loop because we want to close all our orders for a specific EA, within this loop we will have code to select the order, code to check it is the correct symbol and magic number and finally code to close the order:
This code is bad . . . DO NOT USE IT . . . I will explain why in the next section . . .
The Explanation
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:
1st run through the loop:
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:
2nd run through the loop:
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:
3rd run through the loop:
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:
4th run through the loop:
now the value of PositionIndex is 3 OrderSelect() tries to select the Order at position 3 and fails, the continue takes execution of the code to the next value in the loop . .
5th and last run through the loop:
now the value of PositionIndex is 4 OrderSelect() tries to select the Order at position 4 and fails, 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.
The Solution
The following code is the correct approach when closing open orders or deleting pending orders . . .
The key difference is that the loop decrements from ( TotalNumberOfOrders - 1 ) to 0
Once again let's work through the code above . . . line by line, Order by Order . . .
We have the same orders as before:
1st run through the loop:
the initial value of PositionIndex is TotalNumberOfOrders - 1 which is equal to 5 - 1 = 4, so the order at position 4 is selected, ticket number 555, this order is successfully deleted and the remaining Orders change position as follows:
2nd run through the loop:
now the value of PositionIndex is 3 so the order at position 3 is selected, ticket number 444, this order is successfully deleted and the remaining Orders change position as follows:
3rd run through the loop:
now the value of PositionIndex is 2 so the order at position 2 is selected, ticket number 333, this order is successfully deleted and the remaining Orders change position as follows:
4th run through the loop:
now the value of PositionIndex is 1 so the order at position 1 is selected, ticket number 222, this order is successfully deleted and the remaining Orders change position as follows:
5th and last run through the loop:
now the value of PositionIndex is 0 so the order at position 0 is selected, ticket number 111, this order is successfully deleted, value 0 is the last valid value for the loop . . . the loop has finished.
We have successfully deleted all our matching orders . . .
Link to this thread: Loops and Closing or Deleting Orders
I have gone through this thread reading about order selection s. Indeed, I have a code where I believe to have done things right but the selection does not work for me as I cannot read the OpenPrice of the selected order. Everything else works fine, just that part of the code provided. I am not sure why.
The entire code has 4 sections where I need call the OrderOpenPrice right after that order is sent... OrderSend works well, OrderSelect won't get me the results I need. Refer to 1 section of the code if you can help.
Thank you.
Thanks dear Simon Gniadkowski,
Your post really saved my time.
This is the code I suggest for closing market orders
And I suggest that, this is the correct way if a forward for loop is used (to comply with FIFO Rules of US brokers)
In the forward loop,
Loops and Closing or Deleting Orders - MQL4 programming forum
For FIFO ( US brokers,)and you (potentially) process multiple orders per symbol, you must Find the earliest order, close it, and on a successful operation, reprocess all remaining positions.
CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
What are Function return values ? How do I use them ? - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
You'll know how much this has helped me. Not with just the code I was working on but my understanding entirely. It works perfectly now and I have the understanding to implement it in different ways.
Really really appreciate this info.