Download MetaTrader 5
To add comments, please log in or register
No time for trading? Subscribe to signals!
orangutangas1
54
orangutangas1 2014.10.09 06:54 

Hello,

I  have just created EA and want to incorporate trailing stop. EA will be launched  for several pairs which have different trailing stop levels. How it is the best way to write that trailing stop script into ea which will operate differently for each pair differently?

Is it  good way to do it like this(example):

extern double           TrailingStop =   90;
extern bool       BreakEven             = true;
extern int        BreakEvenPips         = 40;
 
extern int        TrailingStep          = 1;   
extern bool       ProfitTrailing        = true;
extern bool       UseSound              = true;  
extern string     NameFileSound         = "cork_pop.wav";  


int start() {


//my EA created 


//trailing stop in EA
       for (int H=0; H<OrdersTotal(); H++) 
{ if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 
{ if (OrderSymbol()==Symbol()) 
 {
 
 double pBid, pAsk, pp;

  RefreshRates();
  pp = MarketInfo(OrderSymbol(), MODE_POINT);
  double NewStop = 0 ;
  if (OrderType()==OP_BUY)
  {
    pBid = MarketInfo(OrderSymbol(), MODE_BID);
    if (BreakEven == true && OrderOpenPrice() + BreakEvenPips * pp < pBid && (OrderStopLoss() < OrderOpenPrice() || OrderStopLoss()==0)) NewStop = OrderOpenPrice() ;
    if (ProfitTrailing == true && pBid - OrderOpenPrice() > TrailingStop * pp && (OrderStopLoss() < pBid - (TrailingStop + TrailingStep -1)*pp || OrderStopLoss()==0)) NewStop = pBid - TrailingStop * pp ;
    if(NewStop != 0)
    {   
        ModifyStopLoss(NewStop);
        return;
    }
  }

  if (OrderType()==OP_SELL) {
    pAsk = MarketInfo(OrderSymbol(), MODE_ASK);
    if (BreakEven == true && OrderOpenPrice() - BreakEvenPips * pp > pAsk && (OrderStopLoss() > OrderOpenPrice() || OrderStopLoss()==0)) NewStop = OrderOpenPrice() ;
    if (ProfitTrailing == true && OrderOpenPrice() - pAsk > TrailingStop * pp && (OrderStopLoss() > pAsk + (TrailingStop + TrailingStep -1) * pp || OrderStopLoss()==0)) NewStop = pAsk + TrailingStop * pp ;
    if(NewStop != 0)
    {   
        ModifyStopLoss(NewStop);
        return;
    }
  }

}
}
} 
   
   
   return(0);
  }


void ModifyStopLoss(double ldStopLoss) {
  bool fm;

  fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);
  if (fm && UseSound) PlaySound(NameFileSound);
  if (!fm)
  {
     int err;
     err=GetLastError();
     Print("Wheelbarrel trailing error (",err,")");
     return(0);
  }

}
/
To add comments, please log in or register