72 lines
2.5 KiB
Python
72 lines
2.5 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
if str(ROOT) not in sys.path:
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
from src.services.backtest import detect_confirmed_swings_for_replay, iter_historical_replay, load_replay_candles
|
|
from src.services.research_council import ResearchCouncilService
|
|
|
|
|
|
def main(env=None) -> int:
|
|
env = os.environ if env is None else env
|
|
instrument_id = int(env.get("RESEARCH_COUNCIL_INSTRUMENT_ID", "1"))
|
|
timeframe = env.get("RESEARCH_COUNCIL_TIMEFRAME", "1m")
|
|
model_code = env.get("RESEARCH_COUNCIL_MODEL_CODE", "OSOK_DAILY_RANGE_SWEEP_MSS_FVG_RETRACE")
|
|
replay_file = str(env.get("RESEARCH_COUNCIL_REPLAY_FILE") or "").strip()
|
|
output_json = _get_bool(env, "RESEARCH_COUNCIL_OUTPUT_JSON", False)
|
|
service = ResearchCouncilService()
|
|
payload = {
|
|
"instrument_id": instrument_id,
|
|
"timeframe": timeframe,
|
|
"model_code": model_code,
|
|
"resume_state": service.get_resume_state(instrument_id=instrument_id),
|
|
"preview": service.build_preview(instrument_id=instrument_id, timeframe=timeframe, model_code=model_code),
|
|
"replay": None,
|
|
}
|
|
if replay_file:
|
|
payload["replay"] = build_replay_summary(replay_file)
|
|
payload["passed"] = True
|
|
_print_payload(payload, output_json=output_json)
|
|
return 0
|
|
|
|
|
|
def build_replay_summary(path: str) -> dict:
|
|
candles = load_replay_candles(path)
|
|
frames = list(iter_historical_replay(candles))
|
|
swings = detect_confirmed_swings_for_replay(candles)
|
|
return {
|
|
"path": path,
|
|
"candle_count": len(candles),
|
|
"frame_count": len(frames),
|
|
"confirmed_swing_count": len(swings),
|
|
"first_ts": candles[0].get("ts_open") if candles else None,
|
|
"last_ts": candles[-1].get("ts_close") if candles else None,
|
|
}
|
|
|
|
|
|
def _print_payload(payload: dict, *, output_json: bool) -> None:
|
|
if output_json:
|
|
print(json.dumps(payload, ensure_ascii=False, sort_keys=True, default=str))
|
|
return
|
|
preview = dict(payload.get("preview") or {})
|
|
decision = dict(preview.get("decision") or {})
|
|
print(f"Research council replay summary for instrument {payload.get('instrument_id')}")
|
|
print(f"Overall stance: {decision.get('overall_stance')}")
|
|
|
|
|
|
def _get_bool(env, key: str, default: bool) -> bool:
|
|
value = env.get(key)
|
|
if value is None or value == "":
|
|
return default
|
|
return str(value).strip().lower() in {"1", "true", "yes", "on"}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|