In order to close all open market orders that are opened in MT4, you need to write and then call a similar function to the one below:
void CloseBuyOrders(string symbol=NULL,int magicNumber=-1)
{
int total=OrdersTotal();
for(int b=0; b<=total-1; b++)
{
if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
{
if((OrderMagicNumber()==magicNumber || magicNumber==-1) && (OrderSymbol()==symbol || StringLen(symbol)==0) && OrderType()==OP_BUY)
{
ResetLastError();
color arrowColor=clrSteelBlue;
if(!_ShowArrows) arrowColor=clrNONE;
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),10,arrowColor))
{
Print("Failed to close order in "+__FUNCTION__+", error="+(string)GetLastError());
return;
}
else
{
total=OrdersTotal();
b--;
}
}
}
}
}
As you can clearly see in the name of this MQL4 function, this is supposed to close the BUY positions only. A function/method for SELL orders would be quite the same, except for one difference - can you guess what that is? Post in the comments below if you know what needs to be changed in the "CloseSellOrders" function.
In the next post I'll probably do a generic function for closing both BUY and SELL orders, or only the type that was sent as a parameter. Thanks for learning MT4 with me!
No comments:
Post a Comment