MQL5. My code does not work to close position. Please check what's wrong with it.

 

int temp_total_order=PositionsTotal(); for(int i=0;i<temp_total_order;i++){                            ulong os=PositionGetTicket(i);                      PositionSelectByTicket(os);                      MqlTradeResult result={0};                      MqlTradeRequest request={0};                      request.position=os;                      request.position_by=PositionGetInteger(POSITION_TICKET);                      request.magic=PositionGetInteger(POSITION_MAGIC);                      request.action=TRADE_ACTION_CLOSE_BY;                      request.symbol     =PositionGetString(POSITION_SYMBOL);                      uint order_send=OrderSend(request,result);                      Print(order_send);                ZeroMemory(request);                ZeroMemory(result); MqlTradeResult result={0}; MqlTradeRequest request={0}; request.order=PositionGetInteger(POSITION_TICKET); request.magic=trade_id; request.action=TRADE_ACTION_REMOVE; uint order_send=OrderSend(request,result); }

Hello coders,

I can't close running position and pending order. Please check the above code.

Thank's in advance.

 
#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

void CloseDeleteAll()
{
  for (int i = OrdersTotal() - 1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS))
      if (OrderType() <= OP_SELL)
        OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 100);
      else
        OrderDelete(OrderTicket());
}

void OnStart()
{
  CloseDeleteAll();
}
 
Al Imran Suvro:

Hello coders,

I can't close running position and pending order. Please check the above code.

Thank's in advance.

Unless you have a specific reason to be messing around with the low-level stuff, it's best interact with the stdlib classes. 

#include <Trade\Trade.mqh>
void OnStart()
{  
   CTrade trade;
   trade.SetAsyncMode(true);
   CPositionInfo pos;
   COrderInfo    ord;
   for(int i=PositionsTotal()-1; i>=0; --i)
      if(!pos.SelectByIndex(i) || !trade.PositionClose(pos.Ticket()))
         Print("Could not close position! Error: ",_LastError);
   for(int i=OrdersTotal()-1; i>=0; --i)
      if(!ord.SelectByIndex(i) || !trade.OrderDelete(ord.Ticket()))
         Print("Could not delete order! Error: ",_LastError);
   
}
 
fxsaber:

MT4 work well. I need MT5 code.

 
Al Imran Suvro:

MT4 work well. I need MT5 code.

Try this code in the MT5.

 
nicholi shen:

Unless you have a specific reason to be messing around with the low-level stuff, it's best interact with the stdlib classes. 

Thank you so much Nicholi Shen. it's working great.

 
fxsaber:

Try this code in the MT5.

Thank you so much Fxsaber. I appreciate your idea. My code already in mql5. I will test it when I use mql4 code in mql5.

 
Al Imran Suvro:

Thank you so much Nicholi Shen. it's working great.

I have a question for you which is if the client meta trader 5 has been deleted the header file 'Trade.mqh' then is my '.EX5' file will still work.

 
Al Imran Suvro:

I have a question for you which is if the client meta trader 5 has been deleted the header file 'Trade.mqh' then is my '.EX5' file will still work.

Yes. Once it's "compiled" you no longer need any source files. 

Reason: