Locked Position Resolver
Locking a losing position - opening the opposite side instead of accepting the loss - feels like it stops the bleeding, but it is not free. Both legs of the lock keep paying swap every night the position stays open, on top of whatever the eventual unwind will cost. Most traders who lock a position never actually total up what it is costing them to keep it locked versus what it would cost to just close it and move on. This script does that arithmetic directly. It surveys the buy and sell baskets open on a symbol, identifies the locked volume as the smaller of the two sides, and computes two figures: the cost of resolving the locked portion right now at today's market, and the projected cost of continuing to carry it for a chosen number of days, based on the double swap the locked lots are paying on both legs at once. Whichever figure is smaller is the cheaper path on the numbers as they stand today - the script states the comparison plainly rather than making the decision. With InpConfirmUnlock left at its default of false, the script only reports the comparison. Setting it to true closes the locked portion of both baskets, largest tickets first, partial-closing the last ticket on each side as needed, and leaves only the genuine directional excess (if any) open. Notes and limitations - The daily lock cost treats MarketInfo's MODE_SWAPLONG/MODE_SWAPSHORT as points per lot per day, the common MT4 default, and converts to account currency via the symbol's own tick value - never a hardcoded figure. A broker that reports swap directly in money rather than points would need this conversion adjusted; check a known open position's actual overnight swap charge against this script's per-lot figure once before relying on the projection. - "Locked volume" is the smaller of the buy and sell basket's total lots. Any excess on the larger side is genuine directional exposure, reported separately, and is never touched by the unlock action. - The resolve-now cost is each basket's current floating P&L, scaled by the fraction of that basket which is part of the lock - it assumes money-per-point-per-lot stays constant between each basket's average entry and the current price, the same assumption this catalogue's other basket-level tools make. - Unwinding closes tickets largest-first per side to minimise the number of partial closes needed; it does not try to match specific buy tickets against specific sell tickets by age or price. - Anything that closes real positions can realise a loss. Review the comparison before setting InpConfirmUnlock to true.
Script · MT5 · v1.0 · Free
About Locked Position Resolver
Locked Position Resolver is an expert advisor (trading robot) for MetaTrader 5. It has 5 settings, such as Symbol, Magic filter and Projection days. MQL source code included.
Files in the download: LockedPositionResolver.ex5, LockedPositionResolver.mq5, LockedPositionResolver.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 |
|---|---|---|
| Symbol | Symbol to analyse ("" = this chart's symbol) | |
| Magic filter | -1 | Magic number filter (-1 = every magic) |
| Projection days | 7 | Days to project the carrying cost over |
| Confirm unlock | false | Actually close the locked portion (false = report only) |
| Unlock slippage | 30 | Allowed slippage in points when unwinding |
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: Hedging · Volume · MT5 Script · MQL5 Source Code