OK…this should be a very simple code. All I want is to close all currently open orders. I’ve written code on my own and searched the net for others’ and I still have no luck. In the attached code, all it will do is close the last order placed and [B]only if[/B] the script is run on the chart of that pair. I already know that I’ve complicated it by doing one loop for assigning ticket numbers and another loop to close by ticket number, but I was working off someone else code that was supposed to do it all at once by selecting the order by position rather than ticket number and it wasn’t working. Guess what…my way gives the same results. HELP!
int total = OrdersTotal();
int cnt = 0;
int tickNum[10];
int t = 0, x = 0;
for (cnt = total ; cnt >0 ; cnt--)
{
OrderSelect((cnt - 1),SELECT_BY_POS,MODE_TRADES);
Alert("Selecting ticket #",OrderTicket());
tickNum[t]=OrderTicket();
t++;
}
for (x=(total-1);x>=0;x--)
{
OrderSelect(tickNum[x],SELECT_BY_TICKET);
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,5);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,5);
}
I’ve tried looping from first to last and last to first but nothing makes any difference. Can someone please tell me what obvious thing I’m missing??
I am not familiar with the language used to write EA’s, but the for loop does not look right. I would try something like
for(int i = 1; i<=total; i++)
which is how I would write it in java. Also from looking at the documentation at Closing and Deleting Orders - Programming of Trade Operations - MQL4 Tutorial OrdreClose() and OrderSelect() return boolean values which you have not set to variables.
Maybe try this
int total = OrdersTotal();
int cnt = 0;
int tickNum[10];
int t = 0, x = 0;
bool status;
for (cnt=0;cnt<+total;cnt++)
{
if((OrderSelect(cnt,SELECT_BY_POS))==False)
Print("error, selected order invalid");
else{
Alert("Selecting ticket #",OrderTicket());
tickNum[t]=OrderTicket();
if(OrderType()==OP_BUY)
status = OrderClose(OrderTicket(),OrderLots(),Bid,5);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,5);
t++;}
if (status == true)
Alert("Closed order: ",OrderTicket())
else
Alert("Failed to close order: ",OrderTicket())
}
Tried it and got the alert “Failed to close ticket xxxxxxx” everytime for every open order. I’m beginning to wonder if there is some internal glitch with MT4. This should be a simple function.
very new to this, but just read - can’t use a unary operator with a comparison operator (that sounds corny so here is what I am trying to say)
for (cnt=0;cnt<+total;cnt++)
In the previous post- this for statement was used and from what I understand, can’t do a cnt [B]<+total [/B]type, trying to add 1 to total while comparing if less than cnt.
maybe try cnt < (total+1);cnt++
just guessing - and
sorry had a few adult beverages, otherwise i’d try to add more
I think the cnt<+total was simply a typo. I changed it to cnt<=total because obviously the first makes no sense. The problem lies deeper in the actual closing of the orders. The program loops through all the open orders just fine, closes the first order fine, but when it tries to close the others it fails for some reason. I get the else loop alert, “Failed to close order”
int cnt, total;
total = OrdersTotal();
for(cnt = 0; cnt < total; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType() <= OP_SELL) && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType() == OP_BUY) // long position is opened
{
OrderClose(OrderTicket(), OrderLots(), Bid, 3, Violet); // close buy position
}
else
{
OrderClose(OrderTicket(), OrderLots(), Ask, 3, Violet); // close short position
}
}
}
I can already see 1 failure in the code. If cnt starts at 0, the first order accessed is at position 0 and should close, looping cnt to 1. The problem now is that the order that was at position 1 will now be in position 0 and never get closed. This whole problem is averted by looping:
for(cnt=(total-1);cnt>=0;cnt--)
This is the only difference in your posted code and what I started with in the first place.
I appreciate all the suggestions…keep 'em coming. If anyone has actually been able to make this work, all the better.
OK…tried it and got the same results. As long as you run the script on the oldest order placed, that single order will close, but none of the others. If you run the script on any other chart, nothing closes at all. :mad:
Finally…a ray of hope. It seems that for some reason the script is only using the Bid/Ask of the chart it’s run on (found by GetLastError code 129, “Invaldid bid or ask”) Now I’m going to try to switch those up and get this sucker working.