红色单子?我们给你做成绿色。 利润先提到你银行卡 — 我们事后再分。 不预付,不需账户权限。 🟢 WhatsApp:85254981192 ⚪ Telegram:@kkk437
LotFromRisk is an include file for EA developers. It turns a risk figure, a stop and a commission per lot into a lot size whose loss at the stop never goes over the risk you asked for.
lots = money at risk / (loss of 1 lot at the stop + commission of 1 lot, round turn)
Not counted: slippage at the stop, price gaps and swap.
The risk can be a percent of the balance, a percent of the equity or money in the account currency (ENUM_LFR_RISK). LfrResult holds lots, riskMoney, lossPerLot, commissionPerLot, lossAtStop, margin, marginOk, a code (ENUM_LFR_CODE) and a reason in plain English.
#include <LotFromRisk\LotFromRisk.mqh>
LfrResult r;
double sl = SymbolInfoDouble(_Symbol, SYMBOL_ASK) - 300 * _Point;
if(LfrLots(_Symbol, ORDER_TYPE_BUY, 0, sl, 1.0, LFR_PERCENT_BALANCE, 7.0, r))
trade.Buy(r.lots, _Symbol, 0, sl, 0);
else
Print("no trade: ", r.reason); LotFromRiskDemo.mq5 calculates a buy and a sell on the chart symbol and shows the result in the Experts log and on the chart. It sends no orders. Inputs: risk mode, risk, stop distance in points and commission per lot (-1 takes the estimate from the last 90 days of deals).

The screenshot shows XAUUSD with the stop 500 points (5.00) away, 1% of the balance at risk (84.57 USD) and a commission of 7 per lot round turn: 0.16 lots, 81.12 lost at the stop (1 lot: 500.00 plus 7.00 commission).
The code is free to use and modify, also in your own products. It is provided as is, without warranty. Test it on a demo account first.
Tested on MetaTrader 5 build 6331, a hedging demo account in USD, with EURUSD, USDJPY, GBPJPY, EURGBP, XAUUSD, BTCUSD and USDCHF. In 4054 random cases no size went over the risk. Rounding to the nearest step instead would have gone over in 1977 of them.
Dashboard Panel
A clean information card for your EA or indicator: rows, progress bars and a status pill on one anti-aliased CCanvas bitmap, light or dark to match the chart, and foldable with a click.
Broker Clock
Broker server time, UTC and the local time of London, Frankfurt, New York, Tokyo and Sydney, with daylight saving time on any date, live and in the Strategy Tester.
Trade Cost Meter
Shows the real cost of a trade on the current symbol (spread, commission, swap) and which share of your risk (R) it takes on every timeframe from M1 to D1.
Break-even and Trailing Stop Manager
Moves the stop loss to break-even (with locked profit) and trails it by a fixed distance or an ATR multiple. Never opens or closes trades, the stop loss only moves forward.