Reverse Position Keeping Risk Constant
Flipping a position by hand usually means closing it and opening the same lot size the other way, which is wrong the moment the new stop distance differs from the old one. A 30 pip stop on 1.0 lots and a 60 pip stop on 1.0 lots do not risk the same money, even though the lot size looks unchanged. This script solves for the lot size instead of copying it, so the reversed position risks exactly what the original position was risking, at whatever new stop distance you choose. The money-at-risk figure is computed from the symbol's own tick value and tick size, never an assumed contract size - the same arithmetic that keeps a lot size calculator correct on FX, metals and indices alike, where a point of movement is worth a different amount of money per lot. A position with no stop loss has no risk figure to preserve, so the script refuses outright rather than guessing one; set a stop first if the position does not have one. The sequence is close-then-reopen, the same as any manual reversal, and the same honest gap exists between those two moments: there is briefly no position at all. If the solved lot size falls outside the symbol's tradable range, it is clamped to fit and the resulting risk mismatch is stated plainly in the summary, rather than silently reported as an exact match it is not. If the reopen fails after the original position is already closed, the script says so as loudly as it can, because at that point the account has no position on the symbol at all until you act. Notes and limitations - The original position must carry a stop loss. There is no fallback risk figure for a naked position - the script refuses rather than inventing one. - The lot size is solved and normalised to the symbol's lot step, then clamped to the symbol's minimum/maximum if the solved value falls outside it. A clamp means the new risk no longer exactly matches the old one; the summary states this plainly whenever it happens. - There is genuinely no position open between the close and the reopen - this is not an atomic operation on this platform. If the reopen fails, the account is flat on that symbol until you act. - Commission, where the broker charges one, is not included in the risk figures. - Works on market positions only. Pending orders are rejected outright.
Script · MT4 · v1.0 · Free
About Reverse Position Keeping Risk Constant
Reverse Position Keeping Risk Constant is an expert advisor (trading robot) for MetaTrader 4. It has a news filter. It has 7 settings, such as Ticket, New stop pips and New target pips. MQL source code included.
Files in the download: ReverseKeepingRisk.mq4, ReverseKeepingRisk.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 |
|---|---|---|
| Ticket | 0 | Ticket number of the position to reverse |
| New stop pips | 30.0 | Stop distance for the new, reversed position, pips |
| New target pips | 0.0 | Target distance for the new position, pips (0 = no target) |
| Magic number | 21000 | Magic number tagged on the new ticket |
| Slippage points | 30 | Maximum acceptable slippage, points |
| Max retries | 5 | Attempts per trade action 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 → 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: Risk & Lot Size · News Trading · MT4 Script · MQL4 Source Code