Hi
I've been testing this idea for the past couple of weeks where a long term moving average (in this case EMA288) is used as a filter to determine the direction and then enter when the fast EMA (EMA24) crossed over/under the slow EMA (EMA48).
Initial stop level is placed at the EMA288 of the time of order of which is not supposed to be trailed. But the order will be closed out when the EMA48 crosses over/under the longer term EMA288.
My problem is this that I have not explicitly coded a trailing stop, but I've noticed that every so often the stops in my orders will be modified as the EMA288 updates and moves in favour of the order's direction.
Here is the code and any help explaining why it's trailing or how best to tidy it up would be much appreciated
I've been testing this idea for the past couple of weeks where a long term moving average (in this case EMA288) is used as a filter to determine the direction and then enter when the fast EMA (EMA24) crossed over/under the slow EMA (EMA48).
Initial stop level is placed at the EMA288 of the time of order of which is not supposed to be trailed. But the order will be closed out when the EMA48 crosses over/under the longer term EMA288.
My problem is this that I have not explicitly coded a trailing stop, but I've noticed that every so often the stops in my orders will be modified as the EMA288 updates and moves in favour of the order's direction.
Here is the code and any help explaining why it's trailing or how best to tidy it up would be much appreciated
Inserted Code
#define MAGICMA 18689124 extern int Risk = 1000; //--- //Calculate Open Positions //--- int CalculateCurrentOrders(string symbol) { int buys=0, sells=0; for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA) { if(OrderType()==OP_BUY) buys++; if(OrderType()==OP_SELL) sells++; } } if(buys>0) return(buys); else return(-sells); } //--- //check for open order conditions //--- void CheckForOpen() { double EMA24Current, EMA24Previous, EMA48Current, EMA48Previous, EMA288; double LotsShort, LotsLong; int ticket; EMA24Current = iMA(Symbol(),0,24,0,MODE_EMA,PRICE_CLOSE,0); EMA24Previous = iMA(Symbol(),0,24,0,MODE_EMA,PRICE_CLOSE,1); EMA48Current = iMA(Symbol(),0,48,0,MODE_EMA,PRICE_CLOSE,0); EMA48Previous = iMA(Symbol(),0,48,0,MODE_EMA,PRICE_CLOSE,1); EMA288 = iMA(Symbol(),0,288,0,MODE_EMA,PRICE_CLOSE,0); LotsShort = MathRound(Risk/(((EMA288-Bid)/Point)*10)); LotsLong = MathRound(Risk/(((Ask-EMA288)/Point)*10)); //--sell condition if(Bid < EMA288 && EMA24Previous > EMA48Previous && EMA24Current <= EMA48Current) { ticket=OrderSend(Symbol(),OP_SELL,LotsShort,Bid,0,EMA288,0,NULL,MAGICMA,0,Red); return; } //--buy condition if(Ask > EMA288 && EMA24Previous < EMA48Previous && EMA24Current >=EMA48Current) { ticket=OrderSend(Symbol(),OP_BUY,LotsLong,Ask,0,EMA288,0,NULL,MAGICMA,0,Green); return; } } //--- //Check for stop/exit condition //--- void CloseCondition() { double EMA48Current, EMA48Previous, EMA288Current, EMA288Previous; EMA48Current = iMA(Symbol(),0,48,0,MODE_EMA,PRICE_CLOSE,0); EMA48Previous = iMA(Symbol(),0,48,0,MODE_EMA,PRICE_CLOSE,1); EMA288Current = iMA(Symbol(),0,288,0,MODE_EMA,PRICE_CLOSE,0); EMA288Previous = iMA(Symbol(),0,288,0,MODE_EMA,PRICE_CLOSE,1); for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue; if(OrderType()==OP_SELL) { if(EMA48Previous < EMA288Previous && EMA48Current >= EMA288Current) OrderClose(OrderTicket(),OrderLots(),Ask,0,Yellow); break; } if(OrderType()==OP_BUY) { if(EMA48Previous > EMA288Previous && EMA48Current <= EMA288Current) OrderClose(OrderTicket(),OrderLots(),Bid,0,Yellow); break; } } } //--- //start function //--- void start() { if(IsTradeAllowed()==false) return; if(CalculateCurrentOrders(Symbol())==0) CheckForOpen(); else CloseCondition(); }