Alım-satım robotlarını ücretsiz olarak nasıl indirebileceğinizi izleyin
Bizi Twitter üzerinde bulun!
Fan sayfamıza katılın
Kodu ilginç mi buldunuz?
Öyleyse bir link gönderin -
başkalarının da faydalanmasını sağlayın
Kodu beğendiniz mi? MetaTrader 5 terminalinde deneyin
Kütüphaneler

Position sizing from risk, using the broker's own tick value and volume step - MetaTrader 5 için kütüphane

Ralph Ivan Simeon
Ralph Ivan Simeon
I build and audit automated trading systems for MetaTrader 5, with most of my work on XAUUSD.
Görüntülemeler:
53
Derecelendirme:
(1)
Yayınlandı:
MQL5 Freelance Bu koda dayalı bir robota veya göstergeye mi ihtiyacınız var? Freelance üzerinden sipariş edin Freelance'e git

Every lot size calculator that assumes one pip is worth ten dollars is wrong on gold, wrong on indices, wrong on JPY crosses and wrong on any account not denominated in USD - which is exactly where oversizing hurts. This header does the arithmetic from what the server actually publishes for the symbol instead.

What it does What CalcLots() returns, including the case most sizing routines get wrong

CalcLots() takes a risk amount in account currency, an entry price and a stop price, and returns the volume to trade together with what that volume really risks once the broker's constraints have been applied.

  • It always rounds volume DOWN onto the broker's volume step. Rounding up would silently risk more than you asked for, which is the one thing a sizing routine must never do.
  • When the requested risk is smaller than the minimum lot it does not pretend. It returns the minimum, sets clamped_min, and reports the risk that position actually carries - which will be larger than the request.
  • It is side-agnostic. The stop may sit above or below the entry.

It also returns the stop distance in points and the value of one point per 1.00 lot, so a caller can show its working rather than just a number.

Why the maths is a separate header

Calc.mqh holds no terminal state. It takes a SymbolSpec struct - point, tick size, tick value, volume min/max/step, digits - so the whole of the sizing logic can be exercised offline against synthetic instruments.

SpecFromSymbol() is the only bridge to live data. It reads SYMBOL_TRADE_TICK_VALUE_LOSS first, because that is the figure the server uses when a trade goes against you, which is exactly the side a stop loss sits on; it falls back to SYMBOL_TRADE_TICK_VALUE for brokers that leave the loss variant at zero.

That separation is what makes the included test script possible at all.

The test script

RiskLotCalculatorTests.mq5 runs on any chart, with no broker connection and no history. It asserts 41 cases: volume-step rounding, both clamps and their flags, point value scaling when tick size differs from point size, a 5-digit major, gold, an index with a 0.1 lot step, both trade directions, and every rejection path - zero risk, negative risk, a stop sitting on the entry, and missing tick data.

Drag it onto a chart and read the Experts tab. It ends with ALL PASS, passed=41, failed=0.

Both files are self-contained: drop them in the same folder and the test script compiles as-is.

Heiken Ashi ATR Blend Heiken Ashi ATR Blend

Heiken Ashi ATR Blend combines smoothed Heiken Ashi candles, ATR-based volatility filtering, and fast/slow EMA trend confirmation to identify potential trend-change signals.

Smart Loss Exit — early exit for losing positions (ATR adverse excursion, time in trade, EMA invalidation, RSI, money cap) with a CSV journal Smart Loss Exit — early exit for losing positions (ATR adverse excursion, time in trade, EMA invalidation, RSI, money cap) with a CSV journal

Closes losing positions early, before the full stop-loss, when a rule says the trade is not coming back: adverse move beyond N x ATR, too long in loss, EMA trend flipped, RSI against the position, or a money cap. Never touches a position in profit. Observe-only mode journals what would be closed without closing anything. Every exit is written to CSV with the rule, the age and the maximum adverse excursion, so the rules can be tuned from data.

Accelerator Oscillator (AC) Accelerator Oscillator (AC)

The Acceleration/Deceleration Indicator (AC) measures acceleration and deceleration of the current driving force.

MACD Signals MACD Signals

Indicator edition for new platform.