Time Sliced Risk Budgeter
A single account-wide maximum-loss limit answers "can I still trade at all", but says nothing about pacing. A trader who burns through an entire week's worth of allowed risk in one bad Monday still shows plenty of budget left by an account-wide measure, right up until the moment they do not - by which point the damage for that period is already done and invisible to a limit that only looks at the running total. This library slices one total loss budget into fixed calendar periods - daily via TsrbInitDailyBudgets(), weekly (Monday-anchored) via TsrbInitWeeklyBudgets() - splitting the total evenly across them up front. TsrbRecordLoss() charges a realised loss to whichever period actually contains it by timestamp, so each loss only ever touches its own bucket rather than requiring a full-history recompute. TsrbBurnDownPct() and TsrbRemainingBudget() answer "how much of THIS period's allowance is left" at any point, and TsrbIsBudgetBreached() gives a simple yes/no for gating further trading once a period's own slice is exhausted - independent of whether the account-wide total has anything left. Notes and limitations - Budgets are split EVENLY across periods by these init functions; a caller wanting uneven allocation (e.g. a smaller Friday slice) can overwrite individual budgets[i].budgetAmount after initialising. - TsrbRecordLoss() and TsrbRemainingBudget() return -1 / false for a timestamp outside every configured period - this is deliberately NOT treated as zero loss or as "not breached"; an out-of-range date means the schedule was never extended to cover it, and the caller should treat that as a configuration gap, not a clean bill of health. - Weekly buckets are Monday-anchored using a fixed epoch-offset calculation (Unix epoch, 1970-01-01, was a Thursday) rather than TimeDayOfWeek(), so the same calculation is reproducible without reading server time at all. - This library only tracks consumption against a pre-set allocation - it does not itself close trades, disable AutoTrading, or enforce anything; TsrbIsBudgetBreached() is a signal for the caller's own EA logic to act on.
Script · MT4 · v1.0 · Free
About Time Sliced Risk Budgeter
Time Sliced Risk Budgeter is a script for MetaTrader 4. MQL source code included.
Files in the download: TimeSlicedRiskBudgeter.mqh, TimeSlicedRiskBudgeter.txt, TimeSlicedRiskBudgeterDemo.mq4.
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: MT4 Script · MQL4 Source Code