89 lines
3.8 KiB
Python
89 lines
3.8 KiB
Python
from dataclasses import dataclass
|
|
from datetime import datetime, timedelta
|
|
import os
|
|
from pathlib import Path
|
|
from zoneinfo import ZoneInfo
|
|
|
|
ROOT = Path(__file__).resolve().parent
|
|
REPO = ROOT.parent
|
|
TZ = ZoneInfo("Asia/Bangkok")
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Settings:
|
|
data_path: Path
|
|
port: int = 18901
|
|
live_orders: bool = False
|
|
auto_trade: bool = False
|
|
auto_start: str = ""
|
|
auto_end: str = ""
|
|
server_utc_offset_minutes: int = 0
|
|
magic: int = 1061501
|
|
model_start: str = "2026-01-01"
|
|
history_bars: int = 1000
|
|
eod_hour: int = 20
|
|
eod_minute: int = 30
|
|
max_premium_pct: float = 2.0
|
|
fee_reserve_pct: float = 0.2
|
|
refresh_seconds: int = 60
|
|
state_dir: Path = ROOT / "data"
|
|
|
|
@property
|
|
def warmup_start(self):
|
|
return (datetime.fromisoformat(self.model_start) - timedelta(days=700)).date().isoformat()
|
|
|
|
def closed_through(self, now=None):
|
|
from .market import Market
|
|
now = (now or datetime.now(TZ)).astimezone(TZ)
|
|
day = now.date()
|
|
market = Market()
|
|
if ((now.hour, now.minute) < (self.eod_hour, self.eod_minute)
|
|
or market.reason(day)):
|
|
day = market.previous_session(day)
|
|
return day.isoformat()
|
|
|
|
@classmethod
|
|
def load(cls):
|
|
values = {}
|
|
env = ROOT / ".env"
|
|
if env.exists():
|
|
for raw in env.read_text(encoding="utf-8-sig").splitlines():
|
|
if raw.strip() and not raw.lstrip().startswith("#") and "=" in raw:
|
|
key, value = raw.split("=", 1)
|
|
values[key.strip()] = value.strip().strip('"').strip("'")
|
|
values.update({k: v for k, v in os.environ.items() if k.startswith("MT5_")})
|
|
path = values.get("MT5_DATA_PATH", "")
|
|
if not path:
|
|
raise ValueError("Set MT5_DATA_PATH in mt5_app/.env")
|
|
config = cls(
|
|
data_path=Path(path).resolve(),
|
|
port=int(values.get("MT5_PORT", 18901)),
|
|
live_orders=values.get("MT5_ENABLE_LIVE_ORDERS", "false").lower() == "true",
|
|
auto_trade=values.get("MT5_AUTO_TRADE", "false").lower() == "true",
|
|
auto_start=values.get("MT5_AUTO_START_DATE", ""),
|
|
auto_end=values.get("MT5_AUTO_END_DATE", ""),
|
|
server_utc_offset_minutes=int(values.get("MT5_SERVER_UTC_OFFSET_MINUTES", 0)),
|
|
magic=int(values.get("MT5_MAGIC", 1061501)),
|
|
model_start=values.get("MT5_MODEL_START", "2026-01-01"),
|
|
history_bars=int(values.get("MT5_HISTORY_BARS", 1000)),
|
|
eod_hour=int(values.get("MT5_EOD_HOUR", 20)),
|
|
eod_minute=int(values.get("MT5_EOD_MINUTE", 30)),
|
|
max_premium_pct=float(values.get("MT5_MAX_ENTRY_PREMIUM_PCT", 2)),
|
|
fee_reserve_pct=float(values.get("MT5_FEE_RESERVE_PCT", .2)),
|
|
refresh_seconds=int(values.get("MT5_REFRESH_SECONDS", 60)),
|
|
)
|
|
if not 1024 <= config.port <= 65535 or config.history_bars < 700:
|
|
raise ValueError("Invalid port or fewer than 700 history bars")
|
|
if not 0 <= config.max_premium_pct <= 20 or not 0 <= config.fee_reserve_pct <= 5:
|
|
raise ValueError("Invalid price premium or fee reserve")
|
|
if config.auto_trade and (not config.live_orders or not config.auto_start):
|
|
raise ValueError("Automatic execution requires live orders and an explicit start date")
|
|
for day in (config.auto_start, config.auto_end):
|
|
if day:
|
|
datetime.strptime(day, '%Y-%m-%d')
|
|
if config.auto_end and config.auto_end < config.auto_start:
|
|
raise ValueError("Automatic execution end date precedes start date")
|
|
if not -720 <= config.server_utc_offset_minutes <= 840:
|
|
raise ValueError("MT5 server UTC offset must be between -720 and 840 minutes")
|
|
return config
|