//+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ extern int expertId = 1; extern int TakeProfit=300; extern int StopLoss=70; extern int TrailingStop = 0; extern double Lots = 0.1; extern int HMA_Period=20; extern int slippage=2; //slippage for market order processing extern int shift=1; //shift to current bar, extern int OrderTriesNumber=2; //to repeate sending orders when got some error extern string EAName="hma"; bool buysig,sellsig,closebuy,closesell; int lastsig,tries,x,y,co; double spr,hh,ll; void start() { //---- check for history and trading if(Bars<100 || IsTradeAllowed()==false) return; CheckForSignals(); co=CalculateCurrentOrders(); if (co>0) CheckForClose(); co=CalculateCurrentOrders(); CheckForOpen(); TrailStop(); } //+------------------------------------------------------------------+ //| Calculate open positions | //+------------------------------------------------------------------+ int CalculateCurrentOrders() { int ord; //---- for(int i=0;i10000) sellsig=true; if (hma11<10000 && hma12<10000 && hma13>10000 && !(hma21<10000 && hma22<10000)) buysig=true; closebuy=sellsig; closesell=buysig; } void CheckForOpen() { int res,tr; //---- sell conditions if(sellsig) { if (co==0 && lastsig!=Time[0]) { res = OpenAtMarket(OP_SELL,Lots); lastsig=Time[0]; } return; } //---- buy conditions if(buysig) { if (co==0 && lastsig!=Time[0]) { res = OpenAtMarket(OP_BUY,Lots); lastsig=Time[0]; } return; } } //+------------------------------------------------------------------+ //| Check for close order conditions | //+------------------------------------------------------------------+ void CheckForClose() { bool bres; int tr; for(int i=0;i 2 ) { for (int i = 0; i < OrdersTotal(); i++) { if ( OrderSelect (i, SELECT_BY_POS) == false ) continue; if ( OrderSymbol() != Symbol() || OrderMagicNumber() != expertId ) continue; if ( OrderType() == OP_BUY ) { if ( Bid < OrderOpenPrice()+TrailingStop*Point ) return; StopLoss = Bid-TrailingStop*Point; if ( StopLoss > OrderStopLoss() ) { bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White); if (!bres) Print("Error Modifying BUY order : ",ErrorDescription(GetLastError())); } } if ( OrderType() == OP_SELL ) { if ( Ask > OrderOpenPrice()-TrailingStop*Point ) return; StopLoss = Ask+TrailingStop*Point; if ( StopLoss < OrderStopLoss() ) { bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, Gold); if (!bres) Print("Error Modifying SELL order : ",ErrorDescription(GetLastError())); } } } } return; }