Hi Everyone. I need a bit of help please. I would like to code a simple EA that uses a trailingstop on losing trades once a certain price level is hit. For example, I enter a buy trade with no stoploss. Once the price moves 50 pips against me, I would like to modify the order and set a trailing stoploss. If the price keeps on deteriorating (moving against me) the stoploss gets hit, but if the price improves a bit the stoploss is moved up. I got this far but it is not consistent. What am I doing wrong here, please.
Inserted Code
//+------------------------------------------------------------------+ //| Stoploss | //+------------------------------------------------------------------+ void Stop_Loss(){ double TSTP=TrailingStart*Pip,Trail=TrailingStop*Pip; //TrailingStart and TrailingStop are extern global variables double PriceB=0; for(int i=OrdersTotal()-1; i>=0; i--){ if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){ if(OrderSymbol()==Symbol()){ if(OrderType()==OP_BUY){ if(OrderOpenPrice()-Bid>=TSTP){ PriceB=Bid; //This is the once off trigger. Once price falls by 50 pips, activate the Stoploss } if(Bid>PriceB){ if(OrderStopLoss()<Bid-Trail){ //Then trail the stoploss in case price improves if(OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Trail,OrderTakeProfit(),0,Orange)) RefreshRates(); } } } } } } }