//+------------------------------------------------------------------+ //| Test.mq4 | //| Chronos | //| | //+------------------------------------------------------------------+ #property copyright "Chronos" #property link "" #define MAGICMA 20050610 extern double Lots = 0.1; extern double MaximumRisk = 0.02; extern double DecreaseFactor = 3; extern double MovingPeriod = 30; extern double MovingShift = 0; extern double MinDerivation = 0.0002; extern double MinimumDistance = 0.04; int MathSign(double Value) { if(Value > 0) return(1); else if(Value < 0) return(-1); else return(0); } void Check() { double MovingAverage; int res; static double LastMovingAverage; static double LastDerivation; double Derivation; static bool LastChangeDirection = false; static int SameDirectionCount = 0; static double LastChangeMovingAverage; // Go trading only for first tiks of new bar if(Volume[0] > 1) return; // Get Moving Average MovingAverage = iMA(NULL, 0, MovingPeriod, MovingShift, MODE_SMA, PRICE_CLOSE, 0); Derivation = MovingAverage - LastMovingAverage; Print("Derivation: " + Derivation); Comment("SameDirectionCount: " + SameDirectionCount); if(MathSign(LastDerivation) == MathSign(Derivation)) SameDirectionCount++; else { LastChangeDirection = true; SameDirectionCount = 0; LastChangeMovingAverage = MovingAverage; } // Sell conditions 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_BUY) { if(LastChangeDirection) OrderClose(OrderTicket(), OrderLots(), Bid, 3, White); } if(OrderType() == OP_SELL) { if(LastChangeDirection) OrderClose(OrderTicket(), OrderLots(), Ask, 3, White); } } // Order conditions //if(LastChangeDirection && (SameDirectionCount > 2)) if(LastChangeDirection && (MathAbs(MovingAverage - LastChangeMovingAverage) > MinimumDistance)) { if(MathSign(Derivation) == -1) res = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, 0, 0, "", MAGICMA, 0, Red); if(MathSign(Derivation) == 1) res = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, 0, 0, "", MAGICMA, 0, Blue); LastChangeDirection = false; } LastMovingAverage = MovingAverage; LastDerivation = Derivation; } int init() { return(0); } int deinit() { return(0); } int start() { if((Bars < 100) || (IsTradeAllowed() == false)) return; Check(); return(0); }