- AssetConfig: +window_size, +weight, +leverage fields - Config: +model_for(), +leverage_for() helpers - executor: weight-based position sizing (equity × weight × leverage) - cli init: per-asset window_size for model training - notify: show ws/lev/weight% in Telegram stats - config.yaml: Sharpe-optimal params (exp15 grid search) BTC ws=32 24.1%, ETH ws=64 15.8%, ADA ws=64 13.7%, DOT ws=32 13.7%, DOGE ws=48 12.7%, SOL ws=128 10.8%, XRP ws=64 9.2% Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
177 lines
5.4 KiB
Python
177 lines
5.4 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
|
|
symbol: str # ccxt format, e.g. "BTC/USDT"
|
|
K: int = 2
|
|
corr_thresh: float = 0.5
|
|
trailing_stop: bool = True
|
|
timeframe: str = "" # per-asset override; empty = use global
|
|
window_size: int = 0 # per-asset override; 0 = use global model.window_size
|
|
weight: float = 0.0 # portfolio weight (0 = equal allocation)
|
|
leverage: int = 0 # per-asset leverage; 0 = use global risk.leverage
|
|
|
|
|
|
@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
|
|
leverage: int = 1 # 1 = no leverage, 2-10 = spot margin
|
|
|
|
|
|
@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
|
|
dd_cooldown_hours: int = 48
|
|
|
|
|
|
@dataclass
|
|
class ExchangeConfig:
|
|
name: str = "bybit"
|
|
data_source: str = "exchange" # "exchange" (ccxt) or "yfinance"
|
|
api_key: str = ""
|
|
api_secret: str = ""
|
|
testnet: bool = True
|
|
|
|
|
|
@dataclass
|
|
class WebConfig:
|
|
host: str = "0.0.0.0"
|
|
port: int = 8080
|
|
|
|
|
|
@dataclass
|
|
class LLMConfig:
|
|
enabled: bool = False
|
|
base_url: str = "https://api.z.ai/api/anthropic"
|
|
api_key: str = ""
|
|
model: str = "glm-4.5-air"
|
|
sentiment_threshold: float = -0.3
|
|
|
|
|
|
@dataclass
|
|
class TelegramConfig:
|
|
enabled: bool = False
|
|
bot_token: str = ""
|
|
chat_id: str = ""
|
|
|
|
|
|
@dataclass
|
|
class Config:
|
|
mode: str = "paper"
|
|
initial_capital: float = 100.0
|
|
total_capital: float = 0.0 # actual total deposited; 0 = auto-compute
|
|
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)
|
|
llm: LLMConfig = field(default_factory=LLMConfig)
|
|
|
|
def model_for(self, asset: AssetConfig) -> ModelConfig:
|
|
"""Get ModelConfig with per-asset window_size override."""
|
|
ws = asset.window_size or self.model.window_size
|
|
if ws == self.model.window_size:
|
|
return self.model
|
|
return ModelConfig(
|
|
window_size=ws,
|
|
n_steps=self.model.n_steps,
|
|
total_time=self.model.total_time,
|
|
shift=self.model.shift,
|
|
epochs=self.model.epochs,
|
|
finetune_epochs=self.model.finetune_epochs,
|
|
lr=self.model.lr,
|
|
)
|
|
|
|
def leverage_for(self, asset: AssetConfig) -> int:
|
|
"""Get leverage: per-asset override or global."""
|
|
return asset.leverage or self.risk.leverage
|
|
|
|
|
|
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_raw = raw.get("assets", [])
|
|
for a in assets_raw:
|
|
# Backward compat: binance_symbol → symbol
|
|
if "binance_symbol" in a and "symbol" not in a:
|
|
a["symbol"] = a.pop("binance_symbol")
|
|
assets = [AssetConfig(**a) for a in assets_raw]
|
|
|
|
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)
|
|
|
|
llm_raw = raw.get("llm", {})
|
|
llm = LLMConfig(**llm_raw)
|
|
llm.api_key = os.environ.get("REMIZOV_LLM_TOKEN", llm.api_key)
|
|
|
|
return Config(
|
|
mode=raw.get("mode", "paper"),
|
|
initial_capital=capital_raw.get("initial", 100.0),
|
|
total_capital=capital_raw.get("total", 0.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", symbol="BTC/USDT")],
|
|
model=ModelConfig(**model_raw),
|
|
risk=RiskConfig(**risk_raw),
|
|
circuit_breaker=CircuitBreakerConfig(**cb_raw),
|
|
exchange=exchange,
|
|
web=WebConfig(**web_raw),
|
|
telegram=telegram,
|
|
llm=llm,
|
|
)
|