- CLI: init, run, status, history, web - Web dashboard: FastAPI + Jinja2 + HTMX + Pico CSS + Chart.js - Core: model wrapper, shape confirmation, risk management, circuit breaker - SQLite state management (trades, positions, equity, signals) - config.yaml для всех параметров - Paper mode с $100 начальным капиталом Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
131 lines
3.5 KiB
Python
131 lines
3.5 KiB
Python
"""Configuration loader — single source of truth for all parameters."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from dataclasses import dataclass, field
|
|
from pathlib import Path
|
|
|
|
import yaml
|
|
|
|
CONFIG_PATH = Path(__file__).parent.parent.parent / "config.yaml"
|
|
DATA_DIR = Path(__file__).parent.parent.parent / "data"
|
|
|
|
|
|
@dataclass
|
|
class AssetConfig:
|
|
ticker: str
|
|
binance_symbol: str
|
|
K: int = 2
|
|
corr_thresh: float = 0.5
|
|
trailing_stop: bool = True
|
|
|
|
|
|
@dataclass
|
|
class ModelConfig:
|
|
window_size: int = 64
|
|
n_steps: int = 20
|
|
total_time: float = 0.5
|
|
shift: int = 7
|
|
epochs: int = 200
|
|
finetune_epochs: int = 50
|
|
lr: float = 0.003
|
|
|
|
|
|
@dataclass
|
|
class RiskConfig:
|
|
per_trade: float = 0.05
|
|
max_position_pct: float = 0.50
|
|
max_open_positions: int = 2
|
|
|
|
|
|
@dataclass
|
|
class CircuitBreakerConfig:
|
|
atr_ratio_on: float = 3.0
|
|
atr_ratio_off: float = 2.0
|
|
max_drawdown: float = 0.20
|
|
dd_reset: float = 0.10
|
|
|
|
|
|
@dataclass
|
|
class ExchangeConfig:
|
|
name: str = "binance"
|
|
api_key: str = ""
|
|
api_secret: str = ""
|
|
testnet: bool = True
|
|
|
|
|
|
@dataclass
|
|
class WebConfig:
|
|
host: str = "0.0.0.0"
|
|
port: int = 8080
|
|
|
|
|
|
@dataclass
|
|
class TelegramConfig:
|
|
enabled: bool = False
|
|
bot_token: str = ""
|
|
chat_id: str = ""
|
|
|
|
|
|
@dataclass
|
|
class Config:
|
|
mode: str = "paper"
|
|
initial_capital: float = 100.0
|
|
currency: str = "USDT"
|
|
timeframe: str = "1d"
|
|
check_time: str = "22:00"
|
|
assets: list[AssetConfig] = field(default_factory=list)
|
|
model: ModelConfig = field(default_factory=ModelConfig)
|
|
risk: RiskConfig = field(default_factory=RiskConfig)
|
|
circuit_breaker: CircuitBreakerConfig = field(default_factory=CircuitBreakerConfig)
|
|
exchange: ExchangeConfig = field(default_factory=ExchangeConfig)
|
|
web: WebConfig = field(default_factory=WebConfig)
|
|
telegram: TelegramConfig = field(default_factory=TelegramConfig)
|
|
|
|
|
|
def load_config(path: Path | None = None) -> Config:
|
|
"""Load configuration from YAML file + environment variables."""
|
|
path = path or CONFIG_PATH
|
|
if not path.exists():
|
|
return Config()
|
|
|
|
with open(path) as f:
|
|
raw = yaml.safe_load(f) or {}
|
|
|
|
assets = [
|
|
AssetConfig(**a) for a in raw.get("assets", [])
|
|
]
|
|
|
|
model_raw = raw.get("model", {})
|
|
risk_raw = raw.get("risk", {})
|
|
cb_raw = raw.get("circuit_breaker", {})
|
|
exchange_raw = raw.get("exchange", {})
|
|
web_raw = raw.get("web", {})
|
|
tg_raw = raw.get("telegram", {})
|
|
capital_raw = raw.get("capital", {})
|
|
schedule_raw = raw.get("schedule", {})
|
|
|
|
exchange = ExchangeConfig(**exchange_raw)
|
|
# Override with env vars (secrets should not be in config file)
|
|
exchange.api_key = os.environ.get("REMIZOV_API_KEY", exchange.api_key)
|
|
exchange.api_secret = os.environ.get("REMIZOV_API_SECRET", exchange.api_secret)
|
|
|
|
telegram = TelegramConfig(**tg_raw)
|
|
telegram.bot_token = os.environ.get("REMIZOV_TG_TOKEN", telegram.bot_token)
|
|
|
|
return Config(
|
|
mode=raw.get("mode", "paper"),
|
|
initial_capital=capital_raw.get("initial", 100.0),
|
|
currency=capital_raw.get("currency", "USDT"),
|
|
timeframe=schedule_raw.get("timeframe", "1d"),
|
|
check_time=schedule_raw.get("check_time", "22:00"),
|
|
assets=assets or [AssetConfig(ticker="BTC-USD", binance_symbol="BTC/USDT")],
|
|
model=ModelConfig(**model_raw),
|
|
risk=RiskConfig(**risk_raw),
|
|
circuit_breaker=CircuitBreakerConfig(**cb_raw),
|
|
exchange=exchange,
|
|
web=WebConfig(**web_raw),
|
|
telegram=telegram,
|
|
)
|