142 lines
5.3 KiB
Python
142 lines
5.3 KiB
Python
from pathlib import Path
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
os.environ.setdefault("DATABASE_URL", "postgresql+psycopg://user:pass@localhost:5432/ai_ict_test")
|
|
if str(ROOT) not in sys.path:
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
from scripts.run_trading_plans import DEFAULT_TRADING_PLAN_SYMBOLS
|
|
from src.api.trading_runtime import run_trading_watch
|
|
from src.services.trade_plan_candidates import SetupWatcherService
|
|
|
|
|
|
def build_trade_plan_candidate_symbols_from_env(env=None) -> tuple[str, ...]:
|
|
env = os.environ if env is None else env
|
|
raw_value = str(
|
|
env.get("TRADE_PLAN_CANDIDATE_SYMBOLS")
|
|
or env.get("TRADING_PLAN_SYMBOLS")
|
|
or ",".join(DEFAULT_TRADING_PLAN_SYMBOLS)
|
|
)
|
|
symbols = tuple(item.strip() for item in raw_value.split(",") if item.strip())
|
|
return symbols or DEFAULT_TRADING_PLAN_SYMBOLS
|
|
|
|
|
|
def build_trade_plan_candidate_timeframe_set_from_env(env=None) -> dict:
|
|
env = os.environ if env is None else env
|
|
setup_tf = str(env.get("AI_ICT_SETUP_TIMEFRAME") or env.get("TRADE_PLAN_CANDIDATE_SETUP_TIMEFRAME") or "1m")
|
|
trigger_tf = str(env.get("AI_ICT_TRIGGER_TIMEFRAME") or env.get("TRADE_PLAN_CANDIDATE_TRIGGER_TIMEFRAME") or setup_tf)
|
|
return {
|
|
"setup_tf": setup_tf,
|
|
"trigger_tf": trigger_tf,
|
|
}
|
|
|
|
|
|
def main(env=None) -> int:
|
|
env = os.environ if env is None else env
|
|
output_json = _get_bool(env, "TRADE_PLAN_CANDIDATES_OUTPUT_JSON", False)
|
|
symbols = build_trade_plan_candidate_symbols_from_env(env=env)
|
|
timeframe_set = build_trade_plan_candidate_timeframe_set_from_env(env=env)
|
|
service = SetupWatcherService()
|
|
candidates = []
|
|
|
|
try:
|
|
for symbol in symbols:
|
|
instrument = service.repository.fetch_instrument_by_symbol(symbol)
|
|
if not instrument:
|
|
candidates.append({"symbol": symbol, "passed": False, "error": "instrument_not_found"})
|
|
continue
|
|
candidate = run_trading_watch(
|
|
instrument_id=int(instrument["id"]),
|
|
setup_tf=timeframe_set["setup_tf"],
|
|
trigger_tf=timeframe_set["trigger_tf"],
|
|
)
|
|
candidates.append(
|
|
build_candidate_result(
|
|
symbol=symbol,
|
|
instrument_id=int(instrument["id"]),
|
|
candidate_id=int((candidate or {}).get("id") or 0),
|
|
candidate=candidate,
|
|
)
|
|
)
|
|
except SQLAlchemyError as exc:
|
|
payload = {
|
|
"passed": False,
|
|
"symbols": list(symbols),
|
|
"timeframe_set": timeframe_set,
|
|
"error_type": exc.__class__.__name__,
|
|
"error": str(exc),
|
|
}
|
|
if output_json:
|
|
print(json.dumps(payload, default=str, ensure_ascii=False, sort_keys=True))
|
|
else:
|
|
print(f"Database unavailable for trade plan candidate sample: {exc.__class__.__name__}")
|
|
return 2
|
|
|
|
payload = {
|
|
"passed": all(candidate.get("passed") for candidate in candidates),
|
|
"symbols": list(symbols),
|
|
"timeframe_set": timeframe_set,
|
|
"candidates": candidates,
|
|
}
|
|
if output_json:
|
|
print(json.dumps(payload, default=str, ensure_ascii=False, sort_keys=True))
|
|
else:
|
|
for candidate in candidates:
|
|
print(json.dumps(candidate, default=str, ensure_ascii=False, sort_keys=True))
|
|
return 0 if payload["passed"] else 2
|
|
|
|
|
|
def build_candidate_result(symbol: str, instrument_id: int, candidate_id: int, candidate) -> dict:
|
|
if candidate is None:
|
|
return {
|
|
"symbol": symbol,
|
|
"instrument_id": instrument_id,
|
|
"passed": True,
|
|
"candidate_generated": False,
|
|
"reason": "active_plan_missing",
|
|
"candidate_id": 0,
|
|
"candidate_rows_written": 0,
|
|
}
|
|
if isinstance(candidate, dict) and candidate.get("candidate_generated") is False:
|
|
return {
|
|
"symbol": symbol,
|
|
"instrument_id": instrument_id,
|
|
"passed": True,
|
|
"candidate_generated": False,
|
|
"reason": candidate.get("status") or "market_truth_blocked",
|
|
"reason_codes": list(candidate.get("reason_codes") or []),
|
|
"candidate_id": 0,
|
|
"candidate_rows_written": 0,
|
|
}
|
|
confirmation_state = dict((candidate or {}).get("confirmation_state") or {})
|
|
return {
|
|
"symbol": symbol,
|
|
"instrument_id": instrument_id,
|
|
"passed": True,
|
|
"candidate_generated": True,
|
|
"candidate_id": candidate_id,
|
|
"candidate_rows_written": 1 if candidate_id else 0,
|
|
"trading_plan_id": candidate.get("trading_plan_id"),
|
|
"model_code": candidate.get("model_code"),
|
|
"status": candidate.get("status"),
|
|
"executable": bool(confirmation_state.get("executable")),
|
|
"missing_confirmations": list(candidate.get("missing_conditions") or candidate.get("missing_confirmations") or []),
|
|
"reason_codes": list(confirmation_state.get("reason_codes") or []),
|
|
}
|
|
|
|
|
|
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())
|