| #ifndef RISK_SIZING_MQH
|
| #define RISK_SIZING_MQH
|
| // ---------------------------------------------------------------------------
|
| // RiskSizing.mqh — position size from account risk (2026-09-17, Magic Keys model)
|
| //
|
| // Fixed BaseLotSize x stacked multipliers (praise x2, War_Survivor x2.5, AI x1.5) put 0.3 lots of
|
| // US Tech 100 on a $50 account and then starved every other chart of margin. In RISK_PERCENT mode
|
| // the lot is derived from the stop the order will actually carry:
|
| // lots = equity * Risk_Percent% * mult / loss_per_lot_at_SL
|
| // where mult is the old multiplier product capped at Risk_Mult_Cap, so signal quality still scales
|
| // size but can never multiply it past the cap.
|
| // ---------------------------------------------------------------------------
|
|
|
| int MinStopPoints(); // TradeExecution.mqh
|
|
|
| // Money lost on one lot if price goes from entry to sl (broker contract maths, symbol-agnostic).
|
| double RS_LossPerLot(const bool is_buy, const double entry, const double sl)
|
| {
|
| double profit = 0.0;
|
| ENUM_ORDER_TYPE type = is_buy ? ORDER_TYPE_BUY : ORDER_TYPE_SELL;
|
| if(!OrderCalcProfit(type, _Symbol, 1.0, entry, sl, profit)) return 0.0;
|
| return MathAbs(profit);
|
| }
|
|
|
| // Sum of the money at risk (to SL) across this EA's open positions on all symbols, as % of equity.
|
| double RS_OpenRiskPercent()
|
| {
|
| double equity = AccountInfoDouble(ACCOUNT_EQUITY);
|
| if(equity <= 0.0) return 0.0;
|
| double total = 0.0;
|
| for(int i = PositionsTotal() - 1; i >= 0; --i)
|
| {
|
| ulong t = PositionGetTicket(i);
|
| if(t == 0 || !PositionSelectByTicket(t)) continue;
|
| if(PositionGetInteger(POSITION_MAGIC) != MagicNumber) continue;
|
| string sym = PositionGetString(POSITION_SYMBOL);
|
| double vol = PositionGetDouble(POSITION_VOLUME);
|
| double open = PositionGetDouble(POSITION_PRICE_OPEN);
|
| double sl = PositionGetDouble(POSITION_SL);
|
| bool buy = (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY);
|
| double cur = buy ? SymbolInfoDouble(sym, SYMBOL_BID) : SymbolInfoDouble(sym, SYMBOL_ASK);
|
| double ref = (sl > 0.0) ? sl : cur; // no stop: count the current floating loss only
|
| double profit = 0.0;
|
| if(OrderCalcProfit(buy ? ORDER_TYPE_BUY : ORDER_TYPE_SELL, sym, vol, open, ref, profit) && profit < 0.0)
|
| total += -profit;
|
| }
|
| return total / equity * 100.0;
|
| }
|
|
|
| // Journal at most one sizing line per minute per chart (sizing runs on every setup tick).
|
| bool RS_MayLog()
|
| {
|
| static datetime last = 0;
|
| if(TimeCurrent() - last < 60) return false;
|
| last = TimeCurrent();
|
| return true;
|
| }
|
|
|
| // Fit the stop to the risk cap when the broker minimum lot is already too big for it (2026-09-18):
|
| // shrink sl_pts (and tp_pts by the same ratio) so that min lot x stop <= Risk_Min_Lot_Max_Percent
|
| // of equity, floored at Risk_Fit_Min_SL_ATR and the broker's minimum stop. Returns true when the
|
| // stop is now affordable (possibly unchanged), false when even the floor is too risky.
|
| bool RiskFitStop(const bool is_buy, const double entry, int &sl_pts, int &tp_pts, const string reason)
|
| {
|
| if(!Risk_Fit_Stop || Risk_Min_Lot_Max_Percent <= 0.0 || Lot_Sizing_Mode != LOTS_RISK_PERCENT) return true;
|
| double equity = AccountInfoDouble(ACCOUNT_EQUITY);
|
| double min_lot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
|
| if(equity <= 0.0 || min_lot <= 0.0 || sl_pts <= 0) return true;
|
|
|
| double sl = is_buy ? entry - sl_pts * _Point : entry + sl_pts * _Point;
|
| double lpl = RS_LossPerLot(is_buy, entry, sl);
|
| if(lpl <= 0.0) return true;
|
| double risk_pct = min_lot * lpl / equity * 100.0;
|
| if(risk_pct <= Risk_Min_Lot_Max_Percent) return true; // fits as it is
|
|
|
| // loss is linear in the stop distance: scale the stop by cap / risk
|
| int floor_pts = MinStopPoints();
|
| if(Current_ATR > 0.0) floor_pts = MathMax(floor_pts, (int)(Current_ATR / _Point * Risk_Fit_Min_SL_ATR));
|
| int fit_pts = (int)MathFloor(sl_pts * (Risk_Min_Lot_Max_Percent / risk_pct));
|
| if(fit_pts < floor_pts)
|
| {
|
| if(RS_MayLog()) Print("Sizing: ", reason, " skipped - fitting the stop to ", DoubleToString(Risk_Min_Lot_Max_Percent, 0),
|
| "% would need ", fit_pts, " pts, below the floor of ", floor_pts, " pts (", DoubleToString(Risk_Fit_Min_SL_ATR, 2), " ATR)");
|
| return false;
|
| }
|
| double ratio = (sl_pts > 0) ? (double)tp_pts / (double)sl_pts : 2.0;
|
| int old_sl = sl_pts;
|
| sl_pts = fit_pts;
|
| tp_pts = MathMax((int)(fit_pts * ratio), MinStopPoints());
|
| if(RS_MayLog()) Print("Sizing: ", reason, " stop fitted ", old_sl, " -> ", sl_pts, " pts (",
|
| DoubleToString(sl_pts * _Point / MathMax(Current_ATR, _Point), 2), " ATR) so the min lot risks ",
|
| DoubleToString(Risk_Min_Lot_Max_Percent, 0), "% | TP ", tp_pts, " pts");
|
| return true;
|
| }
|
|
|
| // Lots for a new entry. Returns 0 when the trade should not be taken (min lot too risky, cap hit).
|
| double RiskSizedLots(const bool is_buy, const double entry, const int sl_pts, const double mult_in, const string reason)
|
| {
|
| double equity = AccountInfoDouble(ACCOUNT_EQUITY);
|
| double mult = MathMin(mult_in, Risk_Mult_Cap);
|
| double sl = is_buy ? entry - sl_pts * _Point : entry + sl_pts * _Point;
|
| double lpl = RS_LossPerLot(is_buy, entry, sl);
|
| if(equity <= 0.0 || lpl <= 0.0)
|
| {
|
| Print("Sizing: cannot value the stop for ", _Symbol, " - falling back to BaseLotSize");
|
| return NormalizeLots(BaseLotSize * mult);
|
| }
|
|
|
| double risk_pct = Risk_Percent * mult;
|
| double risk_money = equity * risk_pct / 100.0;
|
| double raw_lots = risk_money / lpl;
|
| double lots = NormalizeLots(raw_lots); // snaps to step, clamps UP to min lot
|
| double lot_risk = lots * lpl / equity * 100.0; // what the gridded lot really risks
|
| double min_lot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
|
|
|
| if(Risk_Min_Lot_Max_Percent > 0.0 && lot_risk > Risk_Min_Lot_Max_Percent + 0.0001 && lots <= min_lot + 1e-9)
|
| {
|
| if(RS_MayLog()) Print("Sizing: ", reason, " skipped - even the minimum lot risks ", DoubleToString(lot_risk, 1), "% of equity (",
|
| DoubleToString(equity, 2), ") vs cap ", DoubleToString(Risk_Min_Lot_Max_Percent, 1), "% | SL ", sl_pts, " pts");
|
| return 0.0;
|
| }
|
|
|
| if(Max_Open_Risk_Percent > 0.0)
|
| {
|
| double open_risk = RS_OpenRiskPercent();
|
| if(open_risk + lot_risk > Max_Open_Risk_Percent)
|
| {
|
| if(RS_MayLog()) Print("Sizing: ", reason, " skipped - open risk ", DoubleToString(open_risk, 1), "% + this ", DoubleToString(lot_risk, 1),
|
| "% > Max_Open_Risk_Percent ", DoubleToString(Max_Open_Risk_Percent, 1), "%");
|
| return 0.0;
|
| }
|
| }
|
|
|
| if(RS_MayLog()) Print("Sizing: ", reason, " -> ", DoubleToString(lots, 2), " lots | equity ", DoubleToString(equity, 2),
|
| " risk ", DoubleToString(risk_pct, 2), "% (", DoubleToString(risk_money, 2), ") | SL ", sl_pts, " pts = ",
|
| DoubleToString(lpl, 2), "/lot | actual risk ", DoubleToString(lot_risk, 2), "% | mult ", DoubleToString(mult, 2));
|
| return lots;
|
| }
|
| #endif
|