LotSizeCalculator
- Utilità
- Versione: 1.0
- Attivazioni: 5
LotSizeCalculator — Risk-Based Trade Panel
Place market & pending orders with the exact risk you intend — automatic lot sizing, draggable SL/TP, and live recalculation on every tick.
Overview
LotSizeCalculator is a lightweight on-chart panel for fast, disciplined order entry. Set your risk once — as a percentage of balance or a fixed money amount — and the panel calculates the correct lot size so that the real loss when your stop-loss is hit equals exactly what you configured, spread and commission included. No more manual lot math, no more "why did I lose more than planned?"
Key Features
- Automatic lot sizing from your risk: % of balance or fixed cash (e.g. risk 10 USD).
- Risk measured from the entry price, so the realized loss at SL matches your target.
- Commission-aware — auto-detects round-trip commission from your trade history, or set it manually.
- Draggable SL / TP lines on the chart — drag freely and the lot adapts in real time; or type pips / price / risk-reward ratio.
- Live recalculation every tick: as price drifts, the lot stays sized to keep your risk constant.
- Direction auto-detect from where you place the stop line, with clear BUY / SELL buttons.
- Market and pending orders — Buy Limit, Sell Limit, Buy Stop, Sell Stop, with the correct type auto-resolved from your entry price.
- Live header: balance, equity, spread, and detected commission.
- Clean light-theme panel, English UI, drag to reposition.
How the lot sizing works
Lot = Risk / (SL_distance × pip_value + commission_per_lot)
SL distance is taken from your entry, and commission is included explicitly — so the figure shown as Real Risk is what you actually lose if the stop is hit. The panel also shows the Real Reward at take-profit.
Pending orders
Switch the Order mode to Pending, set an Entry price (type it or drag the blue entry line), and place BUY or SELL. The panel automatically chooses the correct order type:
- BUY — entry below market → Buy Limit, entry above market → Buy Stop
- SELL — entry above market → Sell Limit, entry below market → Sell Stop
In pending mode the SL/TP distances and the lot are measured from your entry price, so the risk is correct for the level the order will actually trigger at.
Built-in safety checks
Before any order is sent the panel verifies that AutoTrading is enabled, the EA is allowed to trade, the symbol is tradable, the entry respects the broker's minimum stop distance, and there is enough free margin for the calculated lot. If a check fails, you get a clear on-panel message instead of a cryptic error — and no order is sent.
Inputs
- Risk mode / value — percent of balance or fixed money.
- Default SL (pips), TP mode (Pips / Price / R:R) and value.
- Execution mode — Market or Pending (startup default).
- Commission — auto-detect on/off, manual fallback, history lookback days.
- Magic number, order comment, max slippage (points).
- Panel position (X / Y).
Notes
- One instance per chart; different charts can use different settings.
- Works on Forex, metals, indices, and other symbols — lot sizing uses the broker's tick value, so the money risk is correct regardless of the instrument.
- Manual trading tool: it places orders only when you click. It does not trade on its own.
- Please enable Allow Algo Trading when attaching the EA.
INSTALLATION
------------
1. Open MetaTrader 5.
2. From the menu: File -> Open Data Folder.
3. Navigate to: MQL5\Experts\
4. Create a new sub-folder named: LotSizeCalculator
5. Copy ALL of these files into that folder:
- LotSizeCalculator.mq5
- Defs.mqh
- RiskCalc.mqh
- CommissionDetector.mqh
- ChartLines.mqh
- Panel.mqh
6. In MT5, open the MetaEditor (F4 in MT5, or open MetaEditor.exe).
7. In the Navigator panel, find:
Experts -> LotSizeCalculator -> LotSizeCalculator.mq5
8. Right-click the file -> Compile (or press F7).
The file should compile with 0 errors / 0 warnings.
9. Back in MT5, in the Navigator under Experts you will now see
"LotSizeCalculator". Drag it onto a chart.
10. In the dialog that appears:
- "Common" tab: tick "Allow algo trading"
- "Inputs" tab: review settings (defaults are fine for first run)
- Click OK.
The panel will appear in the top-left of the chart.
USAGE
-----
Header (top of panel)
Symbol - the chart's symbol
Balance - account balance
Equity - account equity
Spread - live spread in pips (auto-detected per tick)
Comm/lot - commission per 1 lot, round trip:
- "auto" if detected from your trade history
- "manual" if using the InpManualCommission input
Click [Re-scan] to retry history detection.
Risk
Mode toggle: [%] = percent of balance
[$] = fixed money
Edit field your number
Below shows the resolved money amount.
Order (execution type)
Mode toggle: [Mkt] market order at current price (default)
[Pend] pending order (Limit / Stop)
Entry field in Pending mode, type the trigger price (or drag the
blue Entry line on the chart). In Market mode it just
shows "(market)".
Below shows the resolved pending type, e.g. "= Buy Limit @ 1.2345".
The exact type is chosen automatically from your Entry vs. the
current market and the direction:
BUY : Entry below Ask -> Buy Limit, Entry above Ask -> Buy Stop
SELL: Entry above Bid -> Sell Limit, Entry below Bid -> Sell Stop
In Pending mode, SL/TP pip distances and the lot are measured from
your Entry line (not the live price), so the risk is correct for the
price the order will actually trigger at. Pending orders are GTC
(good till cancelled). An Entry too close to the market (within the
broker's stops level) is rejected with a message.
SL
Mode toggle: [Pips] distance in pips from entry
[Price] absolute SL price
The lot is sized so that the actual loss when SL is hit equals
your Risk amount, INCLUDING spread and commission.
TP
Mode toggle: [Pips] distance in pips
[Price] absolute TP price
[R:R] take-profit as a multiple of SL distance
Results (live)
Lot Size calculated lot, rounded to broker's lot step
Real Risk real money loss when SL hits, includes commission
Real Reward real money gain when TP hits, minus commission
Action
[BUY] in Market mode: market buy at Ask. In Pending mode: places
a Buy Limit or Buy Stop at your Entry. SL/TP are placed so
the SL distance from entry equals your configured pips.
[SELL] mirror of BUY (Sell / Sell Limit / Sell Stop).
Direction is detected automatically from where the SL line sits:
SL line BELOW current price → BUY setup → BUY button highlighted
SL line ABOVE current price → SELL setup → SELL button highlighted
The inactive direction's button is dimmed but still works (it will
flip the line to the other side and execute the opposite trade).
Chart lines (when "Show ... lines" is on)
- Solid blue = Entry. Market mode: follows live Ask. Pending mode:
draggable, sets your limit/stop trigger price.
- Dashed red = SL (the absolute SL price you chose)
- Dashed green = TP (the absolute TP price; auto-flips with direction
in R:R / Pips modes)
The SL line is the SOURCE OF TRUTH. Drag it freely:
- Drag BELOW current price → BUY setup. TP is placed ABOVE Ask
(R:R / Pips modes auto-position TP on the other side of entry).
- Drag ABOVE current price → SELL setup. TP is placed BELOW Bid.
The line stays exactly where you put it; it does NOT snap back. Each
tick the panel measures the live pip distance from the relevant
entry (Ask for BUY, Bid for SELL) to your SL line, and recomputes
the lot size so the realized loss when SL is hit equals your Risk
amount. As price drifts, the lot adjusts; the line stays anchored.
Typing a value into the SL or TP field does the inverse: it moves
the line to that distance (pips, on the side matching the current
direction) or to that absolute price. Pressing Enter commits the
move; clicking BUY or SELL also commits any unsubmitted edit.
NOTES
-----
- Before sending an order the EA checks: AutoTrading is on, Algo
Trading is allowed for the EA, the account/broker permits trading,
the symbol is tradable, and there is enough free margin for the
lot. If any check fails, the status line explains why and NO order
is sent.
- Auto-commission requires you to have at least one closed trade on
this symbol within the InpCommLookbackDays window. If none, the
EA falls back to InpManualCommission.
- Lot sizing formula:
Lot = Risk_Money / (SL_pips * PipValuePerLot + Commission_RT)
- SL price for BUY = Ask - SL_pips, for SELL = Bid + SL_pips. This
makes the SL distance from your entry equal exactly to the pips
value displayed, so the realized loss matches the calculation.
- One EA per chart. Different charts can have different settings.
