Spaces:
Sleeping
Sleeping
File size: 936 Bytes
9d82314 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | """Position sizing. Pure functions.
notional = min(slot_capital * leverage, capital * risk_per_trade_pct / stop_loss_pct)
quantity = floor(notional / entry_price)
slot_capital = capital / max_positions
Worked check, capital 100000, leverage 5, risk 5%, stop 1%, max_positions 1:
slot notional = 500000, risk notional = 500000 -> 500000 notional
loss at stop = 500000 * 1% = 5000 = 5% of capital
"""
from __future__ import annotations
import math
from engine.config import BacktestConfig
def position_quantity(capital: float, entry_price: float, cfg: BacktestConfig) -> int:
if entry_price <= 0:
raise ValueError("entry_price must be positive")
slot_capital = capital / cfg.max_positions
slot_notional = slot_capital * cfg.leverage
risk_notional = capital * cfg.risk_per_trade_pct / cfg.stop_loss_pct
notional = min(slot_notional, risk_notional)
return math.floor(notional / entry_price)
|