Problem in trailing Stop function
You can try this :
//+------------------------------------------------------------------+ //| (My)Trailing Stop | //+------------------------------------------------------------------+ void TrailingStop() { for(int i=0; i<=OrdersTotal(); i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false){GetLastError();} { if(OrderSymbol()==Symbol() && OrderMagicNumber()==magic_no) { if(OrderType()==OP_BUY) { if((OrderClosePrice()>=(OrderOpenPrice()+TrailingStop*Point*p2p) && (OrderStopLoss()<=OrderOpenPrice() || OrderStopLoss()==0))) { if(OrderModify(OrderTicket(),OrderOpenPrice(),(OrderClosePrice()-(TrailingStop-Profit_lock)*Point*p2p),OrderTakeProfit(),0,Red)==false)GetLastError(); } else if((OrderClosePrice()>=(OrderStopLoss()+TrailingStop*Point*p2p) && (OrderStopLoss()>=OrderOpenPrice()))) { if(OrderModify(OrderTicket(),OrderOpenPrice(),(OrderClosePrice()-TrailingStop*Point*p2p),OrderTakeProfit(),0,Red)==false)GetLastError(); } } if(OrderType()==OP_SELL) { if((OrderClosePrice()<=(OrderOpenPrice()-TrailingStop*Point*p2p) && (OrderStopLoss()>=OrderOpenPrice() || OrderStopLoss()==0))) { if(OrderModify(OrderTicket(),OrderOpenPrice(),(OrderClosePrice()+(TrailingStop-Profit_lock)*Point*p2p),OrderTakeProfit(),0,Red)==false)GetLastError(); } else if((OrderClosePrice()<=(OrderStopLoss()-TrailingStop*Point*p2p) && (OrderStopLoss()<=OrderOpenPrice()) && OrderStopLoss() > 0 )) { if(OrderModify(OrderTicket(),OrderOpenPrice(),(OrderClosePrice()+TrailingStop*Point*p2p),OrderTakeProfit(),0,Red)==false)GetLastError(); } } } } } }
Forum on trading, automated trading systems and testing trading strategies
What is wrong in my code? Getting error 138
Alain Verleyen, 2018.11.12 16:35
mql4 topic in mql4 section of the forum please.
can anyone tell me what is wrong in my trailing function it works with Buy only !!!!!