Grid Template EA
RISK WARNING: a pending-order grid can accumulate exposure across several filled levels in a strongly trending market. This template has no lot multiplier at all - every level uses the same InpLots size - because a multiplier is what turns a grid into a martingale system, which is a separate and more dangerous piece of money management covered by its own demonstration EA elsewhere in this catalogue. Do not add compounding to this template without understanding that trade-off. This is a template, not a finished trading system, and it says so honestly rather than dressing up a skeleton as a strategy. It has no view on when a grid should be deployed: as shipped, it deploys once on OnInit around the price the chart was attached at, purely so the mechanics are visible the moment it is dragged onto a chart. Two points in the source are marked TEMPLATE POINT - one in OnInit where the deployment trigger belongs, one in the empty OnTick where a per-tick entry condition belongs - and are the intended starting points for building something real on top of this. What is included and working: a symmetric buy-stop/sell-stop grid around price, a hard InpMaxTotalLots ceiling that deployment refuses to exceed, and a basket stop-loss that flattens every position and pending order under this magic number the moment floating loss reaches InpBasketStopLossPercent of account balance. These three pieces are the minimum any grid system needs regardless of what triggers it, which is why they are the part this template provides rather than leaving as an exercise. Notes and limitations - Deploys once, unconditionally, on attach. There is no re-entry condition and no auto-refill of levels that fill or expire - both are deliberately left for the builder to add at the marked TEMPLATE POINTs, not included as a hidden default. - The basket stop-loss check runs on a timer, not every tick - a very fast move between scans could exceed the cap briefly before it fires. Each level also carries its own per-level stop-loss for that reason. - Requires AutoTrading enabled; on MQL4 that is checked via IsTradeAllowed(), on MQL5 via TERMINAL_TRADE_ALLOWED plus ACCOUNT_TRADE_ALLOWED and ACCOUNT_TRADE_EXPERT. If trading is disabled the EA logs it and neither deploys nor protects the grid. - Ships with a small default lot size and a low basket stop-loss percentage on purpose - raise both deliberately, not by habit. - MQL5 version only: requires a hedging account. It will not deploy at all on a netting account - see "MQL4 VERSUS MQL5 DIFFERENCES" above.
Expert advisor · MT4 · v1.0 · Free
About Grid Template EA
Grid Template EA is an expert advisor (trading robot) for MetaTrader 4. It opens a grid of orders at fixed price steps, places pending buy/sell stop or limit orders and can size trades from your balance and risk percent. It has 13 settings, such as Lots, Grid levels and Grid step pips. MQL source code included.
Files in the download: GridTemplateEa.ex4, GridTemplateEa.mq4, GridTemplateEa.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Lots | 0.01 | Lot size for every level - flat by design, no compounding |
| Grid levels | 3 | Number of levels on EACH side (buy stops above, sell stops below) |
| Grid step pips | 20 | Spacing between levels, in pips |
| Stop loss pips | 50 | Stop-loss per level, in pips from that level's entry |
| Take profit pips | 30 | Take-profit per level, in pips from that level's entry (0 = none) |
| Max total lots | 0.10 | Hard cap on combined lots across every level - deployment stops here |
| Use basket stop loss | true | Flatten the whole grid if floating loss breaches the cap below |
| Basket stop loss percent | 5.0 | Emergency close-all trigger, % of account balance in floating loss |
| Magic number | 20260821 | Magic number - scopes every calculation and action to this grid only |
| Slippage pips | 5 | Slippage tolerance on any market action (basket stop-loss close) |
| Max retries | 5 | Attempts per order action before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
| Scan interval sec | 20 | Seconds between maintenance scans |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Experts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
- Test it first on a demo account or in the Strategy Tester.
Tags: Grid · Martingale · Trend · Risk & Lot Size · Pending Orders · MT4 Expert Advisor · MQL4 Source Code