| 1 | //+------------------------------------------------------------------+
|
|---|
| 2 | //| Test.mq4 |
|
|---|
| 3 | //| Chronos |
|
|---|
| 4 | //| |
|
|---|
| 5 | //+------------------------------------------------------------------+
|
|---|
| 6 | #property copyright "Chronos"
|
|---|
| 7 | #property link ""
|
|---|
| 8 |
|
|---|
| 9 | #define MAGICMA 1000000
|
|---|
| 10 |
|
|---|
| 11 | extern double Lots = 0.1;
|
|---|
| 12 | extern double ShortMovingPeriod = 10;
|
|---|
| 13 | extern double ShortMovingShift = 0;
|
|---|
| 14 | extern double LongMovingPeriod = 60;
|
|---|
| 15 | extern double LongMovingShift = 0;
|
|---|
| 16 | extern double BaseTrailingStop = 100;
|
|---|
| 17 | extern double TrailingStopPart = 0.6;
|
|---|
| 18 | extern int MinBarsBetweenOrders = 10;
|
|---|
| 19 |
|
|---|
| 20 | /*
|
|---|
| 21 | int MathSign(double Value)
|
|---|
| 22 | {
|
|---|
| 23 | if(Value > 0) return(1);
|
|---|
| 24 | else if(Value < 0) return(-1);
|
|---|
| 25 | else return(0);
|
|---|
| 26 | }
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | void Check()
|
|---|
| 30 | {
|
|---|
| 31 | double LongMovingAverage;
|
|---|
| 32 | double ShortMovingAverage;
|
|---|
| 33 | int Polarity;
|
|---|
| 34 | int res;
|
|---|
| 35 | static double LastMovingAverage;
|
|---|
| 36 | static double LastDerivation;
|
|---|
| 37 | double Derivation;
|
|---|
| 38 | static bool PolarityChanged = false;
|
|---|
| 39 | static int SameDirectionCount = 0;
|
|---|
| 40 | static int LastPolarity;
|
|---|
| 41 | static bool FirstStart = true;
|
|---|
| 42 | double TrailingStop;
|
|---|
| 43 | static datetime TimeLastOrder;
|
|---|
| 44 |
|
|---|
| 45 | // Go trading only for first tiks of new bar
|
|---|
| 46 | if(Volume[0] > 1) return;
|
|---|
| 47 |
|
|---|
| 48 | // Get Moving Average
|
|---|
| 49 | ShortMovingAverage = iMA(NULL, 0, ShortMovingPeriod, ShortMovingShift, MODE_SMA, PRICE_MEDIAN, 0);
|
|---|
| 50 | LongMovingAverage = iMA(NULL, 0, LongMovingPeriod, LongMovingShift, MODE_SMA, PRICE_MEDIAN, 0);
|
|---|
| 51 | if(ShortMovingAverage > LongMovingAverage) Polarity = 1; else Polarity = -1;
|
|---|
| 52 | if((LastPolarity != Polarity) && (FirstStart == false)) PolarityChanged = true;
|
|---|
| 53 |
|
|---|
| 54 | /*
|
|---|
| 55 | Derivation = MovingAverage - LastMovingAverage;
|
|---|
| 56 | Print("Derivation: " + Derivation);
|
|---|
| 57 | Comment("SameDirectionCount: " + SameDirectionCount);
|
|---|
| 58 | if(MathSign(LastDerivation) == MathSign(Derivation)) SameDirectionCount++;
|
|---|
| 59 | else {
|
|---|
| 60 | LastChangeDirection = true;
|
|---|
| 61 | SameDirectionCount = 0;
|
|---|
| 62 | LastChangeMovingAverage = MovingAverage;
|
|---|
| 63 | }
|
|---|
| 64 | */
|
|---|
| 65 | RefreshRates();
|
|---|
| 66 |
|
|---|
| 67 | // Sell conditions
|
|---|
| 68 | for(int i=0; i < OrdersTotal(); i++)
|
|---|
| 69 | {
|
|---|
| 70 | if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) break;
|
|---|
| 71 | if((OrderMagicNumber() != MAGICMA) || (OrderSymbol() != Symbol())) continue;
|
|---|
| 72 |
|
|---|
| 73 | if(OrderType() == OP_BUY)
|
|---|
| 74 | {
|
|---|
| 75 | TrailingStop = (Bid - OrderOpenPrice()) * TrailingStopPart + BaseTrailingStop * Point;
|
|---|
| 76 | Print("TrailingStop: " + TrailingStop + ", OpenPrice: " + OrderOpenPrice() + ", Bid: " + Bid);
|
|---|
| 77 | if(Bid > OrderStopLoss() + TrailingStop)
|
|---|
| 78 | {
|
|---|
| 79 | OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop, OrderTakeProfit(), 0, Green);
|
|---|
| 80 | }
|
|---|
| 81 | //if(PolarityChanged && (Polarity == -1))
|
|---|
| 82 | // OrderClose(OrderTicket(), OrderLots(), Bid, 3, White);
|
|---|
| 83 | }
|
|---|
| 84 | if(OrderType() == OP_SELL)
|
|---|
| 85 | {
|
|---|
| 86 | TrailingStop = (OrderOpenPrice() - Ask) * TrailingStopPart + BaseTrailingStop * Point;
|
|---|
| 87 | if(Ask < OrderStopLoss() - TrailingStop)
|
|---|
| 88 | {
|
|---|
| 89 | OrderModify(OrderTicket(), OrderOpenPrice(), Ask + TrailingStop, OrderTakeProfit(), 0, Green);
|
|---|
| 90 | }
|
|---|
| 91 | //if(PolarityChanged && (Polarity == 1))
|
|---|
| 92 | // OrderClose(OrderTicket(), OrderLots(), Ask, 3, White);
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | // Order conditions
|
|---|
| 97 | //if(LastChangeDirection && (SameDirectionCount > 2))
|
|---|
| 98 | if(PolarityChanged && ((TimeCurrent() - TimeLastOrder) > MinBarsBetweenOrders * (Time[0] - Time[1])))
|
|---|
| 99 | {
|
|---|
| 100 | if(Polarity == -1) res = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, Bid + BaseTrailingStop * Point, 0, "", MAGICMA, 0, Red);
|
|---|
| 101 | if(Polarity == 1) res = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Ask - BaseTrailingStop * Point, 0, "", MAGICMA, 0, Blue);
|
|---|
| 102 | TimeLastOrder = TimeCurrent();
|
|---|
| 103 | }
|
|---|
| 104 | PolarityChanged = false;
|
|---|
| 105 |
|
|---|
| 106 | //LastShortMovingAverage = ShortMovingAverage;
|
|---|
| 107 | //LastLongMovingAverage = LongMovingAverage;
|
|---|
| 108 | LastPolarity = Polarity;
|
|---|
| 109 | FirstStart = false;
|
|---|
| 110 | }
|
|---|
| 111 | int init()
|
|---|
| 112 | {
|
|---|
| 113 | return(0);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | int deinit()
|
|---|
| 117 | {
|
|---|
| 118 | return(0);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | int start()
|
|---|
| 122 | {
|
|---|
| 123 | if((Bars < 100) || (IsTradeAllowed() == false)) return;
|
|---|
| 124 | Check();
|
|---|
| 125 | return(0);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|