Weekend Gap Shield
A stop-loss protects against price moving through it while the market is open. It cannot protect against a weekend gap, because there is no trading between Friday's close and Monday's open to fill an order at any price in between - the position simply reopens wherever the market reopens. Every symbol gaps by a different amount on a normal weekend, and that amount is measurable: this reads each held symbol's own weekly bars over a configurable lookback and averages the absolute size of every recorded Friday-close-to-Monday-open jump. Rather than trim every position by one flat percentage, the trim fraction is derived per symbol from the ratio of its measured average gap to its own daily ATR. A symbol whose weekend gaps are small next to its normal daily movement is left alone; one whose gaps are large relative to its own volatility is trimmed harder, up to a configurable ceiling. This avoids the common mistake of treating every symbol's weekend risk as identical when it plainly is not. The trim executes once per calendar week, at a configured time on Friday, and is de-duplicated against a stored week number so a restart or a slow scan cannot fire it twice in the same week. It ships in dry-run mode by default, printing and alerting exactly what it would have trimmed without sending any order, so the sizing can be reviewed before it is allowed to touch real positions. Notes and limitations - Trims are partial closes only - this never adds a hedge position, and it never fully closes a position outright (a trim that would round up to the full position size is skipped rather than sent). - The measured gap is an average over the lookback window, not a worst case - an exceptional gap larger than anything in the recorded history is not protected against, only the typical pattern is. - If a symbol has fewer weekly bars than the configured lookback, or its daily ATR cannot be computed, that symbol is left untouched for that pass rather than trimmed on an unreliable estimate. - The de-duplication watermark is per terminal (a GlobalVariable), so running two attached copies on different charts in the same terminal correctly trims only once per week between them; a second, separate terminal instance has its own watermark.
Expert advisor · MT4 · v1.0 · Free
About Weekend Gap Shield
Weekend Gap Shield is a script for MetaTrader 4 based on ATR. It can alert you with a pop-up, sound, email or push message. It has 12 settings, such as Dry run, Magic filter and Gap lookback weeks. MQL source code included.
Files in the download: WeekendGapShield.ex4, WeekendGapShield.mq4, WeekendGapShield.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Dry run | true | true = report only, never actually trims a position |
| Magic filter | 0 | Magic number to protect; 0 = every magic number |
| Gap lookback weeks | 26 | Weekly bars scanned to measure each symbol's historical Monday gap |
| Gap sensitivity | 1.0 | Multiplier on the measured gap-to-ATR ratio; higher trims harder |
| Max trim percent | 50.0 | Hard ceiling on how much of any one position can be trimmed |
| Min gap atr ratio | 0.15 | Symbols whose average gap is below this fraction of their ATR are left alone |
| Friday trim hour | 20 | Server hour on Friday the trim executes (broker time) |
| Friday trim minute | 0 | Server minute on Friday the trim executes |
| Atr period | 14 | ATR period (daily) used as the volatility yardstick for the gap ratio |
| Scan interval sec | 60 | Seconds between checks for the Friday trim window |
| Max retries | 5 | Attempts per partial close before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
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 → Scripts.
- 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 script onto a chart to run it once.
Tags: Alerts · ATR · MT4 Expert Advisor · MQL4 Source Code