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,535 @@
|
|
|
1
|
+
# coding=utf-8
|
|
2
|
+
"""
|
|
3
|
+
时间序列算子
|
|
4
|
+
|
|
5
|
+
本模块包含所有时间序列相关的因子运算算子。
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import Optional, Union
|
|
11
|
+
|
|
12
|
+
import numpy as np
|
|
13
|
+
import polars as pl
|
|
14
|
+
from polars import Expr
|
|
15
|
+
|
|
16
|
+
from QuantNodes.factor_node.factor_functions._helpers import (
|
|
17
|
+
OperatorCategory,
|
|
18
|
+
register_operator,
|
|
19
|
+
_ensure_expr,
|
|
20
|
+
_expanding_var_expr,
|
|
21
|
+
_apply_weights,
|
|
22
|
+
_cumulative_map_batches_single,
|
|
23
|
+
_cumulative_map_batches_dual,
|
|
24
|
+
_cum_single_quantile,
|
|
25
|
+
_make_rolling_ts_wrapper,
|
|
26
|
+
_make_expanding_wrapper,
|
|
27
|
+
_make_dual_rolling_wrapper,
|
|
28
|
+
_make_diff_wrapper,
|
|
29
|
+
_make_alias,
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
# ==============================================================================
|
|
33
|
+
# 滚动窗口算子 - 工厂生成
|
|
34
|
+
# ==============================================================================
|
|
35
|
+
|
|
36
|
+
_ROLLING_TS_DOCS = {
|
|
37
|
+
"rolling_mean": "滚动窗口均值\n\n Args:\n f: 表达式或列名\n window: 窗口大小\n min_periods: 最小观测数",
|
|
38
|
+
"rolling_max": "滚动窗口最大值",
|
|
39
|
+
"rolling_min": "滚动窗口最小值",
|
|
40
|
+
"rolling_sum": "滚动窗口求和",
|
|
41
|
+
"rolling_median": "滚动窗口中位数",
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
_ROLLING_TS_METHODS = {
|
|
45
|
+
"rolling_mean": "rolling_mean",
|
|
46
|
+
"rolling_max": "rolling_max",
|
|
47
|
+
"rolling_min": "rolling_min",
|
|
48
|
+
"rolling_sum": "rolling_sum",
|
|
49
|
+
"rolling_median": "rolling_median",
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
for _name, _method in _ROLLING_TS_METHODS.items():
|
|
53
|
+
_make_rolling_ts_wrapper(_name, _method, _ROLLING_TS_DOCS[_name])
|
|
54
|
+
|
|
55
|
+
del _name, _method, _ROLLING_TS_METHODS, _ROLLING_TS_DOCS
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# ==============================================================================
|
|
59
|
+
# 滚动窗口算子 - 显式定义
|
|
60
|
+
# ==============================================================================
|
|
61
|
+
|
|
62
|
+
@register_operator(OperatorCategory.TIME)
|
|
63
|
+
def rolling_std(
|
|
64
|
+
f: Union[Expr, str],
|
|
65
|
+
window: int = 20,
|
|
66
|
+
min_periods: Optional[int] = None,
|
|
67
|
+
ddof: int = 1,
|
|
68
|
+
**kwargs
|
|
69
|
+
) -> Expr:
|
|
70
|
+
"""滚动窗口标准差"""
|
|
71
|
+
e = _ensure_expr(f)
|
|
72
|
+
mp = min_periods or max(1, window // 2)
|
|
73
|
+
return e.rolling_std(window, min_samples=mp, ddof=ddof)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@register_operator(OperatorCategory.TIME)
|
|
77
|
+
def rolling_var(
|
|
78
|
+
f: Union[Expr, str],
|
|
79
|
+
window: int = 20,
|
|
80
|
+
min_periods: Optional[int] = None,
|
|
81
|
+
**kwargs
|
|
82
|
+
) -> Expr:
|
|
83
|
+
"""滚动窗口方差"""
|
|
84
|
+
e = _ensure_expr(f)
|
|
85
|
+
mp = min_periods or max(1, window // 2)
|
|
86
|
+
return e.rolling_var(window, min_samples=mp)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@register_operator(OperatorCategory.TIME)
|
|
90
|
+
def rolling_prod(
|
|
91
|
+
f: Union[Expr, str],
|
|
92
|
+
window: int = 20,
|
|
93
|
+
min_periods: Optional[int] = None,
|
|
94
|
+
**kwargs
|
|
95
|
+
) -> Expr:
|
|
96
|
+
"""滚动窗口求积(log-sum-exp 方法)"""
|
|
97
|
+
e = _ensure_expr(f)
|
|
98
|
+
mp = min_periods or max(1, window // 2)
|
|
99
|
+
return e.log().rolling_sum(window, min_samples=mp).exp()
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@register_operator(OperatorCategory.TIME)
|
|
103
|
+
def rolling_skew(
|
|
104
|
+
f: Union[Expr, str],
|
|
105
|
+
window: int = 20,
|
|
106
|
+
min_periods: Optional[int] = None,
|
|
107
|
+
**kwargs
|
|
108
|
+
) -> Expr:
|
|
109
|
+
"""滚动窗口偏度"""
|
|
110
|
+
e = _ensure_expr(f)
|
|
111
|
+
mp = min_periods or max(1, window // 2)
|
|
112
|
+
mean = e.rolling_mean(window, min_samples=mp)
|
|
113
|
+
std = e.rolling_std(window, min_samples=mp)
|
|
114
|
+
m3 = ((e - mean) ** 3).rolling_mean(window, min_samples=mp)
|
|
115
|
+
return m3 / (std ** 3 + 1e-10)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@register_operator(OperatorCategory.TIME)
|
|
119
|
+
def rolling_kurt(
|
|
120
|
+
f: Union[Expr, str],
|
|
121
|
+
window: int = 20,
|
|
122
|
+
min_periods: Optional[int] = None,
|
|
123
|
+
**kwargs
|
|
124
|
+
) -> Expr:
|
|
125
|
+
"""滚动窗口峰度"""
|
|
126
|
+
e = _ensure_expr(f)
|
|
127
|
+
mp = min_periods or max(1, window // 2)
|
|
128
|
+
mean = e.rolling_mean(window, min_samples=mp)
|
|
129
|
+
std = e.rolling_std(window, min_samples=mp)
|
|
130
|
+
m4 = ((e - mean) ** 4).rolling_mean(window, min_samples=mp)
|
|
131
|
+
return m4 / (std ** 4 + 1e-10) - 3
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
@register_operator(OperatorCategory.TIME)
|
|
135
|
+
def rolling_count(
|
|
136
|
+
f: Union[Expr, str],
|
|
137
|
+
window: int = 20,
|
|
138
|
+
min_periods: Optional[int] = None,
|
|
139
|
+
**kwargs
|
|
140
|
+
) -> Expr:
|
|
141
|
+
"""滚动窗口计数"""
|
|
142
|
+
e = _ensure_expr(f)
|
|
143
|
+
mp = min_periods or max(1, window // 2)
|
|
144
|
+
return e.is_not_null().cast(pl.Int64).rolling_sum(window, min_samples=mp)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
@register_operator(OperatorCategory.TIME)
|
|
148
|
+
def rolling_quantile(
|
|
149
|
+
f: Union[Expr, str],
|
|
150
|
+
window: int = 20,
|
|
151
|
+
quantile: float = 0.5,
|
|
152
|
+
min_periods: Optional[int] = None,
|
|
153
|
+
**kwargs
|
|
154
|
+
) -> Expr:
|
|
155
|
+
"""滚动窗口分位数"""
|
|
156
|
+
e = _ensure_expr(f)
|
|
157
|
+
mp = min_periods or window
|
|
158
|
+
return e.rolling_quantile(quantile, window_size=window, interpolation="nearest", min_samples=mp)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
@register_operator(OperatorCategory.TIME)
|
|
162
|
+
def rolling_rank(
|
|
163
|
+
f: Union[Expr, str],
|
|
164
|
+
window: int = 20,
|
|
165
|
+
min_periods: Optional[int] = None,
|
|
166
|
+
**kwargs
|
|
167
|
+
) -> Expr:
|
|
168
|
+
"""滚动窗口排名(归一化到 0-1)"""
|
|
169
|
+
e = _ensure_expr(f)
|
|
170
|
+
mp = min_periods or window
|
|
171
|
+
return e.rolling_rank(window, min_samples=mp)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
# ==============================================================================
|
|
175
|
+
# Argmax/Argmin 算子
|
|
176
|
+
# ==============================================================================
|
|
177
|
+
|
|
178
|
+
def _rolling_arg_op(f: Union[Expr, str], window: int, op: str,
|
|
179
|
+
min_periods: Optional[int] = None) -> Expr:
|
|
180
|
+
"""滚动窗口最大值/最小值索引,用 map_batches + numpy 实现 O(n·window)"""
|
|
181
|
+
e = _ensure_expr(f)
|
|
182
|
+
window = min(window, 30)
|
|
183
|
+
is_max = op == "max"
|
|
184
|
+
|
|
185
|
+
def _arg_numpy(s: pl.Series) -> pl.Series:
|
|
186
|
+
arr = s.to_numpy()
|
|
187
|
+
n = len(arr)
|
|
188
|
+
result = np.zeros(n, dtype=np.int32)
|
|
189
|
+
for i in range(n):
|
|
190
|
+
start = max(0, i - window + 1)
|
|
191
|
+
idx = np.argmax(arr[start:i + 1]) if is_max else np.argmin(arr[start:i + 1])
|
|
192
|
+
result[i] = idx
|
|
193
|
+
return pl.Series(result)
|
|
194
|
+
|
|
195
|
+
return e.map_batches(_arg_numpy, return_dtype=pl.Int32)
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
@register_operator(OperatorCategory.TIME)
|
|
199
|
+
def rolling_argmax(f: Union[Expr, str], window: int = 20,
|
|
200
|
+
min_periods: Optional[int] = None, **kwargs) -> Expr:
|
|
201
|
+
"""滚动窗口最大值索引"""
|
|
202
|
+
return _rolling_arg_op(f, window, "max", min_periods)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
@register_operator(OperatorCategory.TIME)
|
|
206
|
+
def rolling_argmin(f: Union[Expr, str], window: int = 20,
|
|
207
|
+
min_periods: Optional[int] = None, **kwargs) -> Expr:
|
|
208
|
+
"""滚动窗口最小值索引"""
|
|
209
|
+
return _rolling_arg_op(f, window, "min", min_periods)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
# ==============================================================================
|
|
213
|
+
# 双因子滚动相关/协方差
|
|
214
|
+
# ==============================================================================
|
|
215
|
+
|
|
216
|
+
_make_dual_rolling_wrapper(
|
|
217
|
+
"rolling_corr", True,
|
|
218
|
+
"滚动窗口相关系数(双因子)\n\n Args:\n f1: 第一个表达式\n f2: 第二个表达式\n window: 窗口大小\n min_periods: 最小观测数"
|
|
219
|
+
)
|
|
220
|
+
_make_dual_rolling_wrapper(
|
|
221
|
+
"rolling_cov", False,
|
|
222
|
+
"滚动窗口协方差(双因子)\n\n Args:\n f1: 第一个表达式\n f2: 第二个表达式\n window: 窗口大小\n min_periods: 最小观测数"
|
|
223
|
+
)
|
|
224
|
+
_make_dual_rolling_wrapper(
|
|
225
|
+
"rolling_cov", "ts_cov",
|
|
226
|
+
"滚动窗口协方差(双因子)\n\n Args:\n f1: 第一个表达式\n f2: 第二个表达式\n window: 窗口大小\n min_periods: 最小观测数"
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
# ==============================================================================
|
|
231
|
+
# 别名
|
|
232
|
+
# ==============================================================================
|
|
233
|
+
|
|
234
|
+
_make_alias("ts_corr", rolling_corr, # noqa: F821
|
|
235
|
+
"滚动相关系数 (rolling_corr 别名)")
|
|
236
|
+
_make_alias("ts_cov", rolling_cov, # noqa: F821
|
|
237
|
+
"滚动协方差 (rolling_cov 别名)")
|
|
238
|
+
|
|
239
|
+
_make_alias("correlation", rolling_corr, # noqa: F821
|
|
240
|
+
"相关系数 (rolling_corr 别名)")
|
|
241
|
+
_make_alias("covariance", rolling_cov, # noqa: F821
|
|
242
|
+
"滚动协方差 (rolling_cov 别名)")
|
|
243
|
+
|
|
244
|
+
_make_alias("ts_mean", rolling_mean, # noqa: F821
|
|
245
|
+
"时间序列均值 (滚动均值别名)")
|
|
246
|
+
_make_alias("ts_std", rolling_std,
|
|
247
|
+
"时间序列标准差")
|
|
248
|
+
_make_alias("ts_max", rolling_max, # noqa: F821
|
|
249
|
+
"时间序列最大值")
|
|
250
|
+
_make_alias("ts_min", rolling_min, # noqa: F821
|
|
251
|
+
"时间序列最小值")
|
|
252
|
+
_make_alias("ts_sum", rolling_sum, # noqa: F821
|
|
253
|
+
"时间序列求和")
|
|
254
|
+
_make_alias("ts_median", rolling_median, # noqa: F821
|
|
255
|
+
"时间序列中位数")
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
@register_operator(OperatorCategory.TIME)
|
|
259
|
+
def ts_rank(f: Union[Expr, str], window: int = 20,
|
|
260
|
+
min_periods: Optional[int] = None, **kwargs) -> Expr:
|
|
261
|
+
"""滚动排名 (0-1 归一化)"""
|
|
262
|
+
e = _ensure_expr(f)
|
|
263
|
+
return e.rolling_rank(window)
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
_make_alias("ts_argmax", rolling_argmax,
|
|
267
|
+
"滚动最大值的位置")
|
|
268
|
+
_make_alias("ts_argmin", rolling_argmin,
|
|
269
|
+
"滚动最小值的位置")
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
# ==============================================================================
|
|
273
|
+
# 差分与变化
|
|
274
|
+
# ==============================================================================
|
|
275
|
+
|
|
276
|
+
_make_diff_wrapper("ts_delta", "diff", "差分")
|
|
277
|
+
_make_diff_wrapper("ts_pct_change", "pct_change", "百分比变化")
|
|
278
|
+
_make_diff_wrapper("diff", "diff", "差分")
|
|
279
|
+
_make_diff_wrapper("lag", "shift", "滞后算子")
|
|
280
|
+
|
|
281
|
+
_make_alias("delta", ts_delta, "差分 (ts_delta 别名)") # noqa: F821
|
|
282
|
+
_make_alias("pct_change", ts_pct_change, "百分比变化 (ts_pct_change 别名)") # noqa: F821
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
@register_operator(OperatorCategory.TIME)
|
|
286
|
+
def ts_lag(f: Union[Expr, str], periods: int = 1, **kwargs) -> Expr:
|
|
287
|
+
"""滞后 (向后移动)"""
|
|
288
|
+
e = _ensure_expr(f)
|
|
289
|
+
return e.shift(periods)
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
@register_operator(OperatorCategory.TIME)
|
|
293
|
+
def ts_lead(f: Union[Expr, str], periods: int = 1, **kwargs) -> Expr:
|
|
294
|
+
"""前向移动 (向前移动)"""
|
|
295
|
+
e = _ensure_expr(f)
|
|
296
|
+
return e.shift(-periods)
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
_make_alias("delay", ts_lag, "滞后 (ts_lag 别名)")
|
|
300
|
+
_make_alias("ref", delay, "引用历史值 (delay 别名)") # noqa: F821
|
|
301
|
+
_make_alias("shift", ts_lag, "移动 (shift 别名)")
|
|
302
|
+
_make_alias("ts_shift", ts_lag, "移动 (ts_lag 别名)")
|
|
303
|
+
_make_alias("ts_prod", rolling_prod, "滚动求积 (rolling_prod 别名)")
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
# ==============================================================================
|
|
307
|
+
# 扩展窗口算子
|
|
308
|
+
# ==============================================================================
|
|
309
|
+
|
|
310
|
+
_make_expanding_wrapper("expanding_sum", "cum_sum",
|
|
311
|
+
"扩展窗口求和")
|
|
312
|
+
_make_expanding_wrapper("expanding_max", "cum_max",
|
|
313
|
+
"扩展窗口最大值")
|
|
314
|
+
_make_expanding_wrapper("expanding_min", "cum_min",
|
|
315
|
+
"扩展窗口最小值")
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
@register_operator(OperatorCategory.TIME)
|
|
319
|
+
def expanding_mean(f: Union[Expr, str], min_periods: Optional[int] = None, **kwargs) -> Expr:
|
|
320
|
+
"""扩展窗口均值 (累计至当前)"""
|
|
321
|
+
e = _ensure_expr(f)
|
|
322
|
+
return e.cum_sum() / (pl.int_range(0, pl.len()) + 1)
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
@register_operator(OperatorCategory.TIME)
|
|
326
|
+
def expanding_var(f: Union[Expr, str], min_periods: Optional[int] = None, **kwargs) -> Expr:
|
|
327
|
+
"""扩展窗口方差"""
|
|
328
|
+
return _expanding_var_expr(f)
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
@register_operator(OperatorCategory.TIME)
|
|
332
|
+
def expanding_std(f: Union[Expr, str], min_periods: Optional[int] = None, **kwargs) -> Expr:
|
|
333
|
+
"""扩展窗口标准差"""
|
|
334
|
+
return (_expanding_var_expr(f) + 1e-10).sqrt()
|
|
335
|
+
|
|
336
|
+
|
|
337
|
+
@register_operator(OperatorCategory.TIME)
|
|
338
|
+
def expanding_count(f: Union[Expr, str], min_periods: Optional[int] = None, **kwargs) -> Expr:
|
|
339
|
+
"""扩展窗口计数(非空值累计数量)"""
|
|
340
|
+
return _ensure_expr(f).is_not_null().cast(pl.Int64).cum_sum()
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
@register_operator(OperatorCategory.TIME)
|
|
344
|
+
def expanding_median(f: Union[Expr, str], min_periods: Optional[int] = None, **kwargs) -> Expr:
|
|
345
|
+
"""扩展窗口中位数"""
|
|
346
|
+
return _cumulative_map_batches_single(_ensure_expr(f), "median")
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
@register_operator(OperatorCategory.TIME)
|
|
350
|
+
def expanding_kurt(f: Union[Expr, str], min_periods: Optional[int] = None, **kwargs) -> Expr:
|
|
351
|
+
"""扩展窗口峰度"""
|
|
352
|
+
return _cumulative_map_batches_single(_ensure_expr(f), "kurt")
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
@register_operator(OperatorCategory.TIME)
|
|
356
|
+
def expanding_skew(f: Union[Expr, str], min_periods: Optional[int] = None, **kwargs) -> Expr:
|
|
357
|
+
"""扩展窗口偏度"""
|
|
358
|
+
return _cumulative_map_batches_single(_ensure_expr(f), "skew")
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
@register_operator(OperatorCategory.TIME)
|
|
362
|
+
def expanding_quantile(f: Union[Expr, str], quantile: float = 0.5,
|
|
363
|
+
min_periods: Optional[int] = None, **kwargs) -> Expr:
|
|
364
|
+
"""扩展窗口分位数"""
|
|
365
|
+
|
|
366
|
+
def _cum_quantile(s: pl.Series) -> pl.Series:
|
|
367
|
+
vals = s.to_list()
|
|
368
|
+
result = []
|
|
369
|
+
for i in range(len(vals)):
|
|
370
|
+
window = [v for v in vals[:i + 1] if v is not None]
|
|
371
|
+
if len(window) == 0:
|
|
372
|
+
result.append(None)
|
|
373
|
+
else:
|
|
374
|
+
result.append(_cum_single_quantile(window, quantile))
|
|
375
|
+
return pl.Series(values=result)
|
|
376
|
+
|
|
377
|
+
return _ensure_expr(f).map_batches(_cum_quantile, return_dtype=pl.Float64)
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
@register_operator(OperatorCategory.TIME)
|
|
381
|
+
def expanding_corr(f1: Union[Expr, str], f2: Union[Expr, str],
|
|
382
|
+
min_periods: Optional[int] = None, **kwargs) -> Expr:
|
|
383
|
+
"""扩展窗口相关系数(双因子)"""
|
|
384
|
+
return _cumulative_map_batches_dual(_ensure_expr(f1), _ensure_expr(f2), "corr")
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
@register_operator(OperatorCategory.TIME)
|
|
388
|
+
def expanding_cov(f1: Union[Expr, str], f2: Union[Expr, str],
|
|
389
|
+
min_periods: Optional[int] = None, **kwargs) -> Expr:
|
|
390
|
+
"""扩展窗口协方差(双因子)"""
|
|
391
|
+
return _cumulative_map_batches_dual(_ensure_expr(f1), _ensure_expr(f2), "cov")
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
# ==============================================================================
|
|
395
|
+
# 指数加权算子
|
|
396
|
+
# ==============================================================================
|
|
397
|
+
|
|
398
|
+
@register_operator(OperatorCategory.TIME)
|
|
399
|
+
def ewm_mean(f: Union[Expr, str], alpha: float = 0.5,
|
|
400
|
+
adjust: bool = True, **kwargs) -> Expr:
|
|
401
|
+
"""指数加权移动平均"""
|
|
402
|
+
e = _ensure_expr(f)
|
|
403
|
+
return e.ewm_mean(alpha=alpha, adjust=adjust)
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
@register_operator(OperatorCategory.TIME)
|
|
407
|
+
def ewm_std(f: Union[Expr, str], alpha: float = 0.5,
|
|
408
|
+
adjust: bool = True, **kwargs) -> Expr:
|
|
409
|
+
"""指数加权移动标准差"""
|
|
410
|
+
e = _ensure_expr(f)
|
|
411
|
+
return e.ewm_std(alpha=alpha, adjust=adjust)
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
@register_operator(OperatorCategory.TIME)
|
|
415
|
+
def ewm_corr(f1: Union[Expr, str], f2: Union[Expr, str],
|
|
416
|
+
alpha: float = 0.5, **kwargs) -> Expr:
|
|
417
|
+
"""指数加权相关系数"""
|
|
418
|
+
e1 = _ensure_expr(f1)
|
|
419
|
+
e2 = _ensure_expr(f2)
|
|
420
|
+
mean1 = e1.ewm_mean(alpha=alpha)
|
|
421
|
+
mean2 = e2.ewm_mean(alpha=alpha)
|
|
422
|
+
var1 = e1.ewm_var(alpha=alpha)
|
|
423
|
+
var2 = e2.ewm_var(alpha=alpha)
|
|
424
|
+
cov = (e1 * e2).ewm_mean(alpha=alpha) - mean1 * mean2
|
|
425
|
+
return cov / ((var1 * var2).sqrt() + 1e-10)
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
@register_operator(OperatorCategory.TIME)
|
|
429
|
+
def ewm_var(f: Union[Expr, str], alpha: float = 0.5,
|
|
430
|
+
adjust: bool = True, **kwargs) -> Expr:
|
|
431
|
+
"""指数加权移动方差"""
|
|
432
|
+
e = _ensure_expr(f)
|
|
433
|
+
mean = e.ewm_mean(alpha=alpha, adjust=adjust)
|
|
434
|
+
mean_sq = (e ** 2).ewm_mean(alpha=alpha, adjust=adjust)
|
|
435
|
+
return mean_sq - mean ** 2
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+
@register_operator(OperatorCategory.TIME)
|
|
439
|
+
def ewm_cov(f1: Union[Expr, str], f2: Union[Expr, str],
|
|
440
|
+
alpha: float = 0.5, adjust: bool = True, **kwargs) -> Expr:
|
|
441
|
+
"""指数加权移动协方差(双因子)"""
|
|
442
|
+
e1, e2 = _ensure_expr(f1), _ensure_expr(f2)
|
|
443
|
+
mean1 = e1.ewm_mean(alpha=alpha, adjust=adjust)
|
|
444
|
+
mean2 = e2.ewm_mean(alpha=alpha, adjust=adjust)
|
|
445
|
+
mean12 = (e1 * e2).ewm_mean(alpha=alpha, adjust=adjust)
|
|
446
|
+
return mean12 - mean1 * mean2
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
# ==============================================================================
|
|
450
|
+
# 高级时间序列算子
|
|
451
|
+
# ==============================================================================
|
|
452
|
+
|
|
453
|
+
@register_operator(OperatorCategory.TIME)
|
|
454
|
+
def regress(y: Union[Expr, str], x: Union[Expr, str],
|
|
455
|
+
window: int = 20, **kwargs) -> Expr:
|
|
456
|
+
"""滑动窗口线性回归的残差"""
|
|
457
|
+
ey, ex = _ensure_expr(y), _ensure_expr(x)
|
|
458
|
+
y_mean = ey.rolling_mean(window)
|
|
459
|
+
x_mean = ex.rolling_mean(window)
|
|
460
|
+
x_var = ex.rolling_var(window)
|
|
461
|
+
xy_cov = (ey * ex).rolling_mean(window) - y_mean * x_mean
|
|
462
|
+
beta = xy_cov / (x_var + 1e-8)
|
|
463
|
+
return ey - y_mean - beta * (ex - x_mean)
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
@register_operator(OperatorCategory.TIME)
|
|
467
|
+
def zscored(f: Union[Expr, str], window: int = 20, **kwargs) -> Expr:
|
|
468
|
+
"""滚动 Z-score"""
|
|
469
|
+
e = _ensure_expr(f)
|
|
470
|
+
return (e - e.rolling_mean(window)) / (e.rolling_std(window) + 1e-8)
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
@register_operator(OperatorCategory.TIME)
|
|
474
|
+
def decay_linear(f: Union[Expr, str], window: int = 20, **kwargs) -> Expr:
|
|
475
|
+
"""线性衰减加权"""
|
|
476
|
+
weights = np.arange(1, window + 1)
|
|
477
|
+
weights = weights / weights.sum()
|
|
478
|
+
return _apply_weights(f, weights)
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
@register_operator(OperatorCategory.TIME)
|
|
482
|
+
def decay_exp(f: Union[Expr, str], halflife: int = 10, **kwargs) -> Expr:
|
|
483
|
+
"""指数衰减加权"""
|
|
484
|
+
alpha = 0.5 ** (1 / halflife)
|
|
485
|
+
weights = alpha ** np.arange(halflife)
|
|
486
|
+
weights = weights / weights.sum()
|
|
487
|
+
return _apply_weights(f, weights)
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
@register_operator(OperatorCategory.TIME)
|
|
491
|
+
def vwap(price: Union[Expr, str], volume: Union[Expr, str],
|
|
492
|
+
window: int = 20, **kwargs) -> Expr:
|
|
493
|
+
"""成交量加权平均价"""
|
|
494
|
+
ep, ev = _ensure_expr(price), _ensure_expr(volume)
|
|
495
|
+
return (ep * ev).rolling_sum(window) / (ev.rolling_sum(window) + 1e-8)
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
@register_operator(OperatorCategory.TIME)
|
|
499
|
+
def rolling_change_rate(f: Union[Expr, str], window: int = 20, **kwargs) -> Expr:
|
|
500
|
+
"""滚动变化率(符号保持)"""
|
|
501
|
+
f = _ensure_expr(f)
|
|
502
|
+
numerator = f - f.shift(window)
|
|
503
|
+
denominator = f.shift(window)
|
|
504
|
+
rate = numerator / (denominator.abs() + 1e-8)
|
|
505
|
+
same_sign = (numerator * denominator) >= 0
|
|
506
|
+
return pl.when(same_sign).then(rate).when(numerator > 0).then(pl.lit(1.0)).when(numerator < 0).then(pl.lit(-1.0)).otherwise(pl.lit(0.0))
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
# ==============================================================================
|
|
510
|
+
# QuantAlpha M1 新增别名(Alpha 101 算子命名约定)
|
|
511
|
+
# ==============================================================================
|
|
512
|
+
# 这些别名遵循 Alpha 101 论文的 `ts_*` / `*_xs` 命名约定,
|
|
513
|
+
# 使 Alpha 101 公式可以直接用 eval() 执行,详见
|
|
514
|
+
# QuantNodes/research/quant_alpha/operator_vocab/metadata.py
|
|
515
|
+
|
|
516
|
+
_make_alias(
|
|
517
|
+
"ts_decay_linear",
|
|
518
|
+
decay_linear,
|
|
519
|
+
"线性衰减加权移动平均(Alpha 101 命名约定,等价于 decay_linear)",
|
|
520
|
+
category=OperatorCategory.TIME,
|
|
521
|
+
)
|
|
522
|
+
|
|
523
|
+
_make_alias(
|
|
524
|
+
"ts_skew",
|
|
525
|
+
rolling_skew,
|
|
526
|
+
"滚动偏度(Alpha 101 命名约定,等价于 rolling_skew)",
|
|
527
|
+
category=OperatorCategory.TIME,
|
|
528
|
+
)
|
|
529
|
+
|
|
530
|
+
_make_alias(
|
|
531
|
+
"ts_kurt",
|
|
532
|
+
rolling_kurt,
|
|
533
|
+
"滚动峰度(Alpha 101 命名约定,等价于 rolling_kurt)",
|
|
534
|
+
category=OperatorCategory.TIME,
|
|
535
|
+
)
|