77 lines
2.5 KiB
Python
77 lines
2.5 KiB
Python
from pathlib import Path
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
if str(ROOT) not in sys.path:
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
from src.services.signals import HistoricalSignalService
|
|
|
|
|
|
def run_historical_signal_generation(service=None, env=None) -> dict:
|
|
env = os.environ if env is None else env
|
|
service = service or HistoricalSignalService()
|
|
return service.generate_and_persist(
|
|
instrument_id=_get_int(env, "HISTORICAL_SIGNALS_INSTRUMENT_ID", 1),
|
|
timeframe=env.get("HISTORICAL_SIGNALS_TIMEFRAME") or "1m",
|
|
candle_limit=_get_int(env, "HISTORICAL_SIGNALS_CANDLE_LIMIT", 5000),
|
|
max_candidates=_get_int(env, "HISTORICAL_SIGNALS_MAX_CANDIDATES", 200),
|
|
min_body_ratio=_get_float(env, "HISTORICAL_SIGNALS_MIN_BODY_RATIO", 1.2),
|
|
body_lookback=_get_int(env, "HISTORICAL_SIGNALS_BODY_LOOKBACK", 50),
|
|
)
|
|
|
|
|
|
def main(env=None) -> int:
|
|
env = os.environ if env is None else env
|
|
output_json = _get_bool(env, "HISTORICAL_SIGNALS_OUTPUT_JSON", False)
|
|
try:
|
|
summary = run_historical_signal_generation(env=env)
|
|
except (SQLAlchemyError, ValueError, RuntimeError, OSError) as exc:
|
|
summary = {
|
|
"passed": False,
|
|
"error_type": exc.__class__.__name__,
|
|
"error": str(exc),
|
|
}
|
|
if output_json:
|
|
print(json.dumps(summary, ensure_ascii=False, sort_keys=True))
|
|
else:
|
|
print(f"Historical signal generation failed: {exc.__class__.__name__}")
|
|
return 2
|
|
|
|
if output_json:
|
|
print(json.dumps(summary, ensure_ascii=False, sort_keys=True))
|
|
else:
|
|
print(f"Historical signal candidates: {summary['candidate_count']}")
|
|
print(f"Historical signal inserted: {summary['inserted']}")
|
|
print(f"Historical signal duplicates: {summary['skipped_duplicates']}")
|
|
return 0
|
|
|
|
|
|
def _get_int(env, key: str, default: int) -> int:
|
|
value = env.get(key)
|
|
if value is None or value == "":
|
|
return default
|
|
return int(value)
|
|
|
|
|
|
def _get_float(env, key: str, default: float) -> float:
|
|
value = env.get(key)
|
|
if value is None or value == "":
|
|
return default
|
|
return float(value)
|
|
|
|
|
|
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())
|