Trailing Stop after OrderSend(), not work, please give suggestion

 

I try to put a trailing stop after OrderSend(), but it not work, please give suggestion. Thanks.

Code:

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Stoplose,TakeProfit,"My EA",MagicNumber,0,Red);
      if(ticket>0)
         {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
         Print("SELL order opened : ",OrderOpenPrice());
         }
         else Print("Error opening SELL order : ",GetLastError());
         return(0);
      }

if(OrderType()==OP_SELL){

// check for trailing stop - SELL
if(TrailingStop>0)
{
   if((OrderOpenPrice()- Ask) > Point*TrailingStop)
   {
      if(OrderStopLoss()<Ask+Point*TrailingStop)
      {
      OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
      return(0);
      }
   }
}
                          }
 

You can NOT use OrderTicket(), OrderOpenPRice() etc unless you do a orderSelect FIRST.

Unless you made ticket a static int, you've lost its value when you return.

You can either do a OrderSelect(ticket) in the first case or do an orderSelect(position) loop to find the open order.
 
Select ticket or find open order. Thanks WHRoeder
 
//+------------------------------------------------------------------+
//|                                                         Sell.mq4 |
//|                       Copyright ?2012, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright ?2012, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

//---- input parameters
extern double Lots = 1;
extern int MagicNumber = 54321;
extern int Slippage = 3;
extern double TrailingStop = 50;
extern double StopLose = 1.3487;
extern double TakeProfit = 1.3400;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   int ticket, total, i;
     
//start trade - Sell
total = OrdersTotal();
if(total < 1) //if no trade open
   {
   if (Close[1] > Open[1])
      {
      ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,StopLose,TakeProfit,"My EA - SELL - TrailingStop",MagicNumber,0,Red);
      if(ticket>0)
         {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
         Print("SELL order opened : ",OrderOpenPrice());
         }
         else Print("Error opening SELL order : ",GetLastError());
         return(0);
      }
      for(i=0;i<total;i++)
      {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) continue;
         if(OrderType()==OP_SELL)
         {
            if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
            {
               if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
               {
               OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
               return(0);
               }
            }
         }
      }
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+

I not set for 4/5 digit broker(still learning), please ignore it.

I use a loop to check any opened trade and try to Trailing Stop on it, It not work.

OrderSelect by index, not ticket.

Please give suggestion.

Thanks

 
FXEWEN:

I not set for 4/5 digit broker(still learning), please ignore it.

I use a loop to check any opened trade and try to Trailing Stop on it, It not work.

OrderSelect by index, not ticket.

Please give suggestion.

Thanks

Does not work in what way ? does the OrderModify get called but fails ? maybe the OrderModify doesn't get called ? why aren't you checking the return value from the OrderModeify and reporting any errors ? why do you continue if an OrderSelect fails ? don't you want to know that it has failed and the error ? What are Function return values ? How do I use them ?

By the way, what is the value of total when you try to implement the trailing stop ?

 
You have
you want
if(total < 1) //if no trade open
   {
   if (Close[1] > Open[1])
      {
         try to open
         return(0);
      }
      for(i=0;i<total;i++)
      {
         if(!OrderSelect(i,SELECT_BY_POS)) continue; 
^^^ This line will never be called because total < 1 ^^^
if(total < 1) //if no trade open
   {
   if (Close[1] > Open[1])
      {
         try to open
         return(0);
      }
}
// vv This and below is ALWAYS done. vv
for(i=0;i<total;i++) 
   {
   if(!OrderSelect(i,SELECT_BY_POS,)) continue; 
Reason: