Closing All Long/Short for a symbol
0 replies
Tomas Matejka
9 years ago #117105
Hello,
to close long or short only for specified symbol you can do this:
1) add custom code into CustomFunction.mq4 placed in Wizard folder
void closeAllOrders(string direction, string symbolStr) {
int total = OrdersTotal();
for(int i=total-1;i>=0;i--) {
OrderSelect(i, SELECT_BY_POS);
int type = OrderType();
bool result = false;
if (direction == "LONG" && type == OP_BUY && OrderSymbol() == symbolStr) {
result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
}
else if (direction == "SHORT" && type == OP_SELL && OrderSymbol() == symbolStr) {
result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
}
if(result == false) {
Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}
return(0);
}
2) add CustomAction like this in Wizard
closeAllOrders("LONG", "EURUSD");
This for example will close all long positions for EURUSD
Let me know if any questions
0