Не могу закрыть ордер

Alexandr Andreev  

bool b = false;

void Buy()
  {
    MqlTick Tick;
    SymbolInfoTick(Symbol(),Tick);
    MqlTradeRequest request={0};
    MqlTradeResult  result={0};
    request.action = TRADE_ACTION_DEAL;                    
    request.symbol = Symbol();                              
    request.volume = 1;                                  
    request.type = ORDER_TYPE_BUY;                      
    request.price = Tick.ask;  
    if (!OrderSend(request,result)) PrintFormat("OrderSend ошибка %d",GetLastError());
    else Print(__FUNCTION__,": ",result.comment," код ответа ",result.retcode);
  }
  
void Sell()
  {
    MqlTick Tick;
    SymbolInfoTick(Symbol(),Tick);
    MqlTradeRequest request={0};
    MqlTradeResult  result={0};
    request.action = TRADE_ACTION_DEAL;                    
    request.symbol = Symbol();                              
    request.volume = 1;                                  
    request.type = ORDER_TYPE_SELL;                      
    request.price = Tick.bid;  
    if (!OrderSend(request,result)) PrintFormat("OrderSend ошибка %d",GetLastError());
    else Print(__FUNCTION__,": ",result.comment," код ответа ",result.retcode);
  }
  
void ClBuy()
  {
   MqlTradeRequest request = {0};
   MqlTradeResult  result = {0};
   int total=PositionsTotal();
   for(int pos=0; pos<total; pos++)
     {  
      if(PositionSelectByTicket(PositionGetTicket(pos)))
        {
          if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
            {
              //OrderClose(OrderTicket(),OrderLots(),Bid,0,clrGold);
              Print("Ticket ",PositionGetTicket(pos));
              request.action = TRADE_ACTION_CLOSE_BY;    
              request.position = PositionGetTicket(pos);  
              request.position_by = PositionGetInteger(POSITION_TICKET);    
              request.price = SymbolInfoDouble(Symbol(),SYMBOL_BID);  
              /*request.symbol = Symbol();    //PositionGetString(POSITION_SYMBOL);  
              request.volume = PositionGetDouble(POSITION_VOLUME);  
              request.type = ORDER_TYPE_BUY;*/

              if (!OrderSend(request,result)) PrintFormat("OrderSend ошибка %d",GetLastError());
              else Print(__FUNCTION__,": ",result.comment," код ответа ",result.retcode);
            }
        }
     }
  }

int OnInit()
  {
//---  
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if (b == false)
     {
       Buy();    
       ClBuy();
       b = true;
     }    
  
  }
//+------------------------------------------------------------------+
Возвращает ошибку отправки торгового запроса - 4756 (Не удалось отправить торговый запрос)
Vladimir Karputov  
              request.action = TRADE_ACTION_CLOSE_BY;    

TRADE_ACTION_CLOSE_BY

Закрыть позицию встречной


Может нужно:

TRADE_ACTION_DEAL

Установить торговый ордер на немедленное совершение сделки с указанными параметрами (поставить рыночный ордер)


(Пример открытия и закрытия: Типы торговых операций)
Aleksandr Bebishev  
Alexandr Andreev:

bool b = false;

void Buy()
  {
    MqlTick Tick;
    SymbolInfoTick(Symbol(),Tick);
    MqlTradeRequest request={0};
    MqlTradeResult  result={0};
    request.action = TRADE_ACTION_DEAL;                    
    request.symbol = Symbol();                              
    request.volume = 1;                                  
    request.type = ORDER_TYPE_BUY;                      
    request.price = Tick.ask;  
    if (!OrderSend(request,result)) PrintFormat("OrderSend ошибка %d",GetLastError());
    else Print(__FUNCTION__,": ",result.comment," код ответа ",result.retcode);
  }
  
void Sell()
  {
    MqlTick Tick;
    SymbolInfoTick(Symbol(),Tick);
    MqlTradeRequest request={0};
    MqlTradeResult  result={0};
    request.action = TRADE_ACTION_DEAL;                    
    request.symbol = Symbol();                              
    request.volume = 1;                                  
    request.type = ORDER_TYPE_SELL;                      
    request.price = Tick.bid;  
    if (!OrderSend(request,result)) PrintFormat("OrderSend ошибка %d",GetLastError());
    else Print(__FUNCTION__,": ",result.comment," код ответа ",result.retcode);
  }
  
void ClBuy()
  {
   MqlTradeRequest request = {0};
   MqlTradeResult  result = {0};
   int total=PositionsTotal();
   for(int pos=0; pos<total; pos++)
     {  
      if(PositionSelectByTicket(PositionGetTicket(pos)))
        {
          if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
            {
              //OrderClose(OrderTicket(),OrderLots(),Bid,0,clrGold);
              Print("Ticket ",PositionGetTicket(pos));
              request.action = TRADE_ACTION_CLOSE_BY;    
              request.position = PositionGetTicket(pos);  
              request.position_by = PositionGetInteger(POSITION_TICKET);    
              request.price = SymbolInfoDouble(Symbol(),SYMBOL_BID);  
              /*request.symbol = Symbol();    //PositionGetString(POSITION_SYMBOL);  
              request.volume = PositionGetDouble(POSITION_VOLUME);  
              request.type = ORDER_TYPE_BUY;*/

              if (!OrderSend(request,result)) PrintFormat("OrderSend ошибка %d",GetLastError());
              else Print(__FUNCTION__,": ",result.comment," код ответа ",result.retcode);
            }
        }
     }
  }

int OnInit()
  {
//---  
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if (b == false)
     {
       Buy();    
       ClBuy();
       b = true;
     }    
  
  }
//+------------------------------------------------------------------+
Возвращает ошибку отправки торгового запроса - 4756 (Не удалось отправить торговый запрос)

Таже беда - вы нашли решение?