Skip to content

MT4Sugar — 4. Risk Sizing

Turn "risk this much cash over this stop" into a broker-valid lot — and back.

Both use the symbol's pip value (derived from tick value/size). The lot result is already normalized and clamped.


CalcCashRisk(ctx, symbol, lots, slPips)

func (s *MT4Sugar) CalcCashRisk(ctx context.Context, symbol string, lots, slPips float64) (float64, error)

The cash at risk (account currency) for lots over an slPips stop — the inverse of CalcLotByRisk.

Example — how much does 0.10 lot risk over 20 pips?

risk, _ := sugar.CalcCashRisk(ctx, "EURUSD", 0.10, 20) // e.g. 20.00
fmt.Printf("risking %.2f\n", risk)

CalcLotByRisk(ctx, symbol, riskCash, slPips)

func (s *MT4Sugar) CalcLotByRisk(ctx context.Context, symbol string, riskCash, slPips float64) (float64, error)

The normalized lot size that risks riskCash (account currency) over an slPips stop. It converts cash → lots via the symbol's pip value, then rounds to the volume step and clamps to [min, max].

Guards: if slPips <= 0 or the pip value is unknown it returns the symbol's minimum lot instead of erroring — so a bad input never sends an oversized order.

Example — risk 1% of balance on a 25-pip stop:

bal := must(sugar.GetAccountInfo(ctx)).GetAccountBalance()
lots, err := sugar.CalcLotByRisk(ctx, "EURUSD", bal*0.01, 25) // e.g. 0.12

Example — size then enter in one flow:

bal := must(sugar.GetAccountInfo(ctx)).GetAccountBalance()
lots, _ := sugar.CalcLotByRisk(ctx, "EURUSD", bal*0.01, 20)
r, _ := sugar.BuyMarket(ctx, "EURUSD", lots, nil, nil)
_ = sugar.ModifySLTPByPips(ctx, r.GetTicket(), ptr(20.0), ptr(40.0))

See also: 3. Symbol Math · 5. Order Entry