//+------------------------------------------------------------------+ //| Test.mq4 | //| Chronos | //| | //+------------------------------------------------------------------+ #property copyright "Chronos" #property link "" #define MAGICMA 1000000 extern double Lots = 0.1; extern double ShortMovingPeriod = 10; extern double ShortMovingShift = 0; extern double LongMovingPeriod = 60; extern double LongMovingShift = 0; extern double BaseTrailingStop = 100; extern double TrailingStopPart = 0.6; extern int MinBarsBetweenOrders = 10; /* int MathSign(double Value) { if(Value > 0) return(1); else if(Value < 0) return(-1); else return(0); } */ void Check() { double LongMovingAverage; double ShortMovingAverage; int Polarity; int res; static double LastMovingAverage; static double LastDerivation; double Derivation; static bool PolarityChanged = false; static int SameDirectionCount = 0; static int LastPolarity; static bool FirstStart = true; double TrailingStop; static datetime TimeLastOrder; // Go trading only for first tiks of new bar if(Volume[0] > 1) return; // Get Moving Average ShortMovingAverage = iMA(NULL, 0, ShortMovingPeriod, ShortMovingShift, MODE_SMA, PRICE_MEDIAN, 0); LongMovingAverage = iMA(NULL, 0, LongMovingPeriod, LongMovingShift, MODE_SMA, PRICE_MEDIAN, 0); if(ShortMovingAverage > LongMovingAverage) Polarity = 1; else Polarity = -1; if((LastPolarity != Polarity) && (FirstStart == false)) PolarityChanged = true; /* Derivation = MovingAverage - LastMovingAverage; Print("Derivation: " + Derivation); Comment("SameDirectionCount: " + SameDirectionCount); if(MathSign(LastDerivation) == MathSign(Derivation)) SameDirectionCount++; else { LastChangeDirection = true; SameDirectionCount = 0; LastChangeMovingAverage = MovingAverage; } */ RefreshRates(); // 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) { TrailingStop = (Bid - OrderOpenPrice()) * TrailingStopPart + BaseTrailingStop * Point; Print("TrailingStop: " + TrailingStop + ", OpenPrice: " + OrderOpenPrice() + ", Bid: " + Bid); if(Bid > OrderStopLoss() + TrailingStop) { OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop, OrderTakeProfit(), 0, Green); } //if(PolarityChanged && (Polarity == -1)) // OrderClose(OrderTicket(), OrderLots(), Bid, 3, White); } if(OrderType() == OP_SELL) { TrailingStop = (OrderOpenPrice() - Ask) * TrailingStopPart + BaseTrailingStop * Point; if(Ask < OrderStopLoss() - TrailingStop) { OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TrailingStop, OrderTakeProfit(), 0, Green); } //if(PolarityChanged && (Polarity == 1)) // OrderClose(OrderTicket(), OrderLots(), Ask, 3, White); } } // Order conditions //if(LastChangeDirection && (SameDirectionCount > 2)) if(PolarityChanged && ((TimeCurrent() - TimeLastOrder) > MinBarsBetweenOrders * (Time[0] - Time[1]))) { if(Polarity == -1) res = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, Bid + BaseTrailingStop * Point, 0, "", MAGICMA, 0, Red); if(Polarity == 1) res = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Ask - BaseTrailingStop * Point, 0, "", MAGICMA, 0, Blue); TimeLastOrder = TimeCurrent(); } PolarityChanged = false; //LastShortMovingAverage = ShortMovingAverage; //LastLongMovingAverage = LongMovingAverage; LastPolarity = Polarity; FirstStart = false; } int init() { return(0); } int deinit() { return(0); } int start() { if((Bars < 100) || (IsTradeAllowed() == false)) return; Check(); return(0); }