Trading stop loss problem

[Deleted]  

Hello. I am working on my EA and I am having some problems with trailing stop loss. Here is code I have found on some tutorial that I use for trailing:

void start()
{
   total = OrdersTotal();   
   if(total != 0)
   {   
      for(int cnt=0; cnt<total; cnt++) 
      {
         if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) != true)
         {
            Print("Order select #", cnt, " failed with error: ", GetLastError());   
         }
         else if(/*OrderType()<=OP_SELL && */OrderSymbol()==Symbol()) // I believe this is not neccessary, so I commented it out: OrderType() <= OP_SELL
         {
            if(OrderType()==OP_BUY)   //<-- Long position is opened
            {
               trail_order(OrderType());
            }
            else if(OrderType()==OP_SELL) //<-- Go to short position
            {
               trail_order(OrderType());
            }
         }
      }
   }
}
void trail_order(int type)
{
   if(UseTrailingStopLoss)
   {   
      if(type==OP_BUY)
      {
         if((Bid-OrderOpenPrice())>(Point*TrailingStopLoss))
         {
            if(OrderStopLoss()<(Bid-Point*TrailingStopLoss))
            {
               OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStopLoss,OrderTakeProfit(),0,Green);
            }
         }
      }
      if(type==OP_SELL)
      {
         if((OrderOpenPrice()-Ask)>(Point*TrailingStopLoss))
         {
            if((OrderStopLoss()>(Ask+Point*TrailingStopLoss)) || (OrderStopLoss()==0))
            {
               OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStopLoss,OrderTakeProfit(),0,Red);
            }
         }
      }
   }
}

Problem I am having is that trailing does not start immediately after market go in my favor, but after greater distance is made. For example, if BUY order is sent at price 1.200, and I use stop loss of 15 pips, then it is set to 1.185. If price raise to 1.201, trailing stop loss should change stop loss to 1.186, but it does not. Does anybody know what to change in code above to make this work?

Another problem I have is that sometimes trailing stop loss goes mad. Here is report from journal:

2009.03.03 18:10:27 MACD-CCI-timeframe EURUSD,M1: modify #9472007 buy 50000.00 EURUSD at 1.2546 sl: 1.2561 tp: 1.2595 ok
2009.03.03 18:08:48 MACD-CCI-timeframe EURUSD,M1: modify #9472007 buy 50000.00 EURUSD at 1.2546 sl: 1.2560 tp: 1.2595 ok
2009.03.03 18:08:43 MACD-CCI-timeframe EURUSD,M1: modify #9472007 buy 50000.00 EURUSD at 1.2546 sl: 1.2557 tp: 1.2595 ok
2009.03.03 18:08:34 MACD-CCI-timeframe EURUSD,M1: modify #9472007 buy 50000.00 EURUSD at 1.2546 sl: 1.2556 tp: 1.2595 ok
2009.03.03 18:07:41 MACD-CCI-timeframe EURUSD,M1: modify #9472007 buy 50000.00 EURUSD at 1.2546 sl: 1.2554 tp: 1.2595 ok
2009.03.03 18:07:38 MACD-CCI-timeframe EURUSD,M1: modify #9472007 buy 50000.00 EURUSD at 1.2546 sl: 1.2553 tp: 1.2595 ok
2009.03.03 18:07:34 MACD-CCI-timeframe EURUSD,M1: modify #9472007 buy 50000.00 EURUSD at 1.2546 sl: 1.2551 tp: 1.2595 ok
2009.03.03 18:07:28 MACD-CCI-timeframe EURUSD,M1: modify #9472007 buy 50000.00 EURUSD at 1.2546 sl: 1.2550 tp: 1.2595 ok
2009.03.03 18:02:08 MACD-CCI-timeframe EURUSD,M1: modify #9472007 buy 50000.00 EURUSD at 1.2546 sl: 1.2549 tp: 1.2595 ok
2009.03.03 18:02:05 MACD-CCI-timeframe EURUSD,M1: modify #9472007 buy 50000.00 EURUSD at 1.2546 sl: 1.2548 tp: 1.2595 ok
2009.03.03 18:01:34 MACD-CCI-timeframe EURUSD,M1: modify #9472007 buy 50000.00 EURUSD at 1.2546 sl: 1.2546 tp: 1.2595 ok
2009.03.03 17:57:18 MACD-CCI-timeframe EURUSD,M1: OrderSend (BUY) OK.
2009.03.03 17:57:18 MACD-CCI-timeframe EURUSD,M1: open #9472007 buy 50000.00 EURUSD at 1.2546 sl: 1.2530 tp: 1.2595 ok
Thanks in advance for all help.
Alexander
2441
Alexander  

Just a little fixing:

void trail_order(int type)
{
   if(UseTrailingStopLoss)
   {   
      if(type==OP_BUY)
      {
         //if((Bid-OrderOpenPrice())>(Point*TrailingStopLoss))
         //{
            if(OrderStopLoss()<(Bid-Point*TrailingStopLoss))
            {
               OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStopLoss,OrderTakeProfit(),0,Green);
            }
         //}
      }
      if(type==OP_SELL)
      {
         //if((OrderOpenPrice()-Ask)>(Point*TrailingStopLoss))
        // {
            if((OrderStopLoss()>(Ask+Point*TrailingStopLoss)) || (OrderStopLoss()==0))
            {
               OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStopLoss,OrderTakeProfit(),0,Red);
            }
        // }
      }
   }
}
[Deleted]  
This is to trail at once? Do you have any idea why I have secon problem. Thank you for replaying so fast.
Alexander
2441
Alexander  
I don't see any wrong stuff from your report, it's normal.
[Deleted]  

Well, price did not change, but stop loss has changed on every tick. I guess that is not normal? Not sure. Stop loss should change only if price changes in my favor? Right?

Also, could you please explain why author of this code put those lines you commented :)

Edit:

Oh, I get it, this is order open price. It is OK.

Alexander
2441
Alexander  

Classic TrailingStop starts working when the order has a profit more then the value of trailingstop.

DougRH4x
541
DougRH4x  
adnank wrote >>
This is to trail at once? Do you have any idea why I have secon problem. Thank you for replaying so fast.

Hi,

I can't help you fix yours but I found a great little very reasonably priced and flexible and effective commercial utility that has a 2 stage Trailing Stop Loss that you can use either on its own or in conjunction with other AEs (it overrides the TSL in other AEs). It is from PipBoxer.Com: look for PBTS. Being a commercial product it is only available in compiled format of course.

Good luck with it.

…………………(8 >) Prosperous Trading (< 8)

DougRH4x

.

PS: I’m looking for someone to splice an adjustable (Trailing &) Stop Loss into the Martingale based program: PipMaker. Overcoming this one downfall of this program will make it VERY profitable!

[Deleted]  
DougRH4x wrote >>

Hi,

I can't help you fix yours but I found a great little very reasonably priced and flexible and effective commercial utility that has a 2 stage Trailing Stop Loss that you can use either on its own or in conjunction with other AEs (it overrides the TSL in other AEs). It is from PipBoxer.Com: look for PBTS. Being a commercial product it is only available in compiled format of course.

Good luck with it.

…………………(8 >) Prosperous Trading (< 8)

DougRH4x

.

PS: I’m looking for someone to splice an adjustable (Trailing &) Stop Loss into the Martingale based program: PipMaker. Overcoming this one downfall of this program will make it VERY profitable!