Results 1 to 2 of 2
 0 Attachment(s)    

Thread: Trailing stop function

  1. #1
    Senior Member fxtester's Avatar
    Join Date
    Nov 2008
    Posts
    3,152

    Default Trailing stop function

    Trailing stop and breakeven :

    Code:
    extern double TrailingStop     = 3.5; 
    extern int TrailingStep        = 1;
    
    extern double BreakEvenPips    = 3.5;
    Code:
    double   point=0.0001;
    double   minlots=0.1;
    double   maxlots=0.1;
    double   lotstep=0.1;
    int dg=0;
    Code:
       if(Digits<=3)dg=2;else dg=4;
       if(Digits<=3)point=0.01;
       minlots=MarketInfo(Symbol(),MODE_MINLOT);
       maxlots=MarketInfo(Symbol(),MODE_MAXLOT);
       lotstep=MarketInfo(Symbol(),MODE_LOTSTEP);
       mindistance=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point+point;
    Code:
       if(TrailingStop>0 || BreakEvenPips>0)trailingpositions();
    Code:
    //----------------------------------------------------- modify order
    
    void trailingpositions(){
       double pBid,pAsk,pp;
    
       RefreshRates();
       pp=point;
       double newstop=0;
       for(int p=0;p<OrdersTotal();p++){
          if(OrderSelect(p,SELECT_BY_POS,MODE_TRADES))
             if(OrderSymbol()==Symbol())
             if(OrderMagicNumber()==Magic){
                if(OrderType()==OP_BUY){
                   pBid=MarketInfo(OrderSymbol(),MODE_BID);
                   if(BreakEvenPips>0 && OrderOpenPrice()+BreakEvenPips*pp<pBid && (OrderStopLoss()<OrderOpenPrice() || OrderStopLoss()==0))newstop=OrderOpenPrice() ;
                   if(TrailingStop>0 && pBid-OrderOpenPrice()>TrailingStop*pp && (OrderStopLoss()<pBid-(TrailingStop+TrailingStep-1)*pp || OrderStopLoss()==0))newstop=pBid-TrailingStop*pp ;
                   if(newstop!=0){   
                      //Print(Symbol()+" ; "+"DIFF= "+DoubleToStr(pBid-OrderOpenPrice(),dg)+" TP= "+DoubleToStr(TrailingStop*pp,dg));
                      modifystoploss(newstop);
                      return;
                   }
                }
    
                if(OrderType()==OP_SELL){
                   pAsk=MarketInfo(OrderSymbol(), MODE_ASK);
                   if(BreakEvenPips>0 && OrderOpenPrice()-BreakEvenPips*pp>pAsk && (OrderStopLoss()>OrderOpenPrice() || OrderStopLoss()==0))newstop=OrderOpenPrice();
                   if (TrailingStop>0 && OrderOpenPrice()-pAsk>TrailingStop*pp && (OrderStopLoss()>pAsk+(TrailingStop+TrailingStep -1)*pp || OrderStopLoss()==0))newstop=pAsk+TrailingStop*pp ;
                   if(newstop!=0){   
                      //Print(Symbol()+" ; "+"DIFF= "+DoubleToStr(OrderOpenPrice()-pAsk,dg)+" TP= "+DoubleToStr(TrailingStop*pp,dg));
                      modifystoploss(newstop);
                      return;
                   }
                }
          }
       }
    }
        
    void modifystoploss(double ldStopLoss){
       bool fm;
    
       fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);
       if(!fm){
          int err;
          err=GetLastError();
          Print("Wheelbarrel trailing error (",err,")");
          return;
       }
    }

  2. #2
    Junior Member
    Join Date
    Sep 2013
    Posts
    4

    Default

    Hi fxtester
    Thanks for adding trailing stop snippet,appreciations.
    can you take time to add extra coding skills regarding 5 digit broker auto adjust within this code,i means it work/select automatically for any of 4 digit/5 digit systems.
    hope fully take profit code will be included too ..... for to make it complete.
    regards

Similar Threads

  1. Trailing stop and Trailing profit
    By Luudt in forum MQL programming
    Replies: 0
    Last Post: 02-15-2016, 21:00
  2. Trailing stop
    By painofhell in forum Trading discussion
    Replies: 1
    Last Post: 11-07-2014, 17:43
  3. ADX EA with trailing stop
    By TMC in forum Ideas for expert advisors
    Replies: 0
    Last Post: 01-11-2012, 23:29
  4. Please Add Trailing Stop
    By micutzu in forum Ideas for expert advisors
    Replies: 2
    Last Post: 08-01-2010, 13:35
  5. Waddah Attar Win EA - with Trailing Stop & Stop Loss
    By dlhc in forum Scripts and other tools
    Replies: 0
    Last Post: 03-09-2010, 12:33

Tags for this Thread

breakeven, news, pips, stop, trailing, trailingstep, trailingstop

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •