Spaces:
Running
Running
Download engine/config.py from BharathPESU/meta: direct link, hf CLI and curl.
- Browser
- Download file 2.63 kB
-
https://huggingface.co/spaces/BharathPESU/meta/resolve/main/engine/config.py
- Command line
-
hf download hf://spaces/BharathPESU/meta/engine/config.py
-
curl -L -o config.py https://huggingface.co/spaces/BharathPESU/meta/resolve/main/engine/config.py
2.63 kB
| """Typed backtest configuration. Defaults come from config/defaults.yaml.""" | |
| from __future__ import annotations | |
| from dataclasses import dataclass, field | |
| from datetime import time | |
| from pathlib import Path | |
| import yaml | |
| DEFAULT_CONFIG_PATH = Path(__file__).resolve().parents[1] / "config" / "defaults.yaml" | |
| class CostModel: | |
| """Per-leg trading costs. All rates are TODO(verify) against current schedules.""" | |
| brokerage_pct: float = 0.0003 | |
| brokerage_cap: float = 20.0 | |
| exchange_pct: float = 0.0000297 | |
| sebi_pct: float = 0.000001 | |
| gst_pct: float = 0.18 | |
| stt_sell_pct: float = 0.00025 | |
| stamp_buy_pct: float = 0.00003 | |
| slippage_pct: float = 0.0005 | |
| class BacktestConfig: | |
| capital: float = 100_000.0 | |
| risk_per_trade_pct: float = 0.05 | |
| leverage: float = 5.0 | |
| max_positions: int = 5 | |
| stop_loss_pct: float = 0.01 | |
| long_threshold: float = 0.30 | |
| short_threshold: float = -0.30 | |
| min_expected_pat_cr: float = 10.0 | |
| entry_time: time = time(9, 15) | |
| square_off_time: time = time(15, 15) | |
| enable_long: bool = True | |
| enable_short: bool = True | |
| bar_interval: str = "5min" | |
| costs: CostModel = field(default_factory=CostModel) | |
| def __post_init__(self) -> None: | |
| if self.capital <= 0: | |
| raise ValueError("capital must be positive") | |
| if not 0 < self.risk_per_trade_pct <= 1: | |
| raise ValueError("risk_per_trade_pct must be in (0, 1]") | |
| if self.leverage <= 0: | |
| raise ValueError("leverage must be positive") | |
| if self.max_positions < 1: | |
| raise ValueError("max_positions must be at least 1") | |
| if not 0 < self.stop_loss_pct < 1: | |
| raise ValueError("stop_loss_pct must be in (0, 1)") | |
| if self.long_threshold <= 0: | |
| raise ValueError("long_threshold must be positive") | |
| if self.short_threshold >= 0: | |
| raise ValueError("short_threshold must be negative") | |
| if self.min_expected_pat_cr < 0: | |
| raise ValueError("min_expected_pat_cr cannot be negative") | |
| if self.entry_time >= self.square_off_time: | |
| raise ValueError("entry_time must be before square_off_time") | |
| def from_yaml(cls, path: Path | str = DEFAULT_CONFIG_PATH) -> BacktestConfig: | |
| raw = yaml.safe_load(Path(path).read_text()) or {} | |
| costs_raw = raw.pop("costs", {}) or {} | |
| for key in ("entry_time", "square_off_time"): | |
| if isinstance(raw.get(key), str): | |
| raw[key] = time.fromisoformat(raw[key]) | |
| return cls(**raw, costs=CostModel(**costs_raw)) | |