RiskGuard Toolkit Library
- ライブラリ
- バージョン: 1.1
- アクティベーション: 5
| RiskGuard Toolkit Library for MQL5 developers - version 1.01 |
|---|
|
Stop rewriting the same risk code in every Expert Advisor
Every serious EA needs the same boring but critical pieces: a lot size that matches the risk you intend, a stop that respects volatility, a spread filter, a session window, a daily loss limit and a pre-trade check that the order will actually be accepted. Writing these again for each project wastes hours, and small mistakes (a lot not normalised to the volume step, a stop inside the stops level, a forgotten margin check) are exactly what makes Market validation fail or live accounts misbehave.
RiskGuard Toolkit is a compact, compiled MQL5 library that gives you all of it in 16 tested functions. Import it once and call what you need - from a single lot calculation to a full one-line "can I trade now?" gate that explains why when the answer is no.
What you get
Position sizing. RG_LotsByRiskPercent and RG_LotsByRiskMoney return a lot size that loses exactly the risk you choose if the stop is hit, with the symbol's minimum lot as a floor. Pass 0 as the stop distance and a sensible volatility-based stop is used automatically. RG_NormalizeLots rounds any lot to the broker's step and clamps it to the allowed range. RG_MoneyPerLot tells you what one full lot loses over any price distance, with a fallback for symbols whose tick value is reported as zero.
Volatility tools. RG_ATR returns an ATR value on any symbol, timeframe and period safely (0 when history is missing, never an array error). RG_ATRStopPrice gives you a ready stop price for a buy or sell from an entry price, timeframe, period and multiplier.
Market filters. RG_SpreadPoints and RG_SpreadOK handle spread checks; RG_InSession supports trading windows, including overnight windows that wrap past midnight.
Validation-grade order checks. RG_StopsValid checks SL/TP against the stops level, RG_MarginOK compares the required margin with free margin, and RG_OrderLimitOK respects the account order limit and the symbol volume limit - the same checks MQL5 Market validators look for.
Daily protection. RG_TodayClosedProfit sums today's closed result including swap and commission, optionally filtered by magic number; RG_DailyLossOK tells you whether the daily loss limit has been reached.
One-call gate. RG_CanTrade runs spread, session, daily loss, stops, margin and order-limit checks in one call and fills a reason string such as "OK" or the exact cause of refusal, ideal for logging and on-chart status.
Housekeeping. RG_Version returns the library version so your EA can log it.
How to use it
1. After purchase, copy RiskGuardLib.ex5 into your terminal's MQL5\Libraries folder.
2. In your EA or script, declare the functions you need inside an #import "RiskGuardLib.ex5" ... #import block (the signatures are listed below).
3. Call them like any other function, for example: lots = RG_LotsByRiskPercent(_Symbol, 1.0, 0); then if(RG_CanTrade(_Symbol, true, lots, sl, tp, 30, 7, 20, 3.0, magic, reason)) send your order, otherwise Print(reason).
4. Compile your EA. That is all - no DLL, no file access, no extra resources.
Function signatures
string RG_Version()
double RG_NormalizeLots(string symbol, double lots)
double RG_MoneyPerLot(string symbol, double priceDistance)
double RG_LotsByRiskPercent(string symbol, double riskPercent, double slDistance)
double RG_LotsByRiskMoney(string symbol, double riskMoney, double slDistance)
double RG_ATR(string symbol, ENUM_TIMEFRAMES tf, int period, int shift)
double RG_ATRStopPrice(string symbol, bool isBuy, double entry, ENUM_TIMEFRAMES tf, int period, double mult)
int RG_SpreadPoints(string symbol)
bool RG_SpreadOK(string symbol, int maxPoints)
bool RG_InSession(int startHour, int endHour)
bool RG_StopsValid(string symbol, bool isBuy, double price, double sl, double tp)
bool RG_MarginOK(string symbol, bool isBuy, double lots)
bool RG_OrderLimitOK(string symbol, double lots)
double RG_TodayClosedProfit(long magic)
bool RG_DailyLossOK(double maxLossPercent, long magic)
bool RG_CanTrade(string symbol, bool isBuy, double lots, double sl, double tp, int maxSpread, int startHour, int endHour, double maxDailyLossPct, long magic, string &reason)
Parameter notes
riskPercent - percent of equity to risk per trade. riskMoney - fixed account-currency amount to risk. slDistance - stop distance in price units; 0 uses an automatic volatility stop. maxPoints / maxSpread - maximum spread in points, 0 disables the filter. startHour / endHour - server-time window; equal values mean always open. maxLossPercent / maxDailyLossPct - daily loss limit as a percent of balance, 0 disables it. magic - magic number filter, -1 counts every trade. reason - receives "OK" or the cause of refusal.
The screenshots show a small demonstration script built on the library that prints the computed stop, lot size and gate result on the chart without sending any order. Its settings are: risk percent (1.0), ATR timeframe (H1), ATR period (14), ATR stop multiplier (1.5), maximum spread (30 points), session start and end hour (0 / 24), maximum daily loss (3 percent) and magic filter (-1 = all).
Why developers like it
Fewer validation rejections, cleaner code, consistent risk across all your EAs, and a single place to improve when your rules change. Works with any symbol, any account currency and both hedging and netting accounts. Lightweight: no per-tick heavy work, no global state you need to manage.
Build safer robots faster - add RiskGuard to your toolbox today, and write to me if you want help wiring it into your project.
