├── .gitignore ├── README.md └── Experts └── Orchard └── GridTrader v1 ├── GridTrader v1.mq4 ├── GridTrader v1.mq5 ├── Expert.mqh ├── GridTrader v1.mqh └── Leg.mqh /.gitignore: -------------------------------------------------------------------------------- 1 | mqlcache.dat 2 | *.ex4 3 | *.ex5 4 | *.lnk 5 | 6 | /Frameworks 7 | /Include/Orchard/Frameworks 8 | /*.bat 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 220130_grid_trading_expert 2 | 3 | This is the code for the expert described in the youtube video https://youtu.be/mtnXe4YGk1g 4 | 5 | You will need the framework 3 to use this code. The framework is evolving independently so it is kept in a separate repo at https://github.com/OrchardForexTutorials/framework_3 6 | -------------------------------------------------------------------------------- /Experts/Orchard/GridTrader v1/GridTrader v1.mq4: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | GridTrader v1.mq4 4 | Copyright 2022, Orchard Forex 5 | https://www.orchardforex.com 6 | 7 | */ 8 | 9 | #property copyright "Copyright 2022, Orchard Forex" 10 | #property link "https://www.orchardforex.com" 11 | #property version "1.00" 12 | #property strict 13 | 14 | #include "GridTrader v1.mqh" 15 | 16 | -------------------------------------------------------------------------------- /Experts/Orchard/GridTrader v1/GridTrader v1.mq5: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | GridTrader v1.mq5 4 | Copyright 2022, Orchard Forex 5 | https://www.orchardforex.com 6 | 7 | */ 8 | 9 | #property copyright "Copyright 2022, Orchard Forex" 10 | #property link "https://www.orchardforex.com" 11 | #property version "1.00" 12 | #property strict 13 | 14 | #include "GridTrader v1.mqh" 15 | 16 | -------------------------------------------------------------------------------- /Experts/Orchard/GridTrader v1/Expert.mqh: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | GridTrader v1 4 | Expert 5 | 6 | Copyright 2022, Orchard Forex 7 | https://www.orchardforex.com 8 | 9 | */ 10 | 11 | #include 12 | #include "Leg.mqh" 13 | 14 | class CExpert : public CExpertBase { 15 | 16 | private: 17 | 18 | CLeg* mBuyLeg; 19 | CLeg* mSellLeg; 20 | 21 | protected: 22 | 23 | double mLevelSize; 24 | 25 | void Loop(); 26 | 27 | public: 28 | 29 | CExpert( int levelPoints, 30 | double orderSize, string tradeComment, long magic); 31 | ~CExpert(); 32 | 33 | }; 34 | 35 | 36 | CExpert::CExpert(int levelPoints, double orderSize, string tradeComment, long magic) 37 | : CExpertBase(orderSize, tradeComment, magic) { 38 | 39 | mLevelSize = PointsToDouble(levelPoints); 40 | 41 | mBuyLeg = new CLeg(mLevelSize, POSITION_TYPE_BUY, mOrderSize, mTradeComment, mMagic); 42 | mSellLeg = new CLeg(mLevelSize, POSITION_TYPE_SELL, mOrderSize, mTradeComment, mMagic); 43 | 44 | mInitResult = INIT_SUCCEEDED; 45 | 46 | } 47 | 48 | CExpert::~CExpert() { 49 | 50 | delete mBuyLeg; 51 | delete mSellLeg; 52 | 53 | } 54 | 55 | void CExpert::Loop() { 56 | 57 | mBuyLeg.OnTick(); 58 | mSellLeg.OnTick(); 59 | 60 | } 61 | 62 | -------------------------------------------------------------------------------- /Experts/Orchard/GridTrader v1/GridTrader v1.mqh: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | GridTrader v1.mqh 4 | Copyright 2022, Orchard Forex 5 | https://www.orchardforex.com 6 | 7 | */ 8 | 9 | #property copyright "Copyright 2022, Orchard Forex" 10 | #property link "https://www.orchardforex.com" 11 | #property version "1.00" 12 | #property strict 13 | 14 | #define FRAMEWORK_VERSION_3_01 15 | #define FRAMEWORK_VERSION 16 | #include 17 | 18 | // 19 | // Inputs 20 | // 21 | 22 | // V1 grid trading is simple, we just need spacing between trades 23 | // and lot sizes 24 | input int InpLevelPoints = 0; // Trade gap in points 25 | 26 | // Now some general trading info 27 | input double InpOrderSize = 0.00; // Order size 28 | input string InpTradeComment = "Grid Trader V1"; // Trade comment 29 | input int InpMagic = 222222; // Magic number 30 | 31 | #include "Expert.mqh" 32 | CExpert* Expert; 33 | 34 | int OnInit() { 35 | 36 | Expert = new CExpert( InpLevelPoints, 37 | InpOrderSize, InpTradeComment, InpMagic); 38 | 39 | return(Expert.InitResult()); 40 | 41 | } 42 | 43 | void OnDeinit(const int reason) { 44 | 45 | delete Expert; 46 | 47 | } 48 | 49 | void OnTick() { 50 | 51 | Expert.OnTick(); 52 | return; 53 | 54 | } 55 | 56 | -------------------------------------------------------------------------------- /Experts/Orchard/GridTrader v1/Leg.mqh: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | GridTrader v1 4 | Expert 5 | 6 | Copyright 2022, Orchard Forex 7 | https://www.orchardforex.com 8 | 9 | */ 10 | 11 | #include 12 | 13 | class CLeg : public CLegBase { 14 | 15 | private: 16 | 17 | double mLevelSize; 18 | 19 | int mCount; 20 | double mEntry; 21 | double mExit; 22 | 23 | void CloseAll(double price); 24 | void OpenTrade(double price); 25 | void Recount(); 26 | void Loop(); 27 | 28 | public: 29 | 30 | CLeg( double levelSize, 31 | ENUM_POSITION_TYPE legType, 32 | double orderSize, string tradeComment, long magic); 33 | 34 | }; 35 | 36 | CLeg::CLeg(double levelSize, 37 | ENUM_POSITION_TYPE legType, 38 | double orderSize, string tradeComment, long magic) 39 | : CLegBase(legType, orderSize, tradeComment, magic) { 40 | 41 | mLevelSize = levelSize; 42 | 43 | Recount(); 44 | 45 | } 46 | 47 | void CLeg::Loop() { 48 | 49 | // First process the closing rules 50 | // On the first run there may be no trades but there is no harm 51 | if (mLegType==POSITION_TYPE_BUY && mLastTick.bid>=mExit) { 52 | CloseAll(mLastTick.bid); 53 | } else 54 | if (mLegType==POSITION_TYPE_SELL && mLastTick.ask<=mExit) { 55 | CloseAll(mLastTick.ask); 56 | } 57 | 58 | // Finally the new trade entries 59 | if (mLegType==POSITION_TYPE_BUY) { 60 | if (mCount==0 || mLastTick.ask<=mEntry) { 61 | OpenTrade(mLastTick.ask); 62 | } 63 | } else { 64 | if (mCount==0 || mLastTick.bid>=mEntry) { 65 | OpenTrade(mLastTick.bid); 66 | } 67 | } 68 | 69 | } 70 | 71 | void CLeg::CloseAll(double price) { 72 | 73 | for (int i=PositionInfo.Total()-1; i>=0; i--) { 74 | 75 | if (!PositionInfo.SelectByIndex(i)) continue; 76 | if (PositionInfo.Symbol()!=mSymbol || PositionInfo.Magic()!=mMagic || PositionInfo.PositionType()!=mLegType) continue; 77 | 78 | int ticket = (int)PositionInfo.Ticket(); 79 | 80 | if (PositionInfo.PositionType()==POSITION_TYPE_BUY && (price-mLevelSize)>=PositionInfo.PriceOpen()) { 81 | Trade.PositionClose(ticket); 82 | continue; 83 | } 84 | 85 | if (PositionInfo.PositionType()==POSITION_TYPE_SELL && (price+mLevelSize)<=PositionInfo.PriceOpen()) { 86 | Trade.PositionClose(ticket); 87 | continue; 88 | } 89 | 90 | } 91 | Recount(); 92 | 93 | } 94 | 95 | void CLeg::OpenTrade(double price) { 96 | 97 | Trade.PositionOpen(mSymbol, (ENUM_ORDER_TYPE)mLegType, mOrderSize, price, 0, 0, mTradeComment); 98 | Recount(); 99 | 100 | } 101 | 102 | 103 | /* 104 | * Recount() 105 | * 106 | * Mainly for restarts 107 | * Scans currently open trades and rebuilds the position 108 | */ 109 | void CLeg::Recount() { 110 | 111 | mCount = 0; 112 | mEntry = 0; 113 | mExit = 0; 114 | 115 | double high = 0; 116 | double low = 0; 117 | 118 | double lead = 0; 119 | double trail = 0; 120 | 121 | for (int i=PositionInfo.Total()-1; i>=0; i--) { 122 | 123 | if (!PositionInfo.SelectByIndex(i)) continue; 124 | 125 | if (PositionInfo.Symbol()!=mSymbol || PositionInfo.Magic()!=mMagic || PositionInfo.PositionType()!=mLegType) continue; 126 | 127 | mCount++; 128 | if (high==0 || PositionInfo.PriceOpen()>high) high = PositionInfo.PriceOpen(); 129 | if (low==0 || PositionInfo.PriceOpen()0) { 134 | if (mLegType==POSITION_TYPE_BUY) { 135 | mEntry = low-mLevelSize; 136 | mExit = low+mLevelSize; 137 | } else { 138 | mEntry = high+mLevelSize; 139 | mExit = high-mLevelSize; 140 | } 141 | } 142 | 143 | } 144 | 145 | 146 | --------------------------------------------------------------------------------