OrderClose function not working correctly

OrderClose function not working correctly

Manage alerts

Loading saved threads...

Umer · External communityPost link
External question — Stack Overflow Stack Exchange Author: Umer Original post: https://stackoverflow.com/questions/67690700 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Is this the correct implementation of OrderClose() ? for(int ii = 0; ii < OrdersTotal(); ii++) { if(OrderSelect(ii, SELECT_BY_POS, MODE_TRADES)) { if(OrderType() == OP_BUY && OrderMagicNumber() == MagicStart && OrderSymbol() == symb) { int tikt = OrderTicket(); if(!OrderClose(tikt,OrderLots(),bid,4,clrPurple)) { Print("Close Error", GetLastError()); } } if(OrderType() == OP_SELL && OrderMagicNumber() == MagicStart && OrderSymbol() == symb) { int tikt = OrderTicket(); if(!OrderClose(tikt,OrderLots(),ask,4,clrPurple)) { Print("Close Error", GetLastError()); } } } } I am using this code right before opening a trade. For example, if there is a signal to buy then it closes the sell first and then opens a buy, and if there is a sell signal then it closes a sell first and then opens a buy. But it only does this for the first time and wont work after that. Let's say there is a sell signal. Then it will close the buy and open the sell, but when there is a second signal for a buy then it won't close the sell neither will it open a buy. There is no error in the experts tab. The only thing I receive in the experts tab is a message like this: Positions order mismatch . It does not appear like an error or a warning, it just appears as a message.
Quote
Report
tomgny · External communityPost link
External answer — Stack Overflow Stack Exchange Author: tomgny Original post: https://stackoverflow.com/a/67691824 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. If you want to close all orders, you need to start iteration from the latest order position to the first position. Try this: for(int i = OrdersTotal()-1; i >= 0; i--) { if(OrderSelect(i, SELECT_BY_POS)) { if(OrderType() == OP_BUY) { if(!OrderClose(OrderTicket(), OrderLots(), Bid, 0)) { Print("Error closing order: ", GetLastError()); } } if(OrderType() == OP_SELL) { if(!OrderClose(OrderTicket(), OrderLots(), Ask, 0)) { Print("Error closing order: ", GetLastError()); } } } } You shouldn't start iterating from 0 when you are closing orders, because OrdersTotal() function decrement its value while you loop through all orders and closing them one by one. For example (in your loop) you have 5 orders: i = 0, OrdersTotal() = 5 -> close order on position 0. i = 1, OrdersTotal() = 4 -> close order on position 1. i = 2, OrdersTotal() = 3 -> close order on position 2. i = 3, and now loop is over, because i == OrdersTotal(). Additionally, instead of int tikt= OrderTicket(); , just use OrderTicket() in OrderClose() function.
Quote
Report
PaulB · External communityPost link
External answer — Stack Overflow Stack Exchange Author: PaulB Original post: https://stackoverflow.com/a/67693428 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Try the following code which is more robust and does not assume the orders to be closed are of the same symbol of the chart the EA is running on. This will close all orders on the account. If you want to restrict this to orders that are on the chart the EA is running on, or orders only opened by the EA, you should use OrderSymbol() and OrderMagicNumber() to filter the orders before closing them. if(OrdersTotal()>0) { for(int i=OrdersTotal()-1; i>=0; i--) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if(OrderType()==OP_BUY || OrderType()==OP_SELL) { double price; if(OrderType()==OP_BUY) price=MarketInfo(OrderSymbol(),MODE_BID); else price=MarketInfo(OrderSymbol(),MODE_ASK); int ticket=OrderTicket(); bool res=OrderClose(ticket, OrderLots(), price, 50, clrNONE); if(!res) Print("Error Closing Ticket #", IntegerToString(ticket)); } else { int ticket=OrderTicket(); bool res=OrderDelete(ticket); if(!res) Print("Error Closing Ticket #", IntegerToString(ticket)); } } } }
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: tomgny Source score (net votes, not local likes): 2 Original post: https://stackoverflow.com/a/67691824 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. If you want to close all orders, you need to start iteration from the latest order position to the first position. Try this: for(int i = OrdersTotal()-1; i >= 0; i--) { if(OrderSelect(i, SELECT_BY_POS)) { if(OrderType() == OP_BUY) { if(!OrderClose(OrderTicket(), OrderLots(), Bid, 0)) { Print("Error closing order: ", GetLastError()); } } if(OrderType() == OP_SELL) { if(!OrderClose(OrderTicket(), OrderLots(), Ask, 0)) { Print("Error closing order: ", GetLastError()); } } } } You shouldn't start iterating from 0 when you are closing orders, because OrdersTotal() function decrement its value while you loop through all orders and closing them one by one. For example (in your loop) you have 5 orders: i = 0, OrdersTotal() = 5 -> close order on position 0. i = 1, OrdersTotal() = 4 -> close order on position 1. i = 2, OrdersTotal() = 3 -> close order on position 2. i = 3, and now loop is over, because i == OrdersTotal(). Additionally, instead of int tikt= OrderTicket(); , just use OrderTicket() in OrderClose() function.

Cancel quote

Checking account access…