Trailing Pending orders

Abubakar Abu Saidu  

Hi..

am trying to make a version of trailing pending order for mt5

always get order modify error

mql4 working pending trailing order

void TrailPendingOrders()  
  {
   if(Order_Type==Stop_Order && Mode==trail)
{
   double price;
   int    total,totalTrades=0;

//----
   total=OrdersTotal();
//----
   bool ordmdf;
   double opp=NormalizeDouble(OrderOpenPrice(),digits);

   for(int i=0; i<total; i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
        {
         totalTrades++;
         switch ( OrderType() ) 
                 {
                  case OP_BUYSTOP:
                     price=NormalizeDouble(Ask+pipoffset,digits);
                     if(!((price<opp)))
                        break;
                     ordmdf = OrderModify(OrderTicket(),price,0,OrderTakeProfit(),0,Blue);
                     if(!(!ordmdf))
                        break;
                     Print("Order Modify Error", (GetLastError()));
                     
                      break;
                  case OP_SELLSTOP:
                     price=NormalizeDouble(Bid-pipoffset,digits);
                     if(!((price>opp)))
                        break;
                     ordmdf = OrderModify(OrderTicket(),price,0,OrderTakeProfit(),0,Red);
                     if(!(!ordmdf))
                        break;
                    Print("Order Modify Error", (GetLastError()));
                     
                  }
                              }
            }

          }
}
//----
  }


mql5 code


extern int Distance=50;

//global var
double offset;

int OnInit() 
{
offset = NormalizeDouble(Distance * Point(), Digits());   //initialize crossed
}

void OnTick() 
{  
  
PendTrail(ORDER_TYPE_BUY_STOP, offset * Point(), TrailingStep * Point(), false,0); 
PendTrail(ORDER_TYPE_SELL_STOP, offset * Point(), TrailingStep * Point(), false,0);

}

void TrailingStopTrail(ENUM_ORDER_TYPE type, double TS, double step, bool aboveBE, double aboveBEval) //set Stop Loss to "TS" if price is going your way with "step"
  {
   TS = NormalizeDouble(TS, Digits());
   step = NormalizeDouble(step, Digits());
   int total = PositionsTotal();
   for(int i = total-1; i >= 0; i--)
     {
      if(PositionGetTicket(i) <= 0) continue;
      if(PositionGetInteger(POSITION_MAGIC) != MagicNumber || PositionGetString(POSITION_SYMBOL) != Symbol() || PositionGetInteger(POSITION_TYPE) != type) continue;
      MqlTick last_tick;
      SymbolInfoTick(Symbol(), last_tick);
      double SL = PositionGetDouble(POSITION_SL);
      double openprice = PositionGetDouble(POSITION_PRICE_OPEN);
      ulong ticket = PositionGetInteger(POSITION_TICKET);
   
  
    if(type == ORDER_TYPE_BUY_STOP && (!BE || last_tick.bid > openprice + TS + aboveBEval) && (NormalizeDouble(SL, Digits()) <= 0 || last_tick.bid > SL + TS + step))
         myOrderModify(ORDER_TYPE_BUY_STOP, ticket, last_tick.bid - TS, 0);
      else if(type == ORDER_TYPE_SELL_STOP && (!BE || last_tick.ask < openprice - TS - aboveBEval) && (NormalizeDouble(SL, Digits()) <= 0 || last_tick.ask < SL - TS - step))
         ModifyOrders(ORDER_TYPE_SELL_STOP, ticket, last_tick.ask + TS, 0);
     }
  }
