Self Documenting Template
Every EA has inputs, and every trader who has ever asked "what settings was this actually running with three weeks ago" knows the usual answer is guesswork from memory or a screenshot that may or may not still exist. This template solves that by writing its own documentation to disk: on every OnInit it produces a plain-text file naming the program and its version, describing the trading rule it implements in prose, and then listing every input by name with its current value on one line and a hand-written description of what that input controls on the next. MQL4 has no reflection or introspection API that can enumerate a program's own input parameters at runtime - unlike some other languages, there is no way to ask the compiled program "what inputs do you have and what are they currently set to" and get an automatic answer. The name/value/description table this template writes is therefore built by hand, one WriteInputLine() call per input, and it can drift out of sync with the actual input declarations if a future edit adds, removes, or renames an input without updating the matching line in WriteSelfDocumentation(). This is a genuine limitation of the platform, not a shortcut taken here, and is worth knowing before relying on this pattern for anything beyond documentation. To give the self-documentation something concrete to describe, the EA implements a real fast/slow moving-average cross rule, evaluated once per completed bar so it cannot repaint. It ships with InpEnableTrading=false: with trading disabled, every detected cross is logged and shown on-chart but no order is ever sent, so the template is safe to attach and observe before anyone decides to enable live orders. Notes and limitations - This strategy can lose money once InpEnableTrading is set to true. Defaults ship with trading disabled; test on a demo account first. - The self-documentation table in WriteSelfDocumentation() is hand written and not generated from the actual input declarations - MQL4 has no runtime reflection API for its own inputs. If an input is added, removed, or renamed without a matching edit there, the emitted doc file will describe the wrong set of settings. Keep both in sync by hand when adapting this template. - Only one position at a time is managed, filtered by this EA's own magic number and the chart's symbol; it does not see or manage orders placed by any other EA or manually by the trader. - The cross rule is evaluated on the two most recently CLOSED bars (shift 1 and 2), never the forming bar, so it does not repaint - but this also means a signal is only ever acted on once per new bar, not intrabar. - The doc file is plain text with no locking; running two instances with the same InpDocFileName on the same terminal will have each overwrite the other's file on its own OnInit.
Expert advisor · MT5 · v1.0 · Free
About Self Documenting Template
Self Documenting Template is an expert advisor (trading robot) for MetaTrader 5 based on Moving Average. It moves the stop loss behind price (trailing stop / break-even). It has 11 settings, such as Fast ma period, Slow ma period and Ma method. MQL source code included.
Files in the download: SelfDocumentingTemplate.ex5, SelfDocumentingTemplate.mq5, SelfDocumentingTemplate.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 |
|---|---|---|
| Fast ma period | 10 | Fast moving average period |
| Slow ma period | 30 | Slow moving average period (must exceed fast) |
| Ma method | MODE_SMA | Moving average method applied to both lines |
| Ma price | PRICE_CLOSE | Price series the moving averages use |
| Lots | 0.01 | Fixed lot size sent with every order |
| Stop loss pips | 50 | Stop-loss distance in pips (0 = no stop) |
| Take profit pips | 100 | Take-profit distance in pips (0 = no target) |
| Magic number | 20260817 | Magic number tagging every order this EA sends |
| Enable trading | false | false = signals are logged only, no orders sent |
| Doc file name | SelfDocumentingTemplate_doc.txt | Self-documentation output file |
| Rewrite doc on every init | true | true = refresh the doc file on every attach/recompile |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 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: Trailing Stop & Break-even · Moving Average · MT5 Expert Advisor · MQL5 Source Code