Download MetaTrader 5
To add comments, please log in or register
Register at MQL5.community and leave your comments!
coiler
220
coiler 2014.02.22 10:57 

Hi all,

i am working on some advance stop loss functions, could you give me some ideas about how to add a time variable in the stop loss functions, i mean, the initial stop loss (risk) shouldn't be the same at the first bar (opening trade bar) that in the bar numer ten. Could you give me the way to go here?. We used to update the SL, if there is a move in our favor (classic trailing), but what about the time in the market where price goes straight to our initial SL, or sideways. Examples what i am trying to implement would be:

- Updating the SL a % of the initial ATR each bar closed.

Could you help me with some ideas?.

(sorry about my english).

Best.

whroeder1
13644
whroeder1 2014.02.22 12:49  
coiler: some ideas about how to add a time variable in the stop loss functions, ... SL a % of the initial ATR each bar closed.
perhaps ATR Ratchet By Chuck LeBeau (see attached):
extern double  TSL.ATR                    =  1.1;
extern int     TSL.Pips                   =  3;
extern double  TSL.ATR.Ratchet               =  0.017;
extern int     SL.ATR.Length              = 57;
:
      double   duration = iBarShift(NULL,0, OrderOpenTime()),
            ratchet     = TSL.ATR - TSL.ATR.Ratchet * duration;
            LL       = MaximalPrice(TSL.Bars, 0, -DIR);
      double   SLnew    = LL -DIR* (ratchet * atr + TSL.Pips * pips2dbl)
                     + bid.to.stop,
      if((SLnew - OrderOpenPrice())*DIR >= 0.)  SL = SLnew;
   }
coiler
220
coiler 2014.02.22 18:18  
Thank you @WHRoeder
coiler
220
coiler 2014.02.24 08:53  

Hi @WHRoeder (or another colleague),

i've spent several hours triying to implement a ratchet update function following the text you send to me, please, could you tell me if my ratchet implementation it's ready for production? The flow and ratchet fuction will be correct?. (LONG side only).

I really appreciate any help you can provide.

'-- Parameters

extern int     magicNumber       = 12345678; 
extern int     ATR_period        = 20;
extern double  ATR_factor_profit = 1.5
extern double  ATR_percentage    = 0.05;
extern int     nBarsBackLowest   = 10;

'-- Global Variables
bool ratchetTriggered = false;

int init()
  {
  }

int deinit()
 {    
 }

int start()
 {    
  
   //-- here start logic to open position
   //-- end logic to open position
    
   if( NoOpenPositionsExist(ORDER_COMMENT) )
      ratchetTriggered = false;
   else // if position exits update ratchet
      double ATRCurrentDist   = iATR(Symbol(),PERIOD_D1,ATR_period,1)
      double ATRDistToTrigger = ATRCurrentDist * ATR_factor_profit;
      ATRRatchetTrail(ATRCurrentDist, ATRDistToTrigger, magicNumber, strComment);
 }
 

bool ATRRatchetTrail(double ATRCurrentDist, double ATRDistToTrigger, int magic, string strComment) {   

   double fromOpenPriceDiff, SL_now;
              
   for(int i = 0; i < OrdersTotal(); i++)
   {
   OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if (OrderMagicNumber() == magic && OrderSymbol() == Symbol() && OrderComment() == strComment)
      { 
         RefreshRates();          
         
         if( OrderType() == OP_BUY ) {
                   
            fromOpenPriceDiff = NormalizeDouble(Bid - OrderOpenPrice(), Digits));
            
            if ( fromOpenPriceDiff > ATRDistToTrigger ){
               ratchetTriggered = true;
            }  
                         
            if (ratchetTriggered){                
 
               // Racthet price SL calculation
               int barsSinceEntry      = iBarShift(NULL,0, OrderOpenTime());
               double lowestNBarsBack  = Low[iLowest(NULL, Period(), MODE_LOW, nBarsBackLowest, 1)];                          
               double ratchet          = (ATRCurrentDist * ATRPercetage * barsSinceEntry) + lowestNBarsBack);
               
               SL_now = ratchet;
               
               if (SL_now > NormalizeDouble(OrderStopLoss(), Digits)){
                  if( OrderModify( OrderTicket(), OrderOpenPrice(), SL_now, OrderTakeProfit(), 0, Blue ) ){
                     return(true);
                  }
                  else{
                     Print("error: Ratchet SL not updated.")
                     return(false);
                  }                 
               }//SL_now       
                                   
            }// if ratchetTriggered  
         }//LONG
   }//for
}
whroeder1
13644
whroeder1 2014.02.24 14:35  

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Treat each order separately?
    //          if ( fromOpenPriceDiff > ATRDistToTrigger ){
    //             ratchetTriggered = true;
    //          }  
     bool       ratchetTriggered = ( fromOpenPriceDiff > ATRDistToTrigger );
                if (ratchetTriggered){                
    

  3. //          ratchet     = TSL.ATR - TSL.ATR.Ratchet * duration;
    // double   SLnew    = LL -DIR* (ratchet * atr + TSL.Pips * pips2dbl) 
    double ratchet          = (ATRCurrentDist * ATRPercetage * barsSinceEntry) + lowestNBarsBack);
    For a buy SL is below the LL and you are reducing atr, not increasing it.
coiler
220
coiler 2014.02.24 22:38  
WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Treat each order separately?
  3. For a buy SL is below the LL and you are reducing atr, not increasing it.


Thank you so much @WHRoeder,

what do you mean with the second point? Treat each order separately?.

Best.

whroeder1
13644
whroeder1 2014.02.25 02:04  
coiler: what do you mean with the second point? Treat each order separately?.
Your ratchetTriggered = true; means once that is triggered it is true FOR ALL OPEN ORDERS. Should each SL be dependent on another?
coiler
220
coiler 2014.02.25 15:07  
WHRoeder:
Your ratchetTriggered = true; means once that is triggered it is true FOR ALL OPEN ORDERS. Should each SL be dependent on another?


Oh, i understand what you mean now, i think, the expert opens one position and will not open the next one until the first position has closed, is this what you mean?. If i just open one position at time, i would not not have any problem, wouldn't i?.

Thanks again.

coiler
220
coiler 2014.02.25 18:23  
coiler:


Oh, i understand what you mean now, i think, the expert opens one position and will not open the next one until the first position has closed, is this what you mean?. If i just open one position at time, i would not not have any problem, wouldn't i?.

Thanks again.


@WHRoeder, is this what you mean?, i don't know if i understand you well, because of my poor english.
/
To add comments, please log in or register