int myOrderModify(ENUM_ORDER_TYPE type, ulong ticket, double SL, double TP) //modify SL and TP (absolute price), zero targets do not modify
  {
   if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) || !MQLInfoInteger(MQL_TRADE_ALLOWED)) return(-1);
   bool netting = AccountInfoInteger(ACCOUNT_MARGIN_MODE) != ACCOUNT_MARGIN_MODE_RETAIL_HEDGING;
   int retries = 0;
   int err = 0;
   SL = NormalizeDouble(SL, Digits());
   TP = NormalizeDouble(TP, Digits());
   if(SL < 0) SL = 0;
   if(TP < 0) TP = 0;
   //prepare to select order
   Sleep(10);
   if((type <= 1 && ((netting && !PositionSelect(Symbol())) || (!netting && !PositionSelectByTicket(ticket)))) || (type > 1 && !OrderSelect(ticket)))
     {
      err = GetLastError();
      myAlert("error", "PositionSelect / OrderSelect failed; error #"+IntegerToString(err));
      return(-1);
     }
   //ignore open positions other than "type"
   if (type <= 1 && PositionGetInteger(POSITION_TYPE) != type) return(0);
   //prepare to modify order
   double currentSL = (type <= 1) ? PositionGetDouble(POSITION_SL) : OrderGetDouble(ORDER_SL);
   double currentTP = (type <= 1) ? PositionGetDouble(POSITION_TP) : OrderGetDouble(ORDER_TP);
   if(NormalizeDouble(SL, Digits()) == 0) SL = currentSL; //not to modify
   if(NormalizeDouble(TP, Digits()) == 0) TP = currentTP; //not to modify
   if(NormalizeDouble(SL - currentSL, Digits()) == 0
   && NormalizeDouble(TP - currentTP, Digits()) == 0)
      return(0); //nothing to do
   MqlTradeRequest request;
   ZeroMemory(request);
   request.action = (type <= 1) ? TRADE_ACTION_SLTP : TRADE_ACTION_MODIFY;
   if (type > 1)
      request.order = ticket;
   else
      request.position = PositionGetInteger(POSITION_TICKET);
   request.symbol = Symbol();
   request.price = (type <= 1) ? PositionGetDouble(POSITION_PRICE_OPEN) : OrderGetDouble(ORDER_PRICE_OPEN);
   request.sl = NormalizeDouble(SL, Digits());
   request.tp = NormalizeDouble(TP, Digits());
   request.deviation = MaxSlippage_;
   MqlTradeResult result;
   ZeroMemory(result);
   while(!OrderSuccess(result.retcode) && retries < OrderRetry+1)
     {
      if(!OrderSend(request, result) || !OrderSuccess(result.retcode))
        {
         err = GetLastError();
         myAlert("print", "OrderModify error #"+IntegerToString(err));
         Sleep(OrderWait*1000);
        }
      retries++;
     }
   if(!OrderSuccess(result.retcode))
     {
      myAlert("error", "OrderModify failed "+IntegerToString(OrderRetry+1)+" times; error #"+IntegerToString(err));
      return(-1);
     }
   string alertstr = "Order modify: ticket="+IntegerToString(ticket);
   if(NormalizeDouble(SL, Digits()) != 0) alertstr = alertstr+" SL="+DoubleToString(SL);
   if(NormalizeDouble(TP, Digits()) != 0) alertstr = alertstr+" TP="+DoubleToString(TP);
   myAlert("modify", alertstr);
   return(0);
  }
void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | laser simplify @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
  }
bool OrderSuccess(uint retcode)
  {
   return(retcode == TRADE_RETCODE_PLACED || retcode == TRADE_RETCODE_DONE
      || retcode == TRADE_RETCODE_DONE_PARTIAL || retcode == TRADE_RETCODE_NO_CHANGES);
  }

please what am i doing wrong!!

Vladimir Karputov  
The main mistake:
ENUM_ORDER_TYPE type NOT EQUAL PositionGetInteger (POSITION_TYPE).

Correct this gross error first. Then you can move on.

Please read the MQL5 documentation: an order is NOT a POSITION !!!
Abubakar Abu Saidu  
Vladimir Karputov:
The main mistake:
ENUM_ORDER_TYPE type NOT EQUAL PositionGetInteger (POSITION_TYPE).

Correct this gross error first. Then you can move on.

Please read the MQL5 documentation: an order is NOT a POSITION !!!


void TrailPendingOrders(double price,double step) //set Stop Loss to "TS" if price is going your way with "step"
  {
   int    totalTrades=0;
   bool ordmdf;
   MqlTick last_tick;
   int total = OrdersTotal();
   for(int i = total-1; i >= 0; i--)
     {
      if(OrderGetTicket(i) <= 0) continue;
      if(OrderGetInteger(ORDER_MAGIC) != MagicNumber || OrderGetString(ORDER_SYMBOL) != Symbol()) continue;
      SymbolInfoTick(Symbol(), last_tick);
      double SL = OrderGetDouble(ORDER_SL);
      double currentprice = OrderGetDouble(ORDER_PRICE_CURRENT);
      double openprice = OrderGetDouble(ORDER_PRICE_OPEN);
      double pb=NormalizeDouble(getAsk()+price,_Digits);
      double ps=NormalizeDouble(getBid()-price,_Digits);
               step = NormalizeDouble(step, Digits());
      ulong ticket = OrderGetInteger(ORDER_TICKET);
         totalTrades++;
 switch ( (ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE) ) 
       {
    case ORDER_TYPE_BUY_STOP:
        if(openprice < pb + step) 
          break;
      ordmdf = m_trade.OrderModify(ticket,pb,0,0,ORDER_TIME_GTC,0);
         if(!ordmdf)
        break;
        Print("Order Modify Error", (GetLastError())); break;
    case ORDER_TYPE_SELL_STOP:
     if(ps < openprice + step)
            break;
       ordmdf = m_trade.OrderModify(ticket,ps,0,0,ORDER_TIME_GTC,0);
        if(!ordmdf)
              break; Print("Order Modify Error", (GetLastError()));
        }
     }
}
double getBid()
  {
   MqlTick last_tick;
   SymbolInfoTick(Symbol(), last_tick);
   return(last_tick.bid);
  }

double getAsk()
  {
   MqlTick last_tick;
   SymbolInfoTick(Symbol(), last_tick);
   return(last_tick.ask);
  }


Okay Thanks a lot working now!!

Reason: