Loading…

Risk Unit Ledger

Raw currency P/L makes a 3-lot scalp and a 0.1-lot swing position look incomparable even when both were sized to risk the same fraction of the account. This library expresses every closed trade's result as an R-multiple instead - profit divided by the dollar risk that trade opened with - so a trend follower's expectancy, win rate and best/worst outcomes can be read on one consistent scale regardless of instrument, lot size or stop distance. The naive version of this divides profit by whatever the stop happens to be at close time, or crashes/returns garbage on a trade that never had a stop at all. This library keeps every entry's initial risk figure separate from its close-time state, and explicitly marks a trade as having "no defined R" rather than dividing by zero or a near-zero stop distance, when the risk it opened with cannot be established. An account-history pass is included, but it is honestly limited: MQL4's OrderStopLoss() reports whatever the stop was last set to, which is already a trailed or breakeven-moved stop on any trade whose stop was adjusted after entry - not necessarily the true original risk. The synthetic-ledger path in the demo sidesteps this entirely by taking the initial risk as an explicit input, which is the accurate way to use this library from inside an EA that knows its own true entry risk at open time. Notes and limitations - The ledger has a fixed capacity of 500 entries (RISK_UNIT_LEDGER_MAX_ENTRIES). RiskUnitLedgerRecord() returns false once full rather than overflowing - check the return value if the ledger might be filled programmatically over a long period. - A trade with no stop-loss, or a stop equal to the entry price, has no defined R. It is counted separately (RiskUnitLedgerUndefinedCount) and excluded from every average/expectancy/win-rate figure rather than being silently treated as zero risk. - RiskUnitLedgerRecordFromSelectedOrder() uses OrderStopLoss(), which is the CURRENT stop at close time, not necessarily the stop the trade actually opened with. A trailed or breakeven-adjusted stop makes the recovered R inaccurate for that trade. For an accurate ledger, record the true initial risk yourself at open time using RiskUnitLedgerComputeInitialRisk() and RiskUnitLedgerRecord(), the way a live EA should. - RiskUnitLedgerComputeInitialRisk() reads MODE_TICKVALUE/MODE_TICKSIZE from the broker at call time - it does not cache or hardcode any instrument's tick value, since that varies by symbol and by broker. - This library does not touch the order pool itself beyond reading fields off an already-selected order; it performs no trading action.

Script · MT4 · v1.0 · Free

About Risk Unit Ledger

Risk Unit Ledger is a script for MetaTrader 4. It shows several symbols in one panel. It has 1 setting, such as Max history to scan. MQL source code included.

Files in the download: RiskUnitLedger.mqh, RiskUnitLedger.txt, RiskUnitLedgerDemo.mq4.

Settings

SettingDefaultWhat it does
Max history to scan100how many recent closed orders to scan from real history

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Scalping · Trend · Risk & Lot Size · Dashboards & Scanners · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria