quantnodes 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- QuantNodes/__init__.py +15 -0
- QuantNodes/__main__.py +14 -0
- QuantNodes/agent/__init__.py +158 -0
- QuantNodes/agent/agents/__init__.py +13 -0
- QuantNodes/agent/agents/definition.py +180 -0
- QuantNodes/agent/agents/manager.py +73 -0
- QuantNodes/agent/config/__init__.py +34 -0
- QuantNodes/agent/config/executor.py +958 -0
- QuantNodes/agent/config/loader.py +427 -0
- QuantNodes/agent/config/templates/bollinger_bands.yaml +84 -0
- QuantNodes/agent/config/templates/dual_ma.yaml +72 -0
- QuantNodes/agent/config/templates/empty.yaml +56 -0
- QuantNodes/agent/config/templates/mean_reversion.yaml +47 -0
- QuantNodes/agent/config/templates/mean_reversion_zscore.yaml +90 -0
- QuantNodes/agent/config/templates/momentum.yaml +81 -0
- QuantNodes/agent/config/templates/momentum_breakout.yaml +84 -0
- QuantNodes/agent/config/templates/rsi_strategy.yaml +72 -0
- QuantNodes/agent/config/templates/volume_price.yaml +86 -0
- QuantNodes/agent/config/types.py +156 -0
- QuantNodes/agent/config_mapper.py +293 -0
- QuantNodes/agent/core/__init__.py +19 -0
- QuantNodes/agent/core/dream.py +47 -0
- QuantNodes/agent/core/quant_dream.py +274 -0
- QuantNodes/agent/cron_jobs.py +314 -0
- QuantNodes/agent/nanobot_bridge.py +242 -0
- QuantNodes/agent/permission/__init__.py +30 -0
- QuantNodes/agent/permission/defaults.py +36 -0
- QuantNodes/agent/permission/evaluate.py +41 -0
- QuantNodes/agent/permission/models.py +59 -0
- QuantNodes/agent/permission/service.py +133 -0
- QuantNodes/agent/providers/__init__.py +11 -0
- QuantNodes/agent/providers/base.py +102 -0
- QuantNodes/agent/providers/quantnodes.py +610 -0
- QuantNodes/agent/providers/rate_limiter.py +326 -0
- QuantNodes/agent/providers/registry.py +163 -0
- QuantNodes/agent/skills/__init__.py +20 -0
- QuantNodes/agent/skills/base.py +118 -0
- QuantNodes/agent/skills/bridge.py +73 -0
- QuantNodes/agent/skills/factor/__init__.py +14 -0
- QuantNodes/agent/skills/factor/correlation.py +99 -0
- QuantNodes/agent/skills/factor/group_backtest.py +114 -0
- QuantNodes/agent/skills/factor/ic_analysis.py +106 -0
- QuantNodes/agent/skills/loader.py +107 -0
- QuantNodes/agent/skills/registry.py +105 -0
- QuantNodes/agent/skills/strategy/__init__.py +16 -0
- QuantNodes/agent/skills/strategy/bollinger.py +86 -0
- QuantNodes/agent/skills/strategy/dual_ma.py +82 -0
- QuantNodes/agent/skills/strategy/momentum.py +74 -0
- QuantNodes/agent/skills/strategy/rsi_reversal.py +99 -0
- QuantNodes/agent/skills_quant/__init__.py +14 -0
- QuantNodes/agent/skills_quant/backtest-analyze/SKILL.md +42 -0
- QuantNodes/agent/skills_quant/config-driven/SKILL.md +72 -0
- QuantNodes/agent/skills_quant/factor-research/SKILL.md +40 -0
- QuantNodes/agent/skills_quant/quant-dream/SKILL.md +55 -0
- QuantNodes/agent/skills_quant/risk-management/SKILL.md +45 -0
- QuantNodes/agent/skills_quant/strategy-design/SKILL.md +43 -0
- QuantNodes/agent/templates/__init__.py +4 -0
- QuantNodes/agent/tools/__init__.py +173 -0
- QuantNodes/agent/tools/_workspace.py +51 -0
- QuantNodes/agent/tools/alpha_backtest.py +328 -0
- QuantNodes/agent/tools/alpha_evaluate.py +493 -0
- QuantNodes/agent/tools/backtest.py +226 -0
- QuantNodes/agent/tools/base.py +133 -0
- QuantNodes/agent/tools/code_search.py +207 -0
- QuantNodes/agent/tools/config_backtest.py +401 -0
- QuantNodes/agent/tools/context.py +97 -0
- QuantNodes/agent/tools/dream_skill.py +77 -0
- QuantNodes/agent/tools/echo.py +38 -0
- QuantNodes/agent/tools/factor.py +231 -0
- QuantNodes/agent/tools/file_ops.py +201 -0
- QuantNodes/agent/tools/git_ops.py +190 -0
- QuantNodes/agent/tools/operator_lookup.py +218 -0
- QuantNodes/agent/tools/output_truncation.py +77 -0
- QuantNodes/agent/tools/path_check.py +43 -0
- QuantNodes/agent/tools/pipeline.py +62 -0
- QuantNodes/agent/tools/registry.py +150 -0
- QuantNodes/agent/tools/sandbox.py +62 -0
- QuantNodes/agent/tools/shell_safety.py +63 -0
- QuantNodes/agent/tools/strategy.py +106 -0
- QuantNodes/agent/tools/task.py +171 -0
- QuantNodes/agent/tools/web_fetch.py +142 -0
- QuantNodes/agent/tools/web_search.py +114 -0
- QuantNodes/agent/tools/wiki.py +370 -0
- QuantNodes/agent/utils/__init__.py +11 -0
- QuantNodes/agent/utils/helpers.py +43 -0
- QuantNodes/agent/utils/prompt_templates.py +30 -0
- QuantNodes/agent/workflows/__init__.py +20 -0
- QuantNodes/agent/workflows/implementations/__init__.py +8 -0
- QuantNodes/agent/workflows/implementations/alpha_gpt.py +508 -0
- QuantNodes/agent/workflows/implementations/mcts.py +442 -0
- QuantNodes/agent/workflows/parsers.py +44 -0
- QuantNodes/agent/workflows/registry.py +119 -0
- QuantNodes/agent/workflows/step_agent.py +219 -0
- QuantNodes/agent/workflows/tool.py +198 -0
- QuantNodes/ai/__init__.py +93 -0
- QuantNodes/ai/llm/__init__.py +75 -0
- QuantNodes/ai/llm/base.py +233 -0
- QuantNodes/ai/llm/decorators.py +281 -0
- QuantNodes/ai/llm/gateway.py +571 -0
- QuantNodes/ai/llm/null.py +76 -0
- QuantNodes/ai/llm/openai.py +435 -0
- QuantNodes/ai/optimizer.py +405 -0
- QuantNodes/ai/prompts/__init__.py +229 -0
- QuantNodes/ai/sandbox.py +371 -0
- QuantNodes/ai/sandbox_pandas_bridge.py +150 -0
- QuantNodes/ai/strategy_gen.py +396 -0
- QuantNodes/backtest/__init__.py +64 -0
- QuantNodes/backtest/backtest_node.py +188 -0
- QuantNodes/backtest/broker_node.py +378 -0
- QuantNodes/backtest/config_runner.py +397 -0
- QuantNodes/backtest/config_strategy.py +64 -0
- QuantNodes/backtest/risk_node.py +360 -0
- QuantNodes/backtest/strategy_node.py +268 -0
- QuantNodes/cache_node/__init__.py +19 -0
- QuantNodes/cache_node/base.py +244 -0
- QuantNodes/cache_node/cache_store.py +99 -0
- QuantNodes/cache_node/metadata.py +100 -0
- QuantNodes/cli/__init__.py +109 -0
- QuantNodes/cli/_helpers.py +511 -0
- QuantNodes/cli/command.py +110 -0
- QuantNodes/cli/commands/__init__.py +69 -0
- QuantNodes/cli/commands/agent.py +158 -0
- QuantNodes/cli/commands/alpha.py +951 -0
- QuantNodes/cli/commands/chat.py +38 -0
- QuantNodes/cli/commands/evolve.py +120 -0
- QuantNodes/cli/commands/factor.py +569 -0
- QuantNodes/cli/commands/init.py +190 -0
- QuantNodes/cli/commands/run.py +259 -0
- QuantNodes/cli/commands/serve.py +398 -0
- QuantNodes/cli/commands/version.py +120 -0
- QuantNodes/cli/enhanced.py +146 -0
- QuantNodes/conf_node/__init__.py +37 -0
- QuantNodes/conf_node/base.py +120 -0
- QuantNodes/conf_node/env_config.py +132 -0
- QuantNodes/conf_node/ini_config.py +70 -0
- QuantNodes/conf_node/json_config.py +69 -0
- QuantNodes/conf_node/yaml_config.py +78 -0
- QuantNodes/constants.py +17 -0
- QuantNodes/core/__init__.py +196 -0
- QuantNodes/core/_lookback_helpers.py +49 -0
- QuantNodes/core/ast_parser.py +198 -0
- QuantNodes/core/base.py +61 -0
- QuantNodes/core/cache_manager.py +344 -0
- QuantNodes/core/cache_utils.py +150 -0
- QuantNodes/core/cond_builder.py +53 -0
- QuantNodes/core/config.py +170 -0
- QuantNodes/core/constants.py +48 -0
- QuantNodes/core/control.py +412 -0
- QuantNodes/core/data_preprocessing.py +453 -0
- QuantNodes/core/data_source.py +46 -0
- QuantNodes/core/events.py +178 -0
- QuantNodes/core/evolution/__init__.py +22 -0
- QuantNodes/core/evolution/loop.py +583 -0
- QuantNodes/core/evolution/operators.py +289 -0
- QuantNodes/core/evolution/settings.py +44 -0
- QuantNodes/core/expression.py +841 -0
- QuantNodes/core/feedback/__init__.py +38 -0
- QuantNodes/core/feedback/channels.py +182 -0
- QuantNodes/core/feedback/collector.py +91 -0
- QuantNodes/core/feedback/dataclass.py +239 -0
- QuantNodes/core/feedback/llm_judge.py +138 -0
- QuantNodes/core/knowledge/__init__.py +69 -0
- QuantNodes/core/knowledge/knowledge_base.py +217 -0
- QuantNodes/core/knowledge/lineage_compress.py +196 -0
- QuantNodes/core/knowledge/lineage_expand.py +123 -0
- QuantNodes/core/knowledge/metrics/__init__.py +43 -0
- QuantNodes/core/knowledge/metrics/evaluator.py +176 -0
- QuantNodes/core/knowledge/metrics/metrics.py +220 -0
- QuantNodes/core/knowledge/rag_prompt.py +196 -0
- QuantNodes/core/knowledge/retriever.py +209 -0
- QuantNodes/core/lambda_node.py +81 -0
- QuantNodes/core/monitoring/__init__.py +22 -0
- QuantNodes/core/monitoring/collector.py +292 -0
- QuantNodes/core/monitoring/dashboard.py +365 -0
- QuantNodes/core/node.py +375 -0
- QuantNodes/core/pandas_utils.py +504 -0
- QuantNodes/core/parallel/__init__.py +15 -0
- QuantNodes/core/parallel/worker.py +140 -0
- QuantNodes/core/parallel/worker_process.py +265 -0
- QuantNodes/core/path_utils.py +73 -0
- QuantNodes/core/pipeline.py +328 -0
- QuantNodes/core/plugin.py +135 -0
- QuantNodes/core/quality_gate/__init__.py +32 -0
- QuantNodes/core/quality_gate/complexity.py +94 -0
- QuantNodes/core/quality_gate/consistency.py +26 -0
- QuantNodes/core/quality_gate/node.py +97 -0
- QuantNodes/core/quality_gate/redundancy.py +51 -0
- QuantNodes/core/quality_gate/settings.py +43 -0
- QuantNodes/core/quality_gate/zoo.py +98 -0
- QuantNodes/core/serializable.py +116 -0
- QuantNodes/core/serialization.py +673 -0
- QuantNodes/core/tools.py +333 -0
- QuantNodes/core/trajectory/__init__.py +25 -0
- QuantNodes/core/trajectory/entry.py +116 -0
- QuantNodes/core/trajectory/lineage.py +67 -0
- QuantNodes/core/trajectory/pool.py +211 -0
- QuantNodes/core/trajectory/selector.py +140 -0
- QuantNodes/core/visualization/__init__.py +33 -0
- QuantNodes/core/visualization/builder.py +233 -0
- QuantNodes/core/visualization/gate_breakdown.py +140 -0
- QuantNodes/core/visualization/lineage_dag.py +203 -0
- QuantNodes/core/visualization/metric_distribution.py +125 -0
- QuantNodes/core/visualization/report.py +68 -0
- QuantNodes/database_node/__init__.py +69 -0
- QuantNodes/database_node/base.py +135 -0
- QuantNodes/database_node/clickhouse_node.py +272 -0
- QuantNodes/database_node/csv_node.py +83 -0
- QuantNodes/database_node/duckdb_node.py +86 -0
- QuantNodes/database_node/factory.py +83 -0
- QuantNodes/database_node/mysql_node.py +100 -0
- QuantNodes/database_node/parquet_node.py +75 -0
- QuantNodes/database_node/sqlite_node.py +67 -0
- QuantNodes/factor_node/__init__.py +50 -0
- QuantNodes/factor_node/factor.py +563 -0
- QuantNodes/factor_node/factor_db.py +421 -0
- QuantNodes/factor_node/factor_functions/__init__.py +252 -0
- QuantNodes/factor_node/factor_functions/_helpers.py +358 -0
- QuantNodes/factor_node/factor_functions/_helpers_debug.py +317 -0
- QuantNodes/factor_node/factor_functions/composite_ops.py +136 -0
- QuantNodes/factor_node/factor_functions/math_ops.py +433 -0
- QuantNodes/factor_node/factor_functions/section_ops.py +290 -0
- QuantNodes/factor_node/factor_functions/talib_ops.py +1293 -0
- QuantNodes/factor_node/factor_functions/time_ops.py +535 -0
- QuantNodes/factor_node/factor_operation.py +1115 -0
- QuantNodes/factor_node/factor_table.py +1073 -0
- QuantNodes/factor_node/quant_nodes_object.py +60 -0
- QuantNodes/mcp_server/__init__.py +27 -0
- QuantNodes/mcp_server/__main__.py +4 -0
- QuantNodes/mcp_server/server.py +272 -0
- QuantNodes/methods/__init__.py +28 -0
- QuantNodes/methods/pipeline.py +100 -0
- QuantNodes/methods/sandbox.py +102 -0
- QuantNodes/monitor/__init__.py +27 -0
- QuantNodes/monitor/agent_tools/__init__.py +5 -0
- QuantNodes/monitor/agent_tools/monitor_tool.py +98 -0
- QuantNodes/monitor/agent_tools/schedule_tool.py +98 -0
- QuantNodes/monitor/agent_tools/version_tool.py +133 -0
- QuantNodes/monitor/monitor/__init__.py +6 -0
- QuantNodes/monitor/monitor/alerter.py +60 -0
- QuantNodes/monitor/monitor/collector.py +164 -0
- QuantNodes/monitor/monitor/dashboard.py +115 -0
- QuantNodes/monitor/monitor/drift.py +190 -0
- QuantNodes/monitor/scheduler/__init__.py +4 -0
- QuantNodes/monitor/scheduler/runner.py +133 -0
- QuantNodes/monitor/scheduler/scheduler.py +184 -0
- QuantNodes/monitor/storage/__init__.py +16 -0
- QuantNodes/monitor/storage/models.py +70 -0
- QuantNodes/monitor/storage/repository.py +407 -0
- QuantNodes/monitor/version/__init__.py +4 -0
- QuantNodes/monitor/version/diff.py +81 -0
- QuantNodes/monitor/version/version_manager.py +182 -0
- QuantNodes/operator_node/__init__.py +28 -0
- QuantNodes/operator_node/base.py +97 -0
- QuantNodes/operator_node/query_node.py +129 -0
- QuantNodes/operator_node/sql_builder.py +125 -0
- QuantNodes/operator_node/sql_utils.py +172 -0
- QuantNodes/operator_node/transform.py +130 -0
- QuantNodes/operators/__init__.py +90 -0
- QuantNodes/operators/_engine.py +108 -0
- QuantNodes/operators/composite.py +161 -0
- QuantNodes/operators/composite_dag.py +667 -0
- QuantNodes/operators/composite_dag_ops.py +343 -0
- QuantNodes/operators/composite_dag_pandas_ops.py +382 -0
- QuantNodes/operators/custom.py +408 -0
- QuantNodes/operators/facade.py +164 -0
- QuantNodes/operators/math.py +163 -0
- QuantNodes/operators/proxy.py +29 -0
- QuantNodes/operators/registry.py +144 -0
- QuantNodes/operators/section.py +99 -0
- QuantNodes/operators/talib.py +757 -0
- QuantNodes/operators/templates.py +95 -0
- QuantNodes/operators/time_series.py +136 -0
- QuantNodes/prompts/__init__.py +20 -0
- QuantNodes/prompts/backtest/__init__.py +12 -0
- QuantNodes/prompts/backtest/factor_based.py +86 -0
- QuantNodes/prompts/backtest/standard.py +73 -0
- QuantNodes/prompts/factor/__init__.py +14 -0
- QuantNodes/prompts/factor/correlation.py +77 -0
- QuantNodes/prompts/factor/group_backtest.py +86 -0
- QuantNodes/prompts/factor/ic_analysis.py +91 -0
- QuantNodes/prompts/strategy/__init__.py +18 -0
- QuantNodes/prompts/strategy/market_neutral.py +96 -0
- QuantNodes/prompts/strategy/mean_reversion.py +107 -0
- QuantNodes/prompts/strategy/momentum.py +160 -0
- QuantNodes/prompts/strategy/pairs_trading.py +107 -0
- QuantNodes/prompts/strategy/trend_following.py +96 -0
- QuantNodes/research/README.md +106 -0
- QuantNodes/research/__init__.py +154 -0
- QuantNodes/research/_legacy_3c/__init__.py +61 -0
- QuantNodes/research/_legacy_3c/auto_researcher.py +289 -0
- QuantNodes/research/_legacy_3c/factor_evaluator.py +560 -0
- QuantNodes/research/_legacy_3c/factor_miner.py +318 -0
- QuantNodes/research/_legacy_3c/mcts_search.py +324 -0
- QuantNodes/research/factor_test/__init__.py +25 -0
- QuantNodes/research/factor_test/config.py +184 -0
- QuantNodes/research/factor_test/config_builder.py +276 -0
- QuantNodes/research/factor_test/e2e/data_prep.py +163 -0
- QuantNodes/research/factor_test/e2e/run_evolution_e2e.py +309 -0
- QuantNodes/research/factor_test/evolution_adapter.py +231 -0
- QuantNodes/research/factor_test/feedback_wrapper.py +102 -0
- QuantNodes/research/factor_test/ifind_db/__init__.py +7 -0
- QuantNodes/research/factor_test/ifind_db/fetcher.py +224 -0
- QuantNodes/research/factor_test/ifind_db/ifind_database.py +689 -0
- QuantNodes/research/factor_test/nodes/__init__.py +1 -0
- QuantNodes/research/factor_test/nodes/_base.py +91 -0
- QuantNodes/research/factor_test/nodes/adjust_date_node.py +48 -0
- QuantNodes/research/factor_test/nodes/configs.py +240 -0
- QuantNodes/research/factor_test/nodes/factor_neutralize_node.py +87 -0
- QuantNodes/research/factor_test/nodes/factor_preprocess_node.py +222 -0
- QuantNodes/research/factor_test/nodes/factor_score_node.py +141 -0
- QuantNodes/research/factor_test/nodes/factor_test_report_node.py +153 -0
- QuantNodes/research/factor_test/nodes/group_analyzer_node.py +317 -0
- QuantNodes/research/factor_test/nodes/ic_analyzer_node.py +112 -0
- QuantNodes/research/factor_test/nodes/load_data_node.py +100 -0
- QuantNodes/research/factor_test/nodes/long_short_node.py +93 -0
- QuantNodes/research/factor_test/nodes/neutralizers.py +222 -0
- QuantNodes/research/factor_test/nodes/preprocess_strategies.py +277 -0
- QuantNodes/research/factor_test/nodes/risk_correlation_node.py +112 -0
- QuantNodes/research/factor_test/nodes/sample_pool_filter_node.py +110 -0
- QuantNodes/research/factor_test/nodes/tradability_filter_node.py +92 -0
- QuantNodes/research/factor_test/pipeline_runner.py +305 -0
- QuantNodes/research/factor_test/pipeline_spec.py +216 -0
- QuantNodes/research/factor_test/utils/__init__.py +26 -0
- QuantNodes/research/factor_test/utils/constants.py +86 -0
- QuantNodes/research/factor_test/utils/data_loader.py +141 -0
- QuantNodes/research/factor_test/utils/date_utils.py +232 -0
- QuantNodes/research/factor_test/utils/file_loaders.py +150 -0
- QuantNodes/research/factor_test/utils/labels.py +37 -0
- QuantNodes/research/factor_test/utils/metrics_extractor.py +55 -0
- QuantNodes/research/factor_test/utils/performance_metrics.py +175 -0
- QuantNodes/research/factor_test/utils/safe_load.py +106 -0
- QuantNodes/research/quant_alpha/CHANGELOG.md +80 -0
- QuantNodes/research/quant_alpha/README.md +142 -0
- QuantNodes/research/quant_alpha/__init__.py +45 -0
- QuantNodes/research/quant_alpha/adapters/__init__.py +99 -0
- QuantNodes/research/quant_alpha/adapters/calculator.py +503 -0
- QuantNodes/research/quant_alpha/adapters/expression.py +387 -0
- QuantNodes/research/quant_alpha/alpha101_design/__init__.py +50 -0
- QuantNodes/research/quant_alpha/alpha101_design/few_shot_examples.py +243 -0
- QuantNodes/research/quant_alpha/alpha101_design/philosophy.py +474 -0
- QuantNodes/research/quant_alpha/alpha158_design/__init__.py +63 -0
- QuantNodes/research/quant_alpha/alpha158_design/few_shot_examples.py +219 -0
- QuantNodes/research/quant_alpha/alpha158_design/philosophy.py +240 -0
- QuantNodes/research/quant_alpha/evaluation/__init__.py +47 -0
- QuantNodes/research/quant_alpha/evaluation/baselines/__init__.py +8 -0
- QuantNodes/research/quant_alpha/evaluation/baselines/g1_handcrafted.py +135 -0
- QuantNodes/research/quant_alpha/evaluation/baselines/g2_llm_only.py +269 -0
- QuantNodes/research/quant_alpha/evaluation/baselines/g3_alpha_gpt.py +152 -0
- QuantNodes/research/quant_alpha/evaluation/clickhouse_data_loader.py +227 -0
- QuantNodes/research/quant_alpha/evaluation/contracts.py +376 -0
- QuantNodes/research/quant_alpha/evaluation/evaluators/__init__.py +6 -0
- QuantNodes/research/quant_alpha/evaluation/evaluators/polars_evaluator.py +545 -0
- QuantNodes/research/quant_alpha/evaluation/mock_data_loader.py +226 -0
- QuantNodes/research/quant_alpha/evaluation/runner.py +243 -0
- QuantNodes/research/quant_alpha/llm/__init__.py +38 -0
- QuantNodes/research/quant_alpha/llm/parser.py +681 -0
- QuantNodes/research/quant_alpha/logic_driven_pipeline.py +411 -0
- QuantNodes/research/quant_alpha/logic_mining/__init__.py +74 -0
- QuantNodes/research/quant_alpha/logic_mining/compiler.py +457 -0
- QuantNodes/research/quant_alpha/logic_mining/generator.py +366 -0
- QuantNodes/research/quant_alpha/logic_mining/models.py +252 -0
- QuantNodes/research/quant_alpha/logic_mining/parser.py +287 -0
- QuantNodes/research/quant_alpha/logic_mining/pipelines.py +297 -0
- QuantNodes/research/quant_alpha/logic_mining/sources.py +149 -0
- QuantNodes/research/quant_alpha/mcts/__init__.py +66 -0
- QuantNodes/research/quant_alpha/mcts/cache.py +262 -0
- QuantNodes/research/quant_alpha/mcts/extension_ops.py +320 -0
- QuantNodes/research/quant_alpha/mcts/feedback.py +825 -0
- QuantNodes/research/quant_alpha/mcts/op_prior.py +180 -0
- QuantNodes/research/quant_alpha/mcts/search.py +540 -0
- QuantNodes/research/quant_alpha/mcts/tree.py +201 -0
- QuantNodes/research/quant_alpha/operator_vocab/__init__.py +50 -0
- QuantNodes/research/quant_alpha/operator_vocab/config.py +54 -0
- QuantNodes/research/quant_alpha/operator_vocab/metadata.py +263 -0
- QuantNodes/research/quant_alpha/operator_vocab/vocabulary.py +481 -0
- QuantNodes/research/quant_alpha/pipeline.py +1027 -0
- QuantNodes/research/quant_alpha/types/__init__.py +27 -0
- QuantNodes/research/quant_alpha/types/constants.py +28 -0
- QuantNodes/research/quant_alpha/types/state.py +205 -0
- QuantNodes/research/quant_alpha/workflow/__init__.py +32 -0
- QuantNodes/research/quant_alpha/workflow/alpha_gpt.py +911 -0
- QuantNodes/research/quant_alpha/workflow/alpha_logics.py +416 -0
- QuantNodes/research/quant_alpha/workflow/state.py +27 -0
- QuantNodes/research/report_reproducer.py +485 -0
- QuantNodes/research/wiki.py +1155 -0
- QuantNodes/symbolic/__init__.py +51 -0
- QuantNodes/symbolic/compiler.py +113 -0
- QuantNodes/symbolic/dialect.py +260 -0
- QuantNodes/symbolic/executor.py +147 -0
- QuantNodes/symbolic/expression.py +234 -0
- QuantNodes/symbolic/functions.py +433 -0
- QuantNodes/symbolic/optimizer.py +165 -0
- QuantNodes/ui_node/__init__.py +30 -0
- QuantNodes/ui_node/base.py +222 -0
- quantnodes-3.0.0.dist-info/METADATA +463 -0
- quantnodes-3.0.0.dist-info/RECORD +399 -0
- quantnodes-3.0.0.dist-info/WHEEL +5 -0
- quantnodes-3.0.0.dist-info/entry_points.txt +24 -0
- quantnodes-3.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
"""
|
|
3
|
+
因子挖掘器 - 模板枚举 + 公式生成
|
|
4
|
+
|
|
5
|
+
基于预定义模板库,系统性生成候选因子公式。
|
|
6
|
+
支持4大类因子族:动量、均值回归、波动率、量价。
|
|
7
|
+
|
|
8
|
+
⚠️ DeprecationWarning (v2.7.0+, since 2026-06-23):
|
|
9
|
+
本模块进入 deprecation 周期。新代码请迁移到
|
|
10
|
+
`QuantNodes.research.quant_alpha.operator_vocab.OperatorVocab`。
|
|
11
|
+
|
|
12
|
+
迁移理由:
|
|
13
|
+
- 模板硬编码 10 个算子 → 162 算子动态查询
|
|
14
|
+
- 公式生成器与评估器解耦更清晰
|
|
15
|
+
- 支持 LLM 友好的元数据(Alpha-GPT 路线需要)
|
|
16
|
+
|
|
17
|
+
Phase 时间表:
|
|
18
|
+
- Phase A (current): 本文件仍可用,行为完全兼容
|
|
19
|
+
- Phase B (v2.9+): 本类变 thin wrapper
|
|
20
|
+
- Phase C (v3.0): 归档到 _legacy_3c/
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
import hashlib
|
|
26
|
+
import random
|
|
27
|
+
import warnings
|
|
28
|
+
from dataclasses import dataclass, field
|
|
29
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
30
|
+
|
|
31
|
+
from QuantNodes.research.wiki import FactorCategory
|
|
32
|
+
|
|
33
|
+
warnings.warn(
|
|
34
|
+
"QuantNodes.research.factor_miner 已弃用 (DeprecationWarning)。"
|
|
35
|
+
"请迁移到 QuantNodes.research.quant_alpha.operator_vocab.OperatorVocab。",
|
|
36
|
+
DeprecationWarning,
|
|
37
|
+
stacklevel=2,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass
|
|
42
|
+
class FactorCandidate:
|
|
43
|
+
"""候选因子"""
|
|
44
|
+
name: str
|
|
45
|
+
formula: str
|
|
46
|
+
description: str
|
|
47
|
+
operators_used: List[str]
|
|
48
|
+
category: FactorCategory
|
|
49
|
+
template_name: str = ""
|
|
50
|
+
metadata: Dict[str, Any] = field(default_factory=dict)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass
|
|
54
|
+
class TemplateEntry:
|
|
55
|
+
"""模板条目"""
|
|
56
|
+
formula_pattern: str
|
|
57
|
+
description_pattern: str
|
|
58
|
+
required_ops: List[str]
|
|
59
|
+
n_cols: int # 需要的输入列数 (1 或 2)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
# 模板库
|
|
63
|
+
TEMPLATES: Dict[str, Dict[str, Any]] = {
|
|
64
|
+
"momentum": {
|
|
65
|
+
"category": FactorCategory.MOMENTUM,
|
|
66
|
+
"description": "动量因子",
|
|
67
|
+
"entries": [
|
|
68
|
+
TemplateEntry(
|
|
69
|
+
formula_pattern="ts_delta({col}, {w})",
|
|
70
|
+
description_pattern="{col} 的 {w} 期动量",
|
|
71
|
+
required_ops=["ts_delta"],
|
|
72
|
+
n_cols=1,
|
|
73
|
+
),
|
|
74
|
+
TemplateEntry(
|
|
75
|
+
formula_pattern="ts_pct_change({col}, {w})",
|
|
76
|
+
description_pattern="{col} 的 {w} 期涨跌幅",
|
|
77
|
+
required_ops=["ts_pct_change"],
|
|
78
|
+
n_cols=1,
|
|
79
|
+
),
|
|
80
|
+
TemplateEntry(
|
|
81
|
+
formula_pattern="ts_mean({col}, {w}) / ts_std({col}, {w})",
|
|
82
|
+
description_pattern="{col} 的 {w} 期夏普比",
|
|
83
|
+
required_ops=["ts_mean", "ts_std"],
|
|
84
|
+
n_cols=1,
|
|
85
|
+
),
|
|
86
|
+
TemplateEntry(
|
|
87
|
+
formula_pattern="{col} / ts_lag({col}, {w}) - 1",
|
|
88
|
+
description_pattern="{col} 的 {w} 期收益率",
|
|
89
|
+
required_ops=["ts_lag"],
|
|
90
|
+
n_cols=1,
|
|
91
|
+
),
|
|
92
|
+
TemplateEntry(
|
|
93
|
+
formula_pattern="rank(ts_delta({col}, {w}))",
|
|
94
|
+
description_pattern="{col} 的 {w} 期动量排名",
|
|
95
|
+
required_ops=["rank", "ts_delta"],
|
|
96
|
+
n_cols=1,
|
|
97
|
+
),
|
|
98
|
+
],
|
|
99
|
+
},
|
|
100
|
+
"mean_reversion": {
|
|
101
|
+
"category": FactorCategory.OTHER,
|
|
102
|
+
"description": "均值回归因子",
|
|
103
|
+
"entries": [
|
|
104
|
+
TemplateEntry(
|
|
105
|
+
formula_pattern="({col} - ts_mean({col}, {w})) / ts_std({col}, {w})",
|
|
106
|
+
description_pattern="{col} 的 {w} 期 Z-Score",
|
|
107
|
+
required_ops=["ts_mean", "ts_std"],
|
|
108
|
+
n_cols=1,
|
|
109
|
+
),
|
|
110
|
+
TemplateEntry(
|
|
111
|
+
formula_pattern="{col} / ts_mean({col}, {w}) - 1",
|
|
112
|
+
description_pattern="{col} 相对 {w} 期均值偏离",
|
|
113
|
+
required_ops=["ts_mean"],
|
|
114
|
+
n_cols=1,
|
|
115
|
+
),
|
|
116
|
+
TemplateEntry(
|
|
117
|
+
formula_pattern="rank({col} / ts_mean({col}, {w}) - 1)",
|
|
118
|
+
description_pattern="{col} 的均值回归排名",
|
|
119
|
+
required_ops=["rank", "ts_mean"],
|
|
120
|
+
n_cols=1,
|
|
121
|
+
),
|
|
122
|
+
],
|
|
123
|
+
},
|
|
124
|
+
"volatility": {
|
|
125
|
+
"category": FactorCategory.VOLATILITY,
|
|
126
|
+
"description": "波动率因子",
|
|
127
|
+
"entries": [
|
|
128
|
+
TemplateEntry(
|
|
129
|
+
formula_pattern="ts_std({col}, {w})",
|
|
130
|
+
description_pattern="{col} 的 {w} 期波动率",
|
|
131
|
+
required_ops=["ts_std"],
|
|
132
|
+
n_cols=1,
|
|
133
|
+
),
|
|
134
|
+
TemplateEntry(
|
|
135
|
+
formula_pattern="ts_std({col}, {w}) / ts_mean({col}, {w})",
|
|
136
|
+
description_pattern="{col} 的 {w} 期变异系数",
|
|
137
|
+
required_ops=["ts_std", "ts_mean"],
|
|
138
|
+
n_cols=1,
|
|
139
|
+
),
|
|
140
|
+
TemplateEntry(
|
|
141
|
+
formula_pattern="ts_max({col}, {w}) - ts_min({col}, {w})",
|
|
142
|
+
description_pattern="{col} 的 {w} 期振幅",
|
|
143
|
+
required_ops=["ts_max", "ts_min"],
|
|
144
|
+
n_cols=1,
|
|
145
|
+
),
|
|
146
|
+
TemplateEntry(
|
|
147
|
+
formula_pattern="rank(ts_std({col}, {w}))",
|
|
148
|
+
description_pattern="{col} 的波动率排名",
|
|
149
|
+
required_ops=["rank", "ts_std"],
|
|
150
|
+
n_cols=1,
|
|
151
|
+
),
|
|
152
|
+
],
|
|
153
|
+
},
|
|
154
|
+
"volume_price": {
|
|
155
|
+
"category": FactorCategory.OTHER,
|
|
156
|
+
"description": "量价因子",
|
|
157
|
+
"entries": [
|
|
158
|
+
TemplateEntry(
|
|
159
|
+
formula_pattern="ts_corr({col1}, {col2}, {w})",
|
|
160
|
+
description_pattern="{col1} 与 {col2} 的 {w} 期相关性",
|
|
161
|
+
required_ops=["ts_corr"],
|
|
162
|
+
n_cols=2,
|
|
163
|
+
),
|
|
164
|
+
TemplateEntry(
|
|
165
|
+
formula_pattern="ts_cov({col1}, {col2}, {w})",
|
|
166
|
+
description_pattern="{col1} 与 {col2} 的 {w} 期协方差",
|
|
167
|
+
required_ops=["ts_cov"],
|
|
168
|
+
n_cols=2,
|
|
169
|
+
),
|
|
170
|
+
TemplateEntry(
|
|
171
|
+
formula_pattern="rank(ts_corr({col1}, {col2}, {w}))",
|
|
172
|
+
description_pattern="{col1} 与 {col2} 的相关性排名",
|
|
173
|
+
required_ops=["rank", "ts_corr"],
|
|
174
|
+
n_cols=2,
|
|
175
|
+
),
|
|
176
|
+
],
|
|
177
|
+
},
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
# 单列输入组合
|
|
181
|
+
SINGLE_COL_COMBOS = [
|
|
182
|
+
(["close"],),
|
|
183
|
+
(["open"],),
|
|
184
|
+
(["high"],),
|
|
185
|
+
(["low"],),
|
|
186
|
+
(["vol"],),
|
|
187
|
+
]
|
|
188
|
+
|
|
189
|
+
# 双列输入组合
|
|
190
|
+
DUAL_COL_COMBOS = [
|
|
191
|
+
(["close", "vol"],),
|
|
192
|
+
(["close", "open"],),
|
|
193
|
+
(["high", "low"],),
|
|
194
|
+
(["close", "high"],),
|
|
195
|
+
(["vol", "close"],),
|
|
196
|
+
]
|
|
197
|
+
|
|
198
|
+
# 默认窗口期
|
|
199
|
+
DEFAULT_WINDOWS = [5, 10, 20, 60]
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _make_factor_name(formula: str) -> str:
|
|
203
|
+
"""根据公式生成确定性因子名"""
|
|
204
|
+
h = hashlib.md5(formula.encode()).hexdigest()[:8]
|
|
205
|
+
return f"auto_{h}"
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
class FactorMiner:
|
|
209
|
+
"""模板因子挖掘器"""
|
|
210
|
+
|
|
211
|
+
def __init__(self, seed: int = 42):
|
|
212
|
+
self.rng = random.Random(seed)
|
|
213
|
+
|
|
214
|
+
def generate(
|
|
215
|
+
self,
|
|
216
|
+
available_columns: List[str],
|
|
217
|
+
config: Any = None,
|
|
218
|
+
) -> List[FactorCandidate]:
|
|
219
|
+
"""生成候选因子列表
|
|
220
|
+
|
|
221
|
+
Args:
|
|
222
|
+
available_columns: 数据中可用的列名
|
|
223
|
+
config: MiningConfig (可选)
|
|
224
|
+
|
|
225
|
+
Returns:
|
|
226
|
+
候选因子列表
|
|
227
|
+
"""
|
|
228
|
+
windows = getattr(config, "windows", DEFAULT_WINDOWS) if config else DEFAULT_WINDOWS
|
|
229
|
+
categories = getattr(config, "template_categories", None) if config else None
|
|
230
|
+
max_factors = getattr(config, "max_factors", 100) if config else 100
|
|
231
|
+
|
|
232
|
+
candidates = []
|
|
233
|
+
|
|
234
|
+
for template_name, template_group in TEMPLATES.items():
|
|
235
|
+
if categories and template_name not in categories:
|
|
236
|
+
continue
|
|
237
|
+
|
|
238
|
+
category = template_group["category"]
|
|
239
|
+
entries = template_group["entries"]
|
|
240
|
+
|
|
241
|
+
for entry in entries:
|
|
242
|
+
col_combos = (
|
|
243
|
+
SINGLE_COL_COMBOS if entry.n_cols == 1 else DUAL_COL_COMBOS
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
for combo in col_combos:
|
|
247
|
+
cols = combo[0]
|
|
248
|
+
|
|
249
|
+
# 检查列是否可用
|
|
250
|
+
if not all(c in available_columns for c in cols):
|
|
251
|
+
continue
|
|
252
|
+
|
|
253
|
+
for w in windows:
|
|
254
|
+
formula, desc = self._fill_template(
|
|
255
|
+
entry, cols, w
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
candidate = FactorCandidate(
|
|
259
|
+
name=_make_factor_name(formula),
|
|
260
|
+
formula=formula,
|
|
261
|
+
description=desc,
|
|
262
|
+
operators_used=list(entry.required_ops),
|
|
263
|
+
category=category,
|
|
264
|
+
template_name=template_name,
|
|
265
|
+
metadata={"window": w, "columns": cols},
|
|
266
|
+
)
|
|
267
|
+
candidates.append(candidate)
|
|
268
|
+
|
|
269
|
+
# 去重 (同一公式可能通过不同路径生成)
|
|
270
|
+
seen = set()
|
|
271
|
+
unique = []
|
|
272
|
+
for c in candidates:
|
|
273
|
+
if c.formula not in seen:
|
|
274
|
+
seen.add(c.formula)
|
|
275
|
+
unique.append(c)
|
|
276
|
+
|
|
277
|
+
# 限制数量
|
|
278
|
+
if len(unique) > max_factors:
|
|
279
|
+
unique = self.rng.sample(unique, max_factors)
|
|
280
|
+
|
|
281
|
+
return unique
|
|
282
|
+
|
|
283
|
+
def _fill_template(
|
|
284
|
+
self, entry: TemplateEntry, cols: List[str], window: int
|
|
285
|
+
) -> Tuple[str, str]:
|
|
286
|
+
"""填充模板"""
|
|
287
|
+
placeholders = {
|
|
288
|
+
"col": cols[0],
|
|
289
|
+
"w": str(window),
|
|
290
|
+
}
|
|
291
|
+
if entry.n_cols == 2 and len(cols) >= 2:
|
|
292
|
+
placeholders["col1"] = cols[0]
|
|
293
|
+
placeholders["col2"] = cols[1]
|
|
294
|
+
|
|
295
|
+
formula = entry.formula_pattern
|
|
296
|
+
desc = entry.description_pattern
|
|
297
|
+
for k, v in placeholders.items():
|
|
298
|
+
formula = formula.replace("{" + k + "}", v)
|
|
299
|
+
desc = desc.replace("{" + k + "}", v)
|
|
300
|
+
|
|
301
|
+
return formula, desc
|
|
302
|
+
|
|
303
|
+
def generate_single(
|
|
304
|
+
self,
|
|
305
|
+
formula: str,
|
|
306
|
+
description: str = "",
|
|
307
|
+
operators_used: Optional[List[str]] = None,
|
|
308
|
+
category: FactorCategory = FactorCategory.OTHER,
|
|
309
|
+
) -> FactorCandidate:
|
|
310
|
+
"""手动创建单个候选因子"""
|
|
311
|
+
return FactorCandidate(
|
|
312
|
+
name=_make_factor_name(formula),
|
|
313
|
+
formula=formula,
|
|
314
|
+
description=description or f"手动因子: {formula}",
|
|
315
|
+
operators_used=operators_used or [],
|
|
316
|
+
category=category,
|
|
317
|
+
template_name="manual",
|
|
318
|
+
)
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
"""
|
|
3
|
+
MCTS 搜索树 - 蒙特卡洛树搜索因子挖掘
|
|
4
|
+
|
|
5
|
+
基于 Alpha Jungle 论文思路:
|
|
6
|
+
- 把候选因子组织成搜索树
|
|
7
|
+
- 每个节点有多维评价
|
|
8
|
+
- UCB1 选择策略
|
|
9
|
+
- 维度化反馈指导扩展方向
|
|
10
|
+
- 频繁子树规避
|
|
11
|
+
|
|
12
|
+
⚠️ DeprecationWarning (v2.7.0+, since 2026-06-23):
|
|
13
|
+
本模块进入 deprecation 周期。新代码请迁移到
|
|
14
|
+
`QuantNodes.research.quant_alpha.mcts.MCTSSearch` (M2 PR)。
|
|
15
|
+
|
|
16
|
+
迁移理由:
|
|
17
|
+
- 7 硬编码扩展操作 → 从 OperatorVocab 动态生成
|
|
18
|
+
- 加 5 通道反馈(execution/shape/code/value/llm)
|
|
19
|
+
- 加谱系追踪(parent_id → entry_id)
|
|
20
|
+
|
|
21
|
+
Phase 时间表:
|
|
22
|
+
- Phase A (current): 本文件仍可用,行为完全兼容
|
|
23
|
+
- Phase B (M2+): 本类变 thin wrapper
|
|
24
|
+
- Phase C (v3.0): 归档到 _legacy_3c/
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
from __future__ import annotations
|
|
28
|
+
|
|
29
|
+
import math
|
|
30
|
+
import random
|
|
31
|
+
import warnings
|
|
32
|
+
from dataclasses import dataclass, field
|
|
33
|
+
from typing import Any, Dict, List, Optional
|
|
34
|
+
|
|
35
|
+
import polars as pl
|
|
36
|
+
|
|
37
|
+
warnings.warn(
|
|
38
|
+
"QuantNodes.research.mcts_search 已弃用 (DeprecationWarning)。"
|
|
39
|
+
"M2 PR 将提供新实现 QuantNodes.research.quant_alpha.mcts.MCTSSearch。",
|
|
40
|
+
DeprecationWarning,
|
|
41
|
+
stacklevel=2,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
from QuantNodes.research._legacy_3c.factor_evaluator import (
|
|
45
|
+
EvalConfig,
|
|
46
|
+
FactorEvaluationResult,
|
|
47
|
+
FactorEvaluator,
|
|
48
|
+
)
|
|
49
|
+
from QuantNodes.research._legacy_3c.factor_miner import FactorCandidate
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@dataclass
|
|
53
|
+
class MCTSNode:
|
|
54
|
+
"""MCTS 搜索树节点"""
|
|
55
|
+
formula: str
|
|
56
|
+
parent: Optional[MCTSNode] = None
|
|
57
|
+
children: List[MCTSNode] = field(default_factory=list)
|
|
58
|
+
visits: int = 0
|
|
59
|
+
dimension_scores: Dict[str, float] = field(default_factory=dict)
|
|
60
|
+
overall_score: float = 0.0
|
|
61
|
+
is_expanded: bool = False
|
|
62
|
+
metadata: Dict[str, Any] = field(default_factory=dict)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
# 扩展操作: (算子模板, 参数范围)
|
|
66
|
+
EXTENSION_OPS = [
|
|
67
|
+
# 包裹型: rank(...), zscore(...)
|
|
68
|
+
("rank({f})", "rank"),
|
|
69
|
+
("zscore({f})", "zscore"),
|
|
70
|
+
# 窗口型: ts_mean(..., w), ts_std(..., w)
|
|
71
|
+
("ts_mean({f}, {w})", "ts_mean"),
|
|
72
|
+
("ts_std({f}, {w})", "ts_std"),
|
|
73
|
+
("ts_delta({f}, {w})", "ts_delta"),
|
|
74
|
+
# 差值型: {f} - ts_mean({f}, w)
|
|
75
|
+
("{f} - ts_mean({f}, {w})", "mean_diff"),
|
|
76
|
+
# 比值型: {f} / ts_lag({f}, w)
|
|
77
|
+
("{f} / ts_lag({f}, {w}) - 1", "return"),
|
|
78
|
+
]
|
|
79
|
+
|
|
80
|
+
WINDOWS = [5, 10, 20, 60]
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class MCTSSearch:
|
|
84
|
+
"""MCTS 因子搜索
|
|
85
|
+
|
|
86
|
+
基于蒙特卡洛树搜索,在因子公式空间中寻找高质量因子。
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
def __init__(
|
|
90
|
+
self,
|
|
91
|
+
evaluator: FactorEvaluator = None,
|
|
92
|
+
eval_config: EvalConfig = None,
|
|
93
|
+
exploration_weight: float = 1.414,
|
|
94
|
+
seed: int = 42,
|
|
95
|
+
):
|
|
96
|
+
self.evaluator = evaluator or FactorEvaluator(eval_config)
|
|
97
|
+
self.config = eval_config or EvalConfig()
|
|
98
|
+
self.exploration_weight = exploration_weight
|
|
99
|
+
self.rng = random.Random(seed)
|
|
100
|
+
self._formula_cache: Dict[str, FactorEvaluationResult] = {}
|
|
101
|
+
|
|
102
|
+
def search(
|
|
103
|
+
self,
|
|
104
|
+
data: pl.DataFrame,
|
|
105
|
+
seed_formulas: Optional[List[str]] = None,
|
|
106
|
+
iterations: int = 50,
|
|
107
|
+
date_column: str = "date",
|
|
108
|
+
code_column: str = "code",
|
|
109
|
+
forward_return_column: str = "forward_return",
|
|
110
|
+
) -> List[FactorEvaluationResult]:
|
|
111
|
+
"""执行 MCTS 搜索
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
data: 行情数据
|
|
115
|
+
seed_formulas: 种子公式 (可选, 从已有因子开始搜索)
|
|
116
|
+
iterations: 迭代次数
|
|
117
|
+
date_column: 日期列名
|
|
118
|
+
code_column: 股票代码列名
|
|
119
|
+
forward_return_column: 前瞻收益率列名
|
|
120
|
+
|
|
121
|
+
Returns:
|
|
122
|
+
搜索到的高质量因子列表
|
|
123
|
+
"""
|
|
124
|
+
# 创建根节点
|
|
125
|
+
root = MCTSNode(formula="__ROOT__")
|
|
126
|
+
|
|
127
|
+
# 添加种子节点
|
|
128
|
+
if seed_formulas:
|
|
129
|
+
for formula in seed_formulas:
|
|
130
|
+
child = MCTSNode(formula=formula, parent=root)
|
|
131
|
+
root.children.append(child)
|
|
132
|
+
|
|
133
|
+
# MCTS 主循环
|
|
134
|
+
for _ in range(iterations):
|
|
135
|
+
# 1. SELECT: 选择最有潜力的节点
|
|
136
|
+
node = self._select(root)
|
|
137
|
+
|
|
138
|
+
# 2. EXPAND: 生成子节点
|
|
139
|
+
if not node.is_expanded:
|
|
140
|
+
child = self._expand(node, data)
|
|
141
|
+
if child is None:
|
|
142
|
+
continue
|
|
143
|
+
node = child
|
|
144
|
+
|
|
145
|
+
# 3. EVALUATE: 多维度评分
|
|
146
|
+
scores = self._evaluate(
|
|
147
|
+
node, data, date_column, code_column, forward_return_column
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
# 4. BACKUP: 回传评分
|
|
151
|
+
self._backpropagate(node, scores)
|
|
152
|
+
|
|
153
|
+
# 收集结果
|
|
154
|
+
results = self._collect_results(root)
|
|
155
|
+
return results
|
|
156
|
+
|
|
157
|
+
def _select(self, node: MCTSNode) -> MCTSNode:
|
|
158
|
+
"""UCB1 选择策略"""
|
|
159
|
+
while node.is_expanded and node.children:
|
|
160
|
+
node = max(node.children, key=lambda n: self._ucb1(n))
|
|
161
|
+
return node
|
|
162
|
+
|
|
163
|
+
def _ucb1(self, node: MCTSNode) -> float:
|
|
164
|
+
"""UCB1 公式"""
|
|
165
|
+
if node.visits == 0:
|
|
166
|
+
return float("inf")
|
|
167
|
+
|
|
168
|
+
exploit = node.overall_score
|
|
169
|
+
explore = self.exploration_weight * math.sqrt(
|
|
170
|
+
math.log(node.parent.visits + 1) / node.visits
|
|
171
|
+
)
|
|
172
|
+
return exploit + explore
|
|
173
|
+
|
|
174
|
+
def _expand(
|
|
175
|
+
self, node: MCTSNode, data: pl.DataFrame
|
|
176
|
+
) -> Optional[MCTSNode]:
|
|
177
|
+
"""扩展节点: 生成子公式"""
|
|
178
|
+
if node.formula == "__ROOT__":
|
|
179
|
+
# 根节点: 生成种子公式
|
|
180
|
+
seed_formulas = self._generate_seed_formulas(data)
|
|
181
|
+
if not seed_formulas:
|
|
182
|
+
return None
|
|
183
|
+
formula = self.rng.choice(seed_formulas)
|
|
184
|
+
else:
|
|
185
|
+
# 非根节点: 应用扩展操作
|
|
186
|
+
formula = self._apply_extension(node.formula)
|
|
187
|
+
if formula is None:
|
|
188
|
+
return None
|
|
189
|
+
|
|
190
|
+
# 检查是否已评估过
|
|
191
|
+
if formula in self._formula_cache:
|
|
192
|
+
return None
|
|
193
|
+
|
|
194
|
+
# 检查公式复杂度 (避免过深嵌套)
|
|
195
|
+
depth = formula.count("(") - formula.count(")")
|
|
196
|
+
if depth > 5:
|
|
197
|
+
return None
|
|
198
|
+
|
|
199
|
+
child = MCTSNode(formula=formula, parent=node)
|
|
200
|
+
node.children.append(child)
|
|
201
|
+
node.is_expanded = True
|
|
202
|
+
return child
|
|
203
|
+
|
|
204
|
+
def _evaluate(
|
|
205
|
+
self,
|
|
206
|
+
node: MCTSNode,
|
|
207
|
+
data: pl.DataFrame,
|
|
208
|
+
date_column: str,
|
|
209
|
+
code_column: str,
|
|
210
|
+
forward_return_column: str,
|
|
211
|
+
) -> Dict[str, float]:
|
|
212
|
+
"""评估节点"""
|
|
213
|
+
# 创建候选因子
|
|
214
|
+
candidate = FactorCandidate(
|
|
215
|
+
name=f"mcts_{hash(node.formula) % 10000:04d}",
|
|
216
|
+
formula=node.formula,
|
|
217
|
+
description=f"MCTS 搜索因子: {node.formula}",
|
|
218
|
+
operators_used=[],
|
|
219
|
+
category=type("C", (), {"value": "other"})(),
|
|
220
|
+
template_name="mcts",
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
# 评估
|
|
224
|
+
result = self.evaluator.evaluate(
|
|
225
|
+
candidate=candidate,
|
|
226
|
+
data=data,
|
|
227
|
+
date_column=date_column,
|
|
228
|
+
code_column=code_column,
|
|
229
|
+
forward_return_column=forward_return_column,
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
# 缓存结果
|
|
233
|
+
self._formula_cache[node.formula] = result
|
|
234
|
+
|
|
235
|
+
# 提取维度评分
|
|
236
|
+
node.dimension_scores = result.dimension_scores
|
|
237
|
+
node.overall_score = result.overall_score
|
|
238
|
+
node.visits += 1
|
|
239
|
+
|
|
240
|
+
return result.dimension_scores
|
|
241
|
+
|
|
242
|
+
def _backpropagate(self, node: MCTSNode, scores: Dict[str, float]):
|
|
243
|
+
"""回传评分到父节点"""
|
|
244
|
+
current = node
|
|
245
|
+
while current.parent is not None:
|
|
246
|
+
current = current.parent
|
|
247
|
+
current.visits += 1
|
|
248
|
+
|
|
249
|
+
# 更新父节点的综合评分 (子节点最高分)
|
|
250
|
+
if current.children:
|
|
251
|
+
best_child = max(current.children, key=lambda c: c.overall_score)
|
|
252
|
+
current.overall_score = best_child.overall_score
|
|
253
|
+
|
|
254
|
+
def _generate_seed_formulas(self, data: pl.DataFrame) -> List[str]:
|
|
255
|
+
"""生成种子公式"""
|
|
256
|
+
available_cols = [
|
|
257
|
+
c for c in data.columns
|
|
258
|
+
if c not in ("date", "code", "forward_return")
|
|
259
|
+
]
|
|
260
|
+
|
|
261
|
+
formulas = []
|
|
262
|
+
for col in available_cols[:5]: # 最多用5列
|
|
263
|
+
for w in [10, 20, 60]:
|
|
264
|
+
formulas.extend([
|
|
265
|
+
f"ts_mean({col}, {w})",
|
|
266
|
+
f"ts_std({col}, {w})",
|
|
267
|
+
f"ts_delta({col}, {w})",
|
|
268
|
+
f"rank({col})",
|
|
269
|
+
f"{col} / ts_lag({col}, {w}) - 1",
|
|
270
|
+
])
|
|
271
|
+
|
|
272
|
+
return formulas
|
|
273
|
+
|
|
274
|
+
def _apply_extension(self, formula: str) -> Optional[str]:
|
|
275
|
+
"""对公式应用扩展操作"""
|
|
276
|
+
ops = list(EXTENSION_OPS)
|
|
277
|
+
self.rng.shuffle(ops)
|
|
278
|
+
|
|
279
|
+
for template, op_name in ops:
|
|
280
|
+
w = self.rng.choice(WINDOWS)
|
|
281
|
+
try:
|
|
282
|
+
new_formula = template.replace("{f}", formula).replace("{w}", str(w))
|
|
283
|
+
# 简单语法检查
|
|
284
|
+
if new_formula.count("(") == new_formula.count(")"):
|
|
285
|
+
return new_formula
|
|
286
|
+
except Exception:
|
|
287
|
+
continue
|
|
288
|
+
|
|
289
|
+
return None
|
|
290
|
+
|
|
291
|
+
def _collect_results(
|
|
292
|
+
self, root: MCTSNode, min_score: float = 0.1
|
|
293
|
+
) -> List[FactorEvaluationResult]:
|
|
294
|
+
"""收集搜索结果"""
|
|
295
|
+
results = []
|
|
296
|
+
self._collect_recursive(root, results, min_score)
|
|
297
|
+
|
|
298
|
+
# 按综合评分排序
|
|
299
|
+
results.sort(key=lambda r: r.overall_score, reverse=True)
|
|
300
|
+
|
|
301
|
+
# 去重
|
|
302
|
+
seen_formulas = set()
|
|
303
|
+
unique = []
|
|
304
|
+
for r in results:
|
|
305
|
+
if r.candidate.formula not in seen_formulas:
|
|
306
|
+
seen_formulas.add(r.candidate.formula)
|
|
307
|
+
unique.append(r)
|
|
308
|
+
|
|
309
|
+
return unique
|
|
310
|
+
|
|
311
|
+
def _collect_recursive(
|
|
312
|
+
self,
|
|
313
|
+
node: MCTSNode,
|
|
314
|
+
results: List[FactorEvaluationResult],
|
|
315
|
+
min_score: float,
|
|
316
|
+
):
|
|
317
|
+
"""递归收集结果"""
|
|
318
|
+
if node.formula in self._formula_cache:
|
|
319
|
+
result = self._formula_cache[node.formula]
|
|
320
|
+
if result.overall_score > min_score:
|
|
321
|
+
results.append(result)
|
|
322
|
+
|
|
323
|
+
for child in node.children:
|
|
324
|
+
self._collect_recursive(child, results, min_score)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
"""
|
|
3
|
+
单因子回测节点化模块 / Single-Factor Backtest Node Module
|
|
4
|
+
|
|
5
|
+
将单因子回测的 12 项能力拆分为独立 Node,通过 Pipeline 组合。
|
|
6
|
+
|
|
7
|
+
用法:
|
|
8
|
+
from QuantNodes.research.factor_test import SingleFactorTestConfig, PipelineRunner
|
|
9
|
+
|
|
10
|
+
config = SingleFactorTestConfig(...)
|
|
11
|
+
runner = PipelineRunner(config)
|
|
12
|
+
result = runner.run()
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from .config import (
|
|
16
|
+
SingleFactorTestConfig, FactorSetting, PreprocessSetting,
|
|
17
|
+
TradableSetting, AnalysisSetting, OutputSetting,
|
|
18
|
+
)
|
|
19
|
+
from .utils import DataLoader
|
|
20
|
+
|
|
21
|
+
__all__ = [
|
|
22
|
+
'SingleFactorTestConfig', 'FactorSetting', 'PreprocessSetting',
|
|
23
|
+
'TradableSetting', 'AnalysisSetting', 'OutputSetting',
|
|
24
|
+
'DataLoader',
|
|
25
|
+
]
|