Download MetaTrader 5

OrderModify issues

To add comments, please log in or register
Kenneth Njuguna
181
Kenneth Njuguna  

Hi Everyone,,,I am trading on a demo and cant seem to get the ordermodify code to trail a stop. kindly assist as to where this code could be wrong. Thanks.

extern double    TrailingStop=100.0;

//////EFFECTING TRAILING STOPS!!  

//Trail the longs!!

      

        for(cnt=total-1;cnt>=0;cnt--)

           { if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)==true)

          

              { if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())

                  { if(OrderType()==OP_BUY)  

                     { if(TrailingStop>0)

                       {  

                          if(Bid-OrderOpenPrice()>Point*TrailingStop)

                            {

                              if((OrderStopLoss()<Bid-Point*TrailingStop)|| (OrderStopLoss()==0))

                                {

                                      //--- modify order and exit

                                   if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green))

                                     Print("OrderModify error ",GetLastError());

                                    return(0);

                                }

                             }

                        }                    

                     }

                  }

               }

            }

      

//Trail the shorts!!

      

       for(cnt=total-1;cnt>=0;cnt--)

          { if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)==true)

              { if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())

                  { if(OrderType()==OP_SELL)

                       { if(TrailingStop>0)

                         {

                            if((OrderOpenPrice()-Ask)>(Point*TrailingStop))

                              {

                                 if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))

                                  {

                                     //--- modify order and exit

                                     if(!OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Blue))

                                      Print("OrderModify error ",GetLastError());

                                     return(0);

                                   }

                               }

                           }

                                            

                       }

                  }

               }

         }  

        
Sergey Golubev
Moderator
94708
Sergey Golubev  
eevviill6
27
eevviill6  
int type;

  for(int i=OrdersTotal()-1; i>=0; i--)
  {
   if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
   if(OrderMagicNumber()!=Magic || OrderSymbol()!=Symbol()) continue;
  
  type=OrderType();

    if(type==OP_BUY)
      {
      if(Bid>=NormalizeDouble(OrderOpenPrice()+TrailingStart*point+TrailingStop*point,Digits))
      {
        if(NormalizeDouble(OrderStopLoss(),Digits)<NormalizeDouble(Bid-(TrailingStop+TrailingStep)*point,Digits))
        {
        if(NormalizeDouble(OrderStopLoss(),Digits)!=NormalizeDouble(Bid-TrailingStop*point,Digits))
        {
        while(IsTradeContextBusy()) Sleep(int(pause_if_busy*1000));
        RefreshRates();
          if(OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Bid-TrailingStop*point,Digits), OrderTakeProfit(), 0, clrNONE)) continue;
        }
        }
      }
      }
     else
    if(type==OP_SELL)
      {
      if(Ask<=NormalizeDouble(OrderOpenPrice()-TrailingStart*point-TrailingStop*point,Digits))
      {
        if((NormalizeDouble(OrderStopLoss(),Digits)>NormalizeDouble(Ask+(TrailingStop+TrailingStep)*point,Digits)) || OrderStopLoss()==0)
        {
        if(NormalizeDouble(OrderStopLoss(),Digits)!=NormalizeDouble(Ask+TrailingStop*point,Digits))
        {
        while(IsTradeContextBusy()) Sleep(int(pause_if_busy*1000));
        RefreshRates();
          if(OrderModify(OrderTicket(), OrderOpenPrice(), NormalizeDouble(Ask+TrailingStop*point,Digits), OrderTakeProfit(), 0, clrNONE)) continue;
        }
        }
      }
      }

  }
whroeder1
14784
whroeder1  
Do NOT use NormalizeDouble, EVER. For ANY Reason. It's a kludge, don't use it. It's use is always wrong
To add comments, please log in or register