try travel backwards trought the open orders.
for(int i=OrdersTotal()-1;i>=0;i--){ OrderSelect(i,SELECT_BY_POS); ...... }
also you should call RefreshRates() between the orders...
try following code.
for(int i=OrdersTotal()-1;i>=0;i--){ if(OrderSelect(i,SELECT_BY_POS)>=0){ RefreshRates(); if(OrderType()==OP_BUY || OrderType()==OP_SELL){ OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,CLR_NONE); }else{ OrderDelete(OrderTicket()); } } }
Befor going live with such code you should add further errorhandling
Excellent! That works! Thanks so much!
While we are on the subject, I take it the Order numbers is 0,1,2,3... which is the reason for the OrderTotal()-1.
In order to get the difference in pips between the first and last position, it would be something like this?
if (OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES)==True) {LastOrder = OrderOpenPrice();} if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)==True) {FirstOrder = OrderOpenPrice();} PriceDiff = abs(LastOrder - FirstOrder);
In order to get the difference in pips between the first and last position, it would be something like this?
Why is that? Doesn't it pull up the first and last currently open orders?
If you modify an open order by reducing the lot size, you'll get a new ticket number and it will be at the end of the list, even though it was opened first.
If the ordering is important, you must loop and find them
int ticket[100]; double price[100]; datetime oot[100]; int count=0; for(int index = OrdersTotal() - 1; index >= 0; index--) if ( OrderSelect(index, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == MagicNumber // my magic number && OrderSymbol() == Symbol() ) { // and period and symbol ticket[count]= OrderTicket(); price[count] = OrderOpenPrice(); oot[count] = OrderOpenTime(); count++; } int order[100]; // Insertion sort for (int kk=0; kk<count; kk++) { // Keep ordering ticket[*], price[*] and oot[*] for (int ii=kk; ii>0 && oot[order[ii-1]] > oot[kk]; ii--) { order[ii]=order[ii-1]; // kk oot[kk] order[kk] oot[order[kk]] } // 0 3. 1 1. order[ii]=kk; // 1 1. 2 2. } // 2 2. 0 3. // ticket[order[0]] is the oldest // ticket[order[count-1]] is the latest
Wow, thanks a lot guys. I have much to learn about MQL.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I am trying to code an EA and at times I want the EA to close all open orders. I am using this code:
However, not all the open position close. I have attached a screenshot of the order close problem.
Order 1 is a Sell and closes properly
Order 2 & 3 are Buys. They both should close toghter, but you you see order 2 closes and order 3 closes well after 4 and 5 open.
Anytime there are multiple buys or sells in a row, they should all close at the same time, but they don't.
Any help would be appreciated.
Thanks,
C.J.