| 1 | //+------------------------------------------------------------------+
|
|---|
| 2 | //| Test.mq4 |
|
|---|
| 3 | //| Chronos |
|
|---|
| 4 | //| |
|
|---|
| 5 | //+------------------------------------------------------------------+
|
|---|
| 6 | #property copyright "Chronos"
|
|---|
| 7 | #property link ""
|
|---|
| 8 |
|
|---|
| 9 | #define MAGICMA 20050610
|
|---|
| 10 |
|
|---|
| 11 | extern double Lots = 0.1;
|
|---|
| 12 | extern double MaximumRisk = 0.02;
|
|---|
| 13 | extern double DecreaseFactor = 3;
|
|---|
| 14 | extern double MovingPeriod = 30;
|
|---|
| 15 | extern double MovingShift = 0;
|
|---|
| 16 | extern double MinDerivation = 0.0002;
|
|---|
| 17 | extern double MinimumDistance = 0.04;
|
|---|
| 18 |
|
|---|
| 19 | int MathSign(double Value)
|
|---|
| 20 | {
|
|---|
| 21 | if(Value > 0) return(1);
|
|---|
| 22 | else if(Value < 0) return(-1);
|
|---|
| 23 | else return(0);
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | void Check()
|
|---|
| 27 | {
|
|---|
| 28 | double MovingAverage;
|
|---|
| 29 | int res;
|
|---|
| 30 | static double LastMovingAverage;
|
|---|
| 31 | static double LastDerivation;
|
|---|
| 32 | double Derivation;
|
|---|
| 33 | static bool LastChangeDirection = false;
|
|---|
| 34 | static int SameDirectionCount = 0;
|
|---|
| 35 | static double LastChangeMovingAverage;
|
|---|
| 36 |
|
|---|
| 37 | // Go trading only for first tiks of new bar
|
|---|
| 38 | if(Volume[0] > 1) return;
|
|---|
| 39 |
|
|---|
| 40 | // Get Moving Average
|
|---|
| 41 | MovingAverage = iMA(NULL, 0, MovingPeriod, MovingShift, MODE_SMA, PRICE_CLOSE, 0);
|
|---|
| 42 | Derivation = MovingAverage - LastMovingAverage;
|
|---|
| 43 | Print("Derivation: " + Derivation);
|
|---|
| 44 | Comment("SameDirectionCount: " + SameDirectionCount);
|
|---|
| 45 | if(MathSign(LastDerivation) == MathSign(Derivation)) SameDirectionCount++;
|
|---|
| 46 | else {
|
|---|
| 47 | LastChangeDirection = true;
|
|---|
| 48 | SameDirectionCount = 0;
|
|---|
| 49 | LastChangeMovingAverage = MovingAverage;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | // Sell conditions
|
|---|
| 53 | for(int i=0; i < OrdersTotal(); i++)
|
|---|
| 54 | {
|
|---|
| 55 | if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) break;
|
|---|
| 56 | if((OrderMagicNumber() != MAGICMA) || (OrderSymbol() != Symbol())) continue;
|
|---|
| 57 |
|
|---|
| 58 | if(OrderType() == OP_BUY)
|
|---|
| 59 | {
|
|---|
| 60 | if(LastChangeDirection)
|
|---|
| 61 | OrderClose(OrderTicket(), OrderLots(), Bid, 3, White);
|
|---|
| 62 | }
|
|---|
| 63 | if(OrderType() == OP_SELL)
|
|---|
| 64 | {
|
|---|
| 65 | if(LastChangeDirection)
|
|---|
| 66 | OrderClose(OrderTicket(), OrderLots(), Ask, 3, White);
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | // Order conditions
|
|---|
| 71 | //if(LastChangeDirection && (SameDirectionCount > 2))
|
|---|
| 72 | if(LastChangeDirection && (MathAbs(MovingAverage - LastChangeMovingAverage) > MinimumDistance))
|
|---|
| 73 | {
|
|---|
| 74 | if(MathSign(Derivation) == -1) res = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, 0, 0, "", MAGICMA, 0, Red);
|
|---|
| 75 | if(MathSign(Derivation) == 1) res = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, 0, 0, "", MAGICMA, 0, Blue);
|
|---|
| 76 | LastChangeDirection = false;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | LastMovingAverage = MovingAverage;
|
|---|
| 80 | LastDerivation = Derivation;
|
|---|
| 81 | }
|
|---|
| 82 | int init()
|
|---|
| 83 | {
|
|---|
| 84 | return(0);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | int deinit()
|
|---|
| 88 | {
|
|---|
| 89 | return(0);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | int start()
|
|---|
| 93 | {
|
|---|
| 94 | if((Bars < 100) || (IsTradeAllowed() == false)) return;
|
|---|
| 95 | Check();
|
|---|
| 96 | return(0);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|