71 lines
2.5 KiB
Python
Executable File
71 lines
2.5 KiB
Python
Executable File
"""Runtime settings (environment) and strategy configuration (YAML)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from typing import Any, Literal
|
|
|
|
import yaml
|
|
from pydantic import BaseModel, Field, SecretStr, field_validator, model_validator
|
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
|
|
from .kraken import VALID_INTERVALS
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
model_config = SettingsConfigDict(env_file=".env", extra="ignore")
|
|
|
|
kraken_api_key: str | None = None
|
|
kraken_api_secret: str | None = None
|
|
dashboard_username: str = "admin"
|
|
dashboard_password: SecretStr | None = None
|
|
# Safety switch: strategies in "live" mode refuse to start unless this is true.
|
|
live_trading_enabled: bool = False
|
|
config_path: Path = Path("config/strategies.yaml")
|
|
data_dir: Path = Path("data")
|
|
host: str = "0.0.0.0"
|
|
port: int = 8080
|
|
log_level: str = "INFO"
|
|
|
|
|
|
class StrategyConfig(BaseModel):
|
|
id: str = Field(pattern=r"^[a-zA-Z0-9_-]+$")
|
|
type: str
|
|
mode: Literal["paper", "live"] = "paper"
|
|
enabled: bool = True
|
|
starting_capital: float = Field(gt=0)
|
|
pairs: list[str] = Field(min_length=1)
|
|
interval: int = 60
|
|
params: dict[str, Any] = Field(default_factory=dict)
|
|
|
|
@field_validator("interval")
|
|
@classmethod
|
|
def _valid_interval(cls, v: int) -> int:
|
|
if v not in VALID_INTERVALS:
|
|
raise ValueError(f"interval must be one of {sorted(VALID_INTERVALS)}")
|
|
return v
|
|
|
|
|
|
class AppConfig(BaseModel):
|
|
poll_interval_seconds: int = Field(60, ge=10)
|
|
snapshot_interval_seconds: int = Field(300, ge=10)
|
|
fee_rate: float = Field(0.004, ge=0, description="Taker fee used for paper fills")
|
|
slippage: float = Field(0.0005, ge=0, description="Price slippage used for paper fills")
|
|
min_trade_fraction: float = Field(
|
|
0.01, ge=0, description="Ignore rebalances smaller than this fraction of equity"
|
|
)
|
|
strategies: list[StrategyConfig] = Field(default_factory=list)
|
|
|
|
@model_validator(mode="after")
|
|
def _unique_ids(self) -> AppConfig:
|
|
ids = [s.id for s in self.strategies]
|
|
dupes = {i for i in ids if ids.count(i) > 1}
|
|
if dupes:
|
|
raise ValueError(f"Duplicate strategy ids: {sorted(dupes)}")
|
|
return self
|
|
|
|
|
|
def load_config(path: Path) -> AppConfig:
|
|
with open(path, encoding="utf-8") as f:
|
|
return AppConfig.model_validate(yaml.safe_load(f) or {})
|