风险管理主类构建:从结构体到核心变量的系统骨架(基础篇)
(1/3)· 多数 EA 的风险模块散落各处,改一处崩全盘,本文用类把亏损利润跟踪锁进统一骨架
◍ 把风控逻辑收进一个主类
在 MT5 的 EA 工程里,把风险管理拆成一个独立的主类,比散落在全局变量里更利于维护和回测。类内先定义常量、枚举与结构体,再声明关键变量(如单笔风险比例、账户净值、当前止损距离),由构造函数和初始化方法统一接管。 Niquel Mendoza 在 2026 年 2 月 13 日发布的示例工程中,该系列文章累计阅读量 428,说明这类结构化封装对实盘交易者确有参考价值。把损益赋值、手数计算、止损位推导都做成类方法后,新一天或新一周的规划事件也能在类内闭环处理。 高风险提示:外汇与贵金属杠杆交易可能迅速扩大亏损,任何风控类封装都只是概率约束,不保证避免爆仓。建议在策略测试器用历史数据跑一遍再上模拟盘。
先把风险管理类的骨架立起来
这一篇要落地的不是界面,而是风险管理系统真正的核心:一个能替亏损和利润赋值、并支撑后续跟踪计算的类。先把结构定清楚,后面才不会在内存和状态同步上反复返工。 整套设计拆成四步推进:先定义存储累计亏损、利润所需的结构体与枚举;再写主类 CRiskManagement 把风控计算和流程收拢到一起;接着补上赋值、取值以及构造/析构函数,保证数据初始化和释放不出错;最后挂上事件驱动函数,在新一天或新一周开局时重算利润、重置阶段累计量。 外汇与贵金属杠杆高、跳空频繁,这类按周期重置的累计逻辑若漏掉,会让周级回撤统计直接失真。按这个顺序从空类搭起,下一步才能接住具体的盈亏赋值实现。
「先把风险管理的骨架搭进 Risk_Management.mqh」
做模块化 EA 时,把风控逻辑单独塞进 Risk_Management.mqh 能少踩很多坑。类构造里用 NOT_MAGIC_NUMBER 当初始化值,等于告诉系统「不挑幻数,全账户通管」,实盘对整账户做风控时尤其省事。 平仓不想写一堆函数,就用标志位代替:FLAG_CLOSE_ALL_PROFIT=2 只平盈利单,FLAG_CLOSE_ALL_LOSS=4 只平亏损单,组合着传参比堆函数清爽。 亏损利润的计算方式分两派——固定金额(money)和百分比(percentage)。固定就是写死 1000 美元日限,不随账户变;百分比挂 Balance / equity 等参考值上,余额涨限额跟着涨。FTMO 这类自营账户日亏上限是浮动的:初始 20 万余额、日限 1 万,若当日已平仓亏 8000,浮亏再加仓瞬间触 -11700 就爆规则,哪怕后来扭盈也晚了。 手数也留了活口:Dinamico 走 GetLote 按风险算,Fijo 直接用变量 lote。动态手数再分两种——只按每笔风险,或按风险+止损校准。举例 1000 账户冒 1% 即 10 美元风险,EURUSD 100 点止损、1:2 盈亏比,不校准可能开出 0.02 手,止损真触发才亏 2 美元、止盈赚 4 美元,完全偏离预期;GetIdealLot 就是干校准这事,让给定止损下亏损恰为账户 1%,触 1:2 止盈即落袋 2%。 结构 Loss_Profit 把 value、assigned_percentage、mode_calculation_risk、percentage_applied_to 打包,日亏/总亏/单笔亏/周亏各方案靠它描述,重置节奏从每日清零到永不重置都有。
class="macro">#define NOT_MAGIC_NUMBER class="num">0 class=class="str">"cmt">//Not Magic Number class="macro">#define FLAG_CLOSE_ALL_PROFIT class="num">2 class=class="str">"cmt">//Flag indicating to close only operations with profit class="macro">#define FLAG_CLOSE_ALL_LOSS class="num">4 class=class="str">"cmt">//Flag indicating to close only operations without profit enum ENUM_RISK_CALCULATION_MODE class=class="str">"cmt">//enumeration to define the types of calculation of the value of maximum profits and losses { money, class=class="str">"cmt">//Money percentage class=class="str">"cmt">//Percentage % }; enum ENUM_APPLIED_PERCENTAGES class=class="str">"cmt">//enumeration to define the value to which the percentages will be applied { Balance, class=class="str">"cmt">//Balance ganancianeta,class=class="str">"cmt">//Net profit free_margin, class=class="str">"cmt">//Free margin equity class=class="str">"cmt">//Equity }; enum ENUM_MODE_RISK_MANAGEMENT class=class="str">"cmt">//enumeration to define the type of risk management { propfirm_ftmo, class=class="str">"cmt">//Prop Firm FTMO personal_account class=class="str">"cmt">// Personal Account }; enum ENUM_LOTE_TYPE class=class="str">"cmt">//lot type { Dinamico,class=class="str">"cmt">//Dynamic Fijoclass=class="str">"cmt">//Fixed }; trade.Sell( (Lote_Type == Dinamico ? risk.GetLote(ORDER_TYPE_SELL,GET_LOT_BY_ONLY_RISK_PER_OPERATION) : lote), _Symbol,tick.bid,sl,tp,"EA Sell"); class="type">void GetIdealLot(class="type">class="kw">double& nlot, class="type">class="kw">double glot, class="type">class="kw">double max_risk_per_operation, class="type">class="kw">double& new_risk_per_operation, class="type">long StopLoss) enum ENUM_GET_LOT { GET_LOT_BY_ONLY_RISK_PER_OPERATION, class=class="str">"cmt">//Obtain the lot for the risk per operation GET_LOT_BY_STOPLOSS_AND_RISK_PER_OPERATION class=class="str">"cmt">//Obtain and adjust the lot through the risk per operation and stop loss respectively. }; class="kw">struct Loss_Profit { class="type">class="kw">double value; class=class="str">"cmt">//value
◍ 风险计算结构体的字段拆解
在 MT5 的 EA 参数体系里,风险模块通常用结构体打包。下面这段声明直接决定了下单时仓位怎么算,开 MT5 新建 include 头文件就能复现。 double assigned_percentage 存的是实际要套用的百分比数值,注释写得很直白:percentage to apply,也就是真正参与仓位公式的那个系数。 ENUM_RISK_CALCULATION_MODE mode_calculation_risk 控制风险计算方式,是固定手数、净值百分比还是结余百分比,全看这个枚举怎么赋。 ENUM_APPLIED_PERCENTAGES percentage_applied_to 指明百分比作用对象,比如是落在保证金还是账户净值上,类型选错会导致仓位偏差可能超过 30%。
class="type">class="kw">double assigned_percentage; class=class="str">"cmt">//percentage to apply ENUM_RISK_CALCULATION_MODE mode_calculation_risk; class=class="str">"cmt">//risk calculation method ENUM_APPLIED_PERCENTAGES percentage_applied_to; class=class="str">"cmt">//percentage applied to };
风险引擎里的变量怎么摆
要在 MT5 里写一个能管仓位风险的类,先得把类内部状态捋清楚。这里直接定义 CRiskManagemet(final 类,不可继承),并在私有段把后续算手数、控亏损要用到的字段一次性声明好。 头部先 #include <Trade/Trade.mqh>,拿到 CTrade 指针 trade,后面平仓、查持仓都靠它。主变量里 account_profit 用 double 存账户净利,StopLoss 用 long 存止损点数,lote 用 double 存上一次算出的手数——这三个是普通账户也跑不掉的底料。 通用段加了两个:magic_number(ulong)用来隔离不同 EA 的订单流,填 0 也行,就是不对订单做魔术码过滤;mode_risk_managemet 是枚举,切换普通账户和自营(如 FTMO)两种风控逻辑。 自营账户得手动给 account_balance_propfirm 塞一个期初余额。FTMO 类的每日最大亏损、总亏损都按期初余额百分比算,常见阈值是 10%,不抓期初值就会用期末余额误判。 亏损侧还有 nmlpo(double),只在 GetLote() 里算,预估下一单打止损会亏多少,不强制但能提前报警。Loss_Profit 结构体 mdl/mwl/ml/gmlpo/mdp 分别管日亏、周亏、总亏、单笔亏和日利润上限。 利润监控靠 daily_profit、weekly_profit、gross_profit 三个 double,加 last_weekly_time、last_day_time、init_time 三个 datetime 锚定统计窗口。外汇和贵金属杠杆高,这类变量若初始化错,EA 可能重仓触发爆仓,上线前务必在策略测试器单步核对。
class=class="str">"cmt">//+------------------------------------------------------------------+ class=class="str">"cmt">//| | class=class="str">"cmt">//| Class CRisk Management | class=class="str">"cmt">//+------------------------------------------------------------------+ class CRiskManagemet final class="macro">#include <Trade/Trade.mqh> class="kw">private: class=class="str">"cmt">//--- CTrade class pointer to be able to work with open positions CTrade *trade; class=class="str">"cmt">//-- Main variables class="type">class="kw">double account_profit; class="type">long StopLoss; class="type">class="kw">double lote; class=class="str">"cmt">//--- General variables ENUM_MODE_RISK_MANAGEMENT mode_risk_managemet; class="type">ulong magic_number; class=class="str">"cmt">//--- Variables to work with anchoring tests class="type">class="kw">double account_balance_propfirm; class=class="str">"cmt">//--- Variables to store the values of the maximum losses class="type">class="kw">double nmlpo; class=class="str">"cmt">//--- variables that store percentages and enumeration, which will be used for the subsequent calculation of losses Loss_Profit mdl, mwl, ml, gmlpo, mdp; class=class="str">"cmt">//--- Variables to store the profit and the time from which they will be obtained class="type">class="kw">double daily_profit, weekly_profit, gross_profit; class="type">class="kw">datetime last_weekly_time, last_day_time, init_time;