AI ICT
AI ICT 交易辅助系统第一阶段工程实现。
当前项目已经具备从数据、结构、信号、执行、回测到报告的最小闭环,并提供轻量查询 API、HTTP 调试服务与一键编排入口。
See docs/ for architecture, database, rule, and migration references.
Database connectivity defaults to a 5 second PostgreSQL connect timeout. Override with DATABASE_CONNECT_TIMEOUT when needed.
Set MIGRATIONS_OUTPUT_JSON=1 when running run_migrations.py for a machine-readable migration summary.
Current closed loop
当前主线可运行链路:
OKX -> candles -> market_sessions -> swings -> dealing_ranges -> liquidity_pools -> sweep_events -> displacement_events -> MSS -> FVG -> bias_snapshots -> trade_signals -> trade_executions -> backtest_runs -> backtest_results -> reports
Runtime utility scripts
python3 scripts/run_dev.py- run the scaffold entrypointpython3 scripts/show_models.py- list exported ORM model namespython3 scripts/check_db.py- run a simple database connectivity checkpython3 scripts/run_migrations.py- apply SQL files inmigrations/orderpython3 scripts/run_okx_backfill.py- fetch and write sample OKX candlespython3 scripts/run_session_generator.py- generate market sessionspython3 scripts/run_swings.py- rebuild swingspython3 scripts/run_ranges.py- rebuild dealing rangespython3 scripts/run_liquidity.py- rebuild liquidity poolspython3 scripts/run_sweeps.py- rebuild sweep eventspython3 scripts/run_displacement.py- rebuild displacement eventspython3 scripts/run_mss.py- rebuild MSS structure eventspython3 scripts/run_fvg.py- rebuild FVG zonespython3 scripts/run_signals.py- generate bias and signalspython3 scripts/run_execution.py- run paper execution gating, create an execution record, then auto-settle the latest execution for that signalpython3 scripts/run_backtest.py- run backtest and write resultspython3 scripts/run_autonomous_cycle.py- run the unattended cycle: offline checks, database probe, readiness checks, then guarded pipeline when the database is available; printsNext actionsfor the next unattended steppython3 scripts/run_database_readiness.py- run read-only database readiness checks for connection, schema, seed data, and candle availability; printsnext_actionsandaction_planin JSON mode.venv/bin/python scripts/run_offline_checks.py- run the broader offline check suite, including the Training Case package smoke registered astraining_case_package.venv/bin/python scripts/run_training_case_package.py --input tests/fixtures/training_case_package_m6.json --output /tmp/training_case_public_export.json- run the Training Case offline package smoke, an import/export smoke with local files, and write public output as the public export in the public package shape; exported public JSON omits internal solution detailspython3 scripts/run_migration_static_checks.py- check migration order and required core schema without connecting to the databasepython3 scripts/run_script_static_checks.py- check automation scripts for unsafe ambient environment fallback patternspython3 scripts/run_replay_file.py <candles.json|candles.jsonl|candles.csv>- run offline no-lookahead candle replay without database accesspython3 scripts/run_reports.py- build sample reports from current servicespython3 scripts/run_api_server.py- start a lightweight HTTP API server on127.0.0.1:8000and open/for the minimal dashboardpython3 scripts/run_pipeline.py- run the current end-to-end closed loop and print a structured summary
Optional report environment variables used by run_reports.py:
REPORTS_OUTPUT_JSON- set to1,true,yes, oronfor machine-readable sample report output
Optional utility JSON output variables:
CHECK_DB_OUTPUT_JSONCHECK_DB_CONNECTION_OUTPUT_JSONMIGRATION_CHECKS_OUTPUT_JSONSCRIPT_CHECKS_OUTPUT_JSONSHOW_MODELS_OUTPUT_JSON
Optional structure script JSON output variables:
SESSION_GENERATOR_OUTPUT_JSONSWINGS_OUTPUT_JSONRANGES_OUTPUT_JSONLIQUIDITY_OUTPUT_JSONSWEEPS_OUTPUT_JSONDISPLACEMENT_OUTPUT_JSONMSS_OUTPUT_JSONFVG_OUTPUT_JSON
Optional backtest cost environment variables used by run_backtest.py and run_pipeline.py:
BACKTEST_FEE_RATEBACKTEST_SLIPPAGE_RATEBACKTEST_FIXED_COST_R
Optional backtest automation gate used by run_backtest.py and run_pipeline.py:
BACKTEST_OUTPUT_JSON- set to1,true,yes, oronfor machine-readablerun_backtest.pyoutputBACKTEST_REQUIRE_QUALITY_GATE- set to1,true,yes, oronto exit with code2when the backtest quality gate failsBACKTEST_MIN_TOTAL_SIGNALSBACKTEST_MIN_EXECUTED_SIGNALSBACKTEST_MIN_EXPECTANCY_RBACKTEST_MIN_PROFIT_FACTOR_RBACKTEST_MAX_DRAWDOWN_RBACKTEST_MAX_CONSECUTIVE_LOSSES
Optional pipeline environment variables used by run_pipeline.py:
PIPELINE_OUTPUT_JSON- set to1,true,yes, oronfor machine-readable pipeline error output
Optional replay environment variables used by run_replay_file.py:
OFFLINE_REPLAY_FILE- optional candle file path used byrun_offline_checks.pyto include replay validationOFFLINE_CHECKS_OUTPUT_JSON- set to1,true,yes, oronfor machine-readable offline check summary outputREPLAY_LEFT_BARSREPLAY_RIGHT_BARSREPLAY_MIN_CANDLESREPLAY_MIN_SWINGSREPLAY_MAX_GAP_MINUTESREPLAY_OUTPUT_JSON- set to1,true,yes, oronfor machine-readable replay summary output
Example offline replay gate:
OFFLINE_REPLAY_FILE=tests/fixtures/offline_replay_sample.jsonl \
REPLAY_LEFT_BARS=1 REPLAY_RIGHT_BARS=1 \
REPLAY_MIN_CANDLES=5 REPLAY_MIN_SWINGS=2 \
.venv/bin/python scripts/run_offline_checks.py
Optional OKX backfill environment variables used by run_okx_backfill.py:
OKX_BACKFILL_SYMBOL- defaults toBTC-USDTOKX_BACKFILL_INST_ID- defaults to<symbol>-SWAPOKX_BACKFILL_PAGES- defaults to20OKX_BACKFILL_LIMIT- defaults to100OKX_BACKFILL_TIMEFRAMES- defaults to1m,5m,15mOKX_BACKFILL_OUTPUT_JSON- emit a machine-readable backfill summary
Optional autonomous cycle environment variables used by run_autonomous_cycle.py:
AUTONOMOUS_OUTPUT_JSON- emit a machine-readable cycle summaryAUTONOMOUS_REQUIRE_DB- return code2when the database is unavailableAUTONOMOUS_RUN_PIPELINE- set to0to stop after a successful database probeAUTONOMOUS_MAX_DB_PROBES- number of database probe attempts before stoppingAUTONOMOUS_PROBE_INTERVAL_SECONDS- sleep interval between database probe attemptsAUTONOMOUS_REQUIRE_SCHEMA- enabled by default; set to0to skip core table readiness checks before the guarded pipelineAUTONOMOUS_APPLY_MIGRATIONS- enabled by default; when schema is not ready, run migrations, recheck schema, then continueAUTONOMOUS_REQUIRE_DATA_READINESS- enabled by default; set to0to skip read-only seed/candle readiness before the guarded pipelineAUTONOMOUS_BACKFILL_CANDLES- enabled by default; when readiness only lacks candles, run OKX backfill, recheck readiness, then continue- The guarded pipeline step defaults
BACKTEST_REQUIRE_QUALITY_GATE=1andPIPELINE_OUTPUT_JSON=1unless explicitly overridden
Current autonomous modes emit machine-readable next_actions and action_plan, including:
waiting_for_database->wait_for_database_recoverydatabase_schema_not_ready->run_migrations_before_pipelinemigration_failed->inspect_migration_output_and_schemadatabase_readiness_failed-> the readiness script's own next action, such asrun_okx_backfill_or_load_replay_samplebackfill_failed->inspect_backfill_output_and_data_readinesspipeline_failed->inspect_pipeline_output_and_quality_gates
Each action_plan item includes the action name, suggested command, description, and requires_database.
Optional database readiness environment variables used by run_database_readiness.py:
READINESS_OUTPUT_JSONREADINESS_REQUIRED_VENUESREADINESS_REQUIRED_INSTRUMENTSREADINESS_CANDLE_INSTRUMENT_IDREADINESS_CANDLE_TIMEFRAMEREADINESS_MIN_CANDLES- defaults to50READINESS_MIN_CANDLE_SPAN_MINUTES- defaults to60READINESS_MAX_CANDLE_AGE_MINUTES- defaults to240
Optional account risk environment variables used by run_execution.py and run_pipeline.py:
EXECUTION_OUTPUT_JSON- set to1,true,yes, oronfor machine-readablerun_execution.pyoutputEXECUTION_SETTLEMENT_OUTPUT_JSON- set to1,true,yes, oronfor machine-readablerun_execution_settlement.pyoutputEXECUTION_KILL_SWITCHEXECUTION_DAILY_PNL_PCTEXECUTION_CONSECUTIVE_LOSSESEXECUTION_OPEN_POSITIONSEXECUTION_SAME_SIDE_OPEN_POSITIONSEXECUTION_MAX_DAILY_LOSS_PCTEXECUTION_MAX_CONSECUTIVE_LOSSESEXECUTION_MAX_OPEN_POSITIONSEXECUTION_MAX_SAME_SIDE_POSITIONS
Optional signal quality environment variables used by run_signals.py and run_pipeline.py:
SIGNALS_OUTPUT_JSON- set to1,true,yes, oronfor machine-readablerun_signals.pyoutputSIGNAL_EVIDENCE_MODE-minimalby default, orstrictto require sweep/displacement/liquidity evidence before building a signal
Lightweight API helpers
The src/api/ package exposes internal query helpers and HTTP routes for:
GET /healthGET /candles?instrument_id=1&timeframe=1m&limit=100GET /sessions?instrument_id=1&limit=20GET /signals?instrument_id=1&limit=20GET /executions?instrument_id=1&limit=20GET /backtests?instrument_id=1&limit=10
Open / in the browser for the minimal dashboard, which now includes health, candles, signals, sessions, executions, and backtests.
Validation
Unit tests:
python3 -m unittest discover -s tests/unit -p "test_*.py"