Stop Pair At Range Extremes
Before a breakout you want orders waiting at both edges of the range: a buy stop above the high, a sell stop below the low, whichever way it goes you are in. Reading the extremes off the chart by eye and typing them into two tickets is slow, and the two mistakes the naive version of this script makes are worth naming. The first is scanning the forming bar. Bar zero's high and low are still moving, so a range that includes it changes between the survey and the send, and can even make the script's own order price invalid. This script scans completed bars only - bar 1 backwards - and refuses to run if the chart has fewer bars loaded than asked, rather than quietly building a smaller range than the one you wanted. The second is sizing the orders by feel while calling the setup "risk managed". Here each order's stop loss sits at the opposite trigger - the classic range-reversal placement - which makes the stop distance identical on both sides (the whole range plus both buffers), and each side's volume is computed so that distance loses the asked percentage of equity. The conversion runs through tick value arithmetic, not Point arithmetic, which mis-sizes metals and indices by exactly the tick-to-point ratio. Volumes are floored to the lot step, never rounded up. And the honesty note, stated in the confirmation prompt rather than buried here: THESE ARE TWO INDEPENDENT ORDERS. MetaTrader has no server-side one-cancels-the-other, so filling one side does not cancel the opposite order. A breakout that reverses and crosses the whole range fills both. Each side can lose its full risk; the worst case over the pair is twice the per-order figure. Scripts that claim otherwise are lying about what the platform can do - after one side triggers, deleting the survivor is your job or an EA's, not a dead script's. A side that price has already broken through is refused with the numbers printed - the breakout it was waiting for has already happened - and the other side is still placed. Notes and limitations - No OCO. Nothing cancels the surviving order when the other fills - delete it yourself or manage it with an EA. This is a platform fact, not a script choice. - Take profits are not placed. Where a breakout runs to is your trade management, not a range statistic; each order carries only its stop. - The range is the chart timeframe's bars. The same N on a different timeframe is a different range. - Prices are surveyed once, at run time. New bars after the run do not move the orders. - The risk figure assumes the stop fills at its price; a gap through the range loses more. - Both orders are GTC and survive restarts until filled or deleted - if the setup goes stale, remember to delete them. - A range narrower than the broker's minimum stop distance cannot be traded this way at all and is refused with both numbers.
Script · MT5 · v1.0 · Free
About Stop Pair At Range Extremes
Stop Pair At Range Extremes is an expert advisor (trading robot) for MetaTrader 5. It places pending buy/sell stop or limit orders and can size trades from your balance and risk percent. It has 9 settings, such as Range bars, Buffer pips and Sizing mode. MQL source code included.
Files in the download: RangeExtremeStops.ex5, RangeExtremeStops.mq5, RangeExtremeStops.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 |
|---|---|---|
| Range bars | 20 | Completed bars to scan (bar 1 backwards) |
| Buffer pips | 5 | Buffer beyond each extreme, pips |
| Sizing mode | SIZE_RISK_PERCENT | How each order is sized |
| Risk percent | 1.0 | Risk per order if its stop is hit, % of equity |
| Lots | 0.10 | Volume per order (fixed-lots mode) |
| Magic | 0 | Magic number written on the orders |
| Max retries | 5 | Attempts per order before giving up |
| Retry wait ms | 500 | Pause between attempts, milliseconds |
| Confirm first | true | Ask for confirmation before placing |
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: Breakout · Pending Orders · Risk & Lot Size · MT5 Script · MQL5 Source Code