Spaces:
Sleeping
Sleeping
Download engine/sizing.py from BharathPESU/meta: direct link, hf CLI and curl.
- Browser
- Download file 936 Bytes
-
https://huggingface.co/spaces/BharathPESU/meta/resolve/main/engine/sizing.py
- Command line
-
hf download hf://spaces/BharathPESU/meta/engine/sizing.py
-
curl -L -o sizing.py https://huggingface.co/spaces/BharathPESU/meta/resolve/main/engine/sizing.py
936 Bytes
| """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) | |