Download MetaTrader 5

Problems with trailing stop

To add comments, please log in or register
Icarus
192
Icarus 2012.10.07 00:06 

Hi, Guys!

Please, I need some help with my trailing stops.

I would like to create a rule where, every time the close of yesterday is greater than the close of the day before, the stop moves an amount equal to the range of the last bar (that is, newstop = laststope + (previoushigh - previouslow)).

However, when modifying the order, I dont manage to make the system recognize the last stop.

I tried a reference by the ticket number of the last order, as well as by order type. Both didn't work, they are returning the value of the lastStop=0; 

The stoploss is working well, as well as the last bar range, just I could not make a reference to the previous stop. 

 

There follows the code:


input double   StopLoss1=0.002;

(...)

p_close1=mrate[1].close; //copia o fechamento anterior para p_close1

p_close2=mrate[2].close; 

(...)

 

//in the buy order, I put:

 

 mrequest.sl = latest_price.ask - StopLoss1; 

 

// and after: 


double newStop;
 
   newTrailing = 0; // declared as double before
   changeTrailing = false; // declared as bool before
  
   if(p_close1>p_close2)// close[1]>close[2]
     {
      newTrailing = mrate[1].high - mrate[1].low;
      changeTrailing = true;
     }
  
double lastStop = HistoryOrderGetDouble(mrequest.order,ORDER_SL);
 
      if(Buy_opened)
        {
         if(changeTrailing == true)
           {
           
            //--- placing an immediate order
           
            mrequest.action=TRADE_ACTION_SLTP;
           
            //--- instrument
           
            mrequest.symbol=Symbol();
           
            //--- Stop Loss
           
            newStop = lastStop + newTrailing;
            mrequest.sl = newStop;
           
           
            MqlTradeCheckResult CheckResult;
            //--- sending the order
            if(OrderCheck(mrequest,CheckResult)) {
            OrderSend(mrequest,mresult);
            Alert("trailing stop modificado ", mrequest.sl, " ", newTrailing, " ");
           
            }
            else
              {
               //--- printing the server response to the log 
               Print(CheckResult.retcode,"  -error ");
               Print("stoploss: ", mrequest.sl,"  ","ultimo preco: ", latest_price.last,"  buy:  ",latest_price.ask);
               Print(__FUNCTION__,":",CheckResult.comment);
              }
           }
        }

 

 

Thanks a lot!!! 

Biantoro Kunarto
15259
Biantoro Kunarto 2012.10.08 08:41  

Maybe you can try this:

double lastStop = HistoryOrderGetDouble(mrequest.order,ORDER_SL);

with :

if(PositionSelect(_Symbol))
   {
      double lastStop = PositionGetDouble(POSITION_SL);
   }
 
Icarus
192
Icarus 2012.10.09 04:38  
biantoro:

Maybe you can try this:

with :

 

biantoro, it worked just perfectly, thank you so much! :)

I still dont figure out why the HistoryOrderGetDouble didnt work...

 Thanks again!

Bruno 

 

To add comments, please log in or register