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.
Files changed (399) hide show
  1. QuantNodes/__init__.py +15 -0
  2. QuantNodes/__main__.py +14 -0
  3. QuantNodes/agent/__init__.py +158 -0
  4. QuantNodes/agent/agents/__init__.py +13 -0
  5. QuantNodes/agent/agents/definition.py +180 -0
  6. QuantNodes/agent/agents/manager.py +73 -0
  7. QuantNodes/agent/config/__init__.py +34 -0
  8. QuantNodes/agent/config/executor.py +958 -0
  9. QuantNodes/agent/config/loader.py +427 -0
  10. QuantNodes/agent/config/templates/bollinger_bands.yaml +84 -0
  11. QuantNodes/agent/config/templates/dual_ma.yaml +72 -0
  12. QuantNodes/agent/config/templates/empty.yaml +56 -0
  13. QuantNodes/agent/config/templates/mean_reversion.yaml +47 -0
  14. QuantNodes/agent/config/templates/mean_reversion_zscore.yaml +90 -0
  15. QuantNodes/agent/config/templates/momentum.yaml +81 -0
  16. QuantNodes/agent/config/templates/momentum_breakout.yaml +84 -0
  17. QuantNodes/agent/config/templates/rsi_strategy.yaml +72 -0
  18. QuantNodes/agent/config/templates/volume_price.yaml +86 -0
  19. QuantNodes/agent/config/types.py +156 -0
  20. QuantNodes/agent/config_mapper.py +293 -0
  21. QuantNodes/agent/core/__init__.py +19 -0
  22. QuantNodes/agent/core/dream.py +47 -0
  23. QuantNodes/agent/core/quant_dream.py +274 -0
  24. QuantNodes/agent/cron_jobs.py +314 -0
  25. QuantNodes/agent/nanobot_bridge.py +242 -0
  26. QuantNodes/agent/permission/__init__.py +30 -0
  27. QuantNodes/agent/permission/defaults.py +36 -0
  28. QuantNodes/agent/permission/evaluate.py +41 -0
  29. QuantNodes/agent/permission/models.py +59 -0
  30. QuantNodes/agent/permission/service.py +133 -0
  31. QuantNodes/agent/providers/__init__.py +11 -0
  32. QuantNodes/agent/providers/base.py +102 -0
  33. QuantNodes/agent/providers/quantnodes.py +610 -0
  34. QuantNodes/agent/providers/rate_limiter.py +326 -0
  35. QuantNodes/agent/providers/registry.py +163 -0
  36. QuantNodes/agent/skills/__init__.py +20 -0
  37. QuantNodes/agent/skills/base.py +118 -0
  38. QuantNodes/agent/skills/bridge.py +73 -0
  39. QuantNodes/agent/skills/factor/__init__.py +14 -0
  40. QuantNodes/agent/skills/factor/correlation.py +99 -0
  41. QuantNodes/agent/skills/factor/group_backtest.py +114 -0
  42. QuantNodes/agent/skills/factor/ic_analysis.py +106 -0
  43. QuantNodes/agent/skills/loader.py +107 -0
  44. QuantNodes/agent/skills/registry.py +105 -0
  45. QuantNodes/agent/skills/strategy/__init__.py +16 -0
  46. QuantNodes/agent/skills/strategy/bollinger.py +86 -0
  47. QuantNodes/agent/skills/strategy/dual_ma.py +82 -0
  48. QuantNodes/agent/skills/strategy/momentum.py +74 -0
  49. QuantNodes/agent/skills/strategy/rsi_reversal.py +99 -0
  50. QuantNodes/agent/skills_quant/__init__.py +14 -0
  51. QuantNodes/agent/skills_quant/backtest-analyze/SKILL.md +42 -0
  52. QuantNodes/agent/skills_quant/config-driven/SKILL.md +72 -0
  53. QuantNodes/agent/skills_quant/factor-research/SKILL.md +40 -0
  54. QuantNodes/agent/skills_quant/quant-dream/SKILL.md +55 -0
  55. QuantNodes/agent/skills_quant/risk-management/SKILL.md +45 -0
  56. QuantNodes/agent/skills_quant/strategy-design/SKILL.md +43 -0
  57. QuantNodes/agent/templates/__init__.py +4 -0
  58. QuantNodes/agent/tools/__init__.py +173 -0
  59. QuantNodes/agent/tools/_workspace.py +51 -0
  60. QuantNodes/agent/tools/alpha_backtest.py +328 -0
  61. QuantNodes/agent/tools/alpha_evaluate.py +493 -0
  62. QuantNodes/agent/tools/backtest.py +226 -0
  63. QuantNodes/agent/tools/base.py +133 -0
  64. QuantNodes/agent/tools/code_search.py +207 -0
  65. QuantNodes/agent/tools/config_backtest.py +401 -0
  66. QuantNodes/agent/tools/context.py +97 -0
  67. QuantNodes/agent/tools/dream_skill.py +77 -0
  68. QuantNodes/agent/tools/echo.py +38 -0
  69. QuantNodes/agent/tools/factor.py +231 -0
  70. QuantNodes/agent/tools/file_ops.py +201 -0
  71. QuantNodes/agent/tools/git_ops.py +190 -0
  72. QuantNodes/agent/tools/operator_lookup.py +218 -0
  73. QuantNodes/agent/tools/output_truncation.py +77 -0
  74. QuantNodes/agent/tools/path_check.py +43 -0
  75. QuantNodes/agent/tools/pipeline.py +62 -0
  76. QuantNodes/agent/tools/registry.py +150 -0
  77. QuantNodes/agent/tools/sandbox.py +62 -0
  78. QuantNodes/agent/tools/shell_safety.py +63 -0
  79. QuantNodes/agent/tools/strategy.py +106 -0
  80. QuantNodes/agent/tools/task.py +171 -0
  81. QuantNodes/agent/tools/web_fetch.py +142 -0
  82. QuantNodes/agent/tools/web_search.py +114 -0
  83. QuantNodes/agent/tools/wiki.py +370 -0
  84. QuantNodes/agent/utils/__init__.py +11 -0
  85. QuantNodes/agent/utils/helpers.py +43 -0
  86. QuantNodes/agent/utils/prompt_templates.py +30 -0
  87. QuantNodes/agent/workflows/__init__.py +20 -0
  88. QuantNodes/agent/workflows/implementations/__init__.py +8 -0
  89. QuantNodes/agent/workflows/implementations/alpha_gpt.py +508 -0
  90. QuantNodes/agent/workflows/implementations/mcts.py +442 -0
  91. QuantNodes/agent/workflows/parsers.py +44 -0
  92. QuantNodes/agent/workflows/registry.py +119 -0
  93. QuantNodes/agent/workflows/step_agent.py +219 -0
  94. QuantNodes/agent/workflows/tool.py +198 -0
  95. QuantNodes/ai/__init__.py +93 -0
  96. QuantNodes/ai/llm/__init__.py +75 -0
  97. QuantNodes/ai/llm/base.py +233 -0
  98. QuantNodes/ai/llm/decorators.py +281 -0
  99. QuantNodes/ai/llm/gateway.py +571 -0
  100. QuantNodes/ai/llm/null.py +76 -0
  101. QuantNodes/ai/llm/openai.py +435 -0
  102. QuantNodes/ai/optimizer.py +405 -0
  103. QuantNodes/ai/prompts/__init__.py +229 -0
  104. QuantNodes/ai/sandbox.py +371 -0
  105. QuantNodes/ai/sandbox_pandas_bridge.py +150 -0
  106. QuantNodes/ai/strategy_gen.py +396 -0
  107. QuantNodes/backtest/__init__.py +64 -0
  108. QuantNodes/backtest/backtest_node.py +188 -0
  109. QuantNodes/backtest/broker_node.py +378 -0
  110. QuantNodes/backtest/config_runner.py +397 -0
  111. QuantNodes/backtest/config_strategy.py +64 -0
  112. QuantNodes/backtest/risk_node.py +360 -0
  113. QuantNodes/backtest/strategy_node.py +268 -0
  114. QuantNodes/cache_node/__init__.py +19 -0
  115. QuantNodes/cache_node/base.py +244 -0
  116. QuantNodes/cache_node/cache_store.py +99 -0
  117. QuantNodes/cache_node/metadata.py +100 -0
  118. QuantNodes/cli/__init__.py +109 -0
  119. QuantNodes/cli/_helpers.py +511 -0
  120. QuantNodes/cli/command.py +110 -0
  121. QuantNodes/cli/commands/__init__.py +69 -0
  122. QuantNodes/cli/commands/agent.py +158 -0
  123. QuantNodes/cli/commands/alpha.py +951 -0
  124. QuantNodes/cli/commands/chat.py +38 -0
  125. QuantNodes/cli/commands/evolve.py +120 -0
  126. QuantNodes/cli/commands/factor.py +569 -0
  127. QuantNodes/cli/commands/init.py +190 -0
  128. QuantNodes/cli/commands/run.py +259 -0
  129. QuantNodes/cli/commands/serve.py +398 -0
  130. QuantNodes/cli/commands/version.py +120 -0
  131. QuantNodes/cli/enhanced.py +146 -0
  132. QuantNodes/conf_node/__init__.py +37 -0
  133. QuantNodes/conf_node/base.py +120 -0
  134. QuantNodes/conf_node/env_config.py +132 -0
  135. QuantNodes/conf_node/ini_config.py +70 -0
  136. QuantNodes/conf_node/json_config.py +69 -0
  137. QuantNodes/conf_node/yaml_config.py +78 -0
  138. QuantNodes/constants.py +17 -0
  139. QuantNodes/core/__init__.py +196 -0
  140. QuantNodes/core/_lookback_helpers.py +49 -0
  141. QuantNodes/core/ast_parser.py +198 -0
  142. QuantNodes/core/base.py +61 -0
  143. QuantNodes/core/cache_manager.py +344 -0
  144. QuantNodes/core/cache_utils.py +150 -0
  145. QuantNodes/core/cond_builder.py +53 -0
  146. QuantNodes/core/config.py +170 -0
  147. QuantNodes/core/constants.py +48 -0
  148. QuantNodes/core/control.py +412 -0
  149. QuantNodes/core/data_preprocessing.py +453 -0
  150. QuantNodes/core/data_source.py +46 -0
  151. QuantNodes/core/events.py +178 -0
  152. QuantNodes/core/evolution/__init__.py +22 -0
  153. QuantNodes/core/evolution/loop.py +583 -0
  154. QuantNodes/core/evolution/operators.py +289 -0
  155. QuantNodes/core/evolution/settings.py +44 -0
  156. QuantNodes/core/expression.py +841 -0
  157. QuantNodes/core/feedback/__init__.py +38 -0
  158. QuantNodes/core/feedback/channels.py +182 -0
  159. QuantNodes/core/feedback/collector.py +91 -0
  160. QuantNodes/core/feedback/dataclass.py +239 -0
  161. QuantNodes/core/feedback/llm_judge.py +138 -0
  162. QuantNodes/core/knowledge/__init__.py +69 -0
  163. QuantNodes/core/knowledge/knowledge_base.py +217 -0
  164. QuantNodes/core/knowledge/lineage_compress.py +196 -0
  165. QuantNodes/core/knowledge/lineage_expand.py +123 -0
  166. QuantNodes/core/knowledge/metrics/__init__.py +43 -0
  167. QuantNodes/core/knowledge/metrics/evaluator.py +176 -0
  168. QuantNodes/core/knowledge/metrics/metrics.py +220 -0
  169. QuantNodes/core/knowledge/rag_prompt.py +196 -0
  170. QuantNodes/core/knowledge/retriever.py +209 -0
  171. QuantNodes/core/lambda_node.py +81 -0
  172. QuantNodes/core/monitoring/__init__.py +22 -0
  173. QuantNodes/core/monitoring/collector.py +292 -0
  174. QuantNodes/core/monitoring/dashboard.py +365 -0
  175. QuantNodes/core/node.py +375 -0
  176. QuantNodes/core/pandas_utils.py +504 -0
  177. QuantNodes/core/parallel/__init__.py +15 -0
  178. QuantNodes/core/parallel/worker.py +140 -0
  179. QuantNodes/core/parallel/worker_process.py +265 -0
  180. QuantNodes/core/path_utils.py +73 -0
  181. QuantNodes/core/pipeline.py +328 -0
  182. QuantNodes/core/plugin.py +135 -0
  183. QuantNodes/core/quality_gate/__init__.py +32 -0
  184. QuantNodes/core/quality_gate/complexity.py +94 -0
  185. QuantNodes/core/quality_gate/consistency.py +26 -0
  186. QuantNodes/core/quality_gate/node.py +97 -0
  187. QuantNodes/core/quality_gate/redundancy.py +51 -0
  188. QuantNodes/core/quality_gate/settings.py +43 -0
  189. QuantNodes/core/quality_gate/zoo.py +98 -0
  190. QuantNodes/core/serializable.py +116 -0
  191. QuantNodes/core/serialization.py +673 -0
  192. QuantNodes/core/tools.py +333 -0
  193. QuantNodes/core/trajectory/__init__.py +25 -0
  194. QuantNodes/core/trajectory/entry.py +116 -0
  195. QuantNodes/core/trajectory/lineage.py +67 -0
  196. QuantNodes/core/trajectory/pool.py +211 -0
  197. QuantNodes/core/trajectory/selector.py +140 -0
  198. QuantNodes/core/visualization/__init__.py +33 -0
  199. QuantNodes/core/visualization/builder.py +233 -0
  200. QuantNodes/core/visualization/gate_breakdown.py +140 -0
  201. QuantNodes/core/visualization/lineage_dag.py +203 -0
  202. QuantNodes/core/visualization/metric_distribution.py +125 -0
  203. QuantNodes/core/visualization/report.py +68 -0
  204. QuantNodes/database_node/__init__.py +69 -0
  205. QuantNodes/database_node/base.py +135 -0
  206. QuantNodes/database_node/clickhouse_node.py +272 -0
  207. QuantNodes/database_node/csv_node.py +83 -0
  208. QuantNodes/database_node/duckdb_node.py +86 -0
  209. QuantNodes/database_node/factory.py +83 -0
  210. QuantNodes/database_node/mysql_node.py +100 -0
  211. QuantNodes/database_node/parquet_node.py +75 -0
  212. QuantNodes/database_node/sqlite_node.py +67 -0
  213. QuantNodes/factor_node/__init__.py +50 -0
  214. QuantNodes/factor_node/factor.py +563 -0
  215. QuantNodes/factor_node/factor_db.py +421 -0
  216. QuantNodes/factor_node/factor_functions/__init__.py +252 -0
  217. QuantNodes/factor_node/factor_functions/_helpers.py +358 -0
  218. QuantNodes/factor_node/factor_functions/_helpers_debug.py +317 -0
  219. QuantNodes/factor_node/factor_functions/composite_ops.py +136 -0
  220. QuantNodes/factor_node/factor_functions/math_ops.py +433 -0
  221. QuantNodes/factor_node/factor_functions/section_ops.py +290 -0
  222. QuantNodes/factor_node/factor_functions/talib_ops.py +1293 -0
  223. QuantNodes/factor_node/factor_functions/time_ops.py +535 -0
  224. QuantNodes/factor_node/factor_operation.py +1115 -0
  225. QuantNodes/factor_node/factor_table.py +1073 -0
  226. QuantNodes/factor_node/quant_nodes_object.py +60 -0
  227. QuantNodes/mcp_server/__init__.py +27 -0
  228. QuantNodes/mcp_server/__main__.py +4 -0
  229. QuantNodes/mcp_server/server.py +272 -0
  230. QuantNodes/methods/__init__.py +28 -0
  231. QuantNodes/methods/pipeline.py +100 -0
  232. QuantNodes/methods/sandbox.py +102 -0
  233. QuantNodes/monitor/__init__.py +27 -0
  234. QuantNodes/monitor/agent_tools/__init__.py +5 -0
  235. QuantNodes/monitor/agent_tools/monitor_tool.py +98 -0
  236. QuantNodes/monitor/agent_tools/schedule_tool.py +98 -0
  237. QuantNodes/monitor/agent_tools/version_tool.py +133 -0
  238. QuantNodes/monitor/monitor/__init__.py +6 -0
  239. QuantNodes/monitor/monitor/alerter.py +60 -0
  240. QuantNodes/monitor/monitor/collector.py +164 -0
  241. QuantNodes/monitor/monitor/dashboard.py +115 -0
  242. QuantNodes/monitor/monitor/drift.py +190 -0
  243. QuantNodes/monitor/scheduler/__init__.py +4 -0
  244. QuantNodes/monitor/scheduler/runner.py +133 -0
  245. QuantNodes/monitor/scheduler/scheduler.py +184 -0
  246. QuantNodes/monitor/storage/__init__.py +16 -0
  247. QuantNodes/monitor/storage/models.py +70 -0
  248. QuantNodes/monitor/storage/repository.py +407 -0
  249. QuantNodes/monitor/version/__init__.py +4 -0
  250. QuantNodes/monitor/version/diff.py +81 -0
  251. QuantNodes/monitor/version/version_manager.py +182 -0
  252. QuantNodes/operator_node/__init__.py +28 -0
  253. QuantNodes/operator_node/base.py +97 -0
  254. QuantNodes/operator_node/query_node.py +129 -0
  255. QuantNodes/operator_node/sql_builder.py +125 -0
  256. QuantNodes/operator_node/sql_utils.py +172 -0
  257. QuantNodes/operator_node/transform.py +130 -0
  258. QuantNodes/operators/__init__.py +90 -0
  259. QuantNodes/operators/_engine.py +108 -0
  260. QuantNodes/operators/composite.py +161 -0
  261. QuantNodes/operators/composite_dag.py +667 -0
  262. QuantNodes/operators/composite_dag_ops.py +343 -0
  263. QuantNodes/operators/composite_dag_pandas_ops.py +382 -0
  264. QuantNodes/operators/custom.py +408 -0
  265. QuantNodes/operators/facade.py +164 -0
  266. QuantNodes/operators/math.py +163 -0
  267. QuantNodes/operators/proxy.py +29 -0
  268. QuantNodes/operators/registry.py +144 -0
  269. QuantNodes/operators/section.py +99 -0
  270. QuantNodes/operators/talib.py +757 -0
  271. QuantNodes/operators/templates.py +95 -0
  272. QuantNodes/operators/time_series.py +136 -0
  273. QuantNodes/prompts/__init__.py +20 -0
  274. QuantNodes/prompts/backtest/__init__.py +12 -0
  275. QuantNodes/prompts/backtest/factor_based.py +86 -0
  276. QuantNodes/prompts/backtest/standard.py +73 -0
  277. QuantNodes/prompts/factor/__init__.py +14 -0
  278. QuantNodes/prompts/factor/correlation.py +77 -0
  279. QuantNodes/prompts/factor/group_backtest.py +86 -0
  280. QuantNodes/prompts/factor/ic_analysis.py +91 -0
  281. QuantNodes/prompts/strategy/__init__.py +18 -0
  282. QuantNodes/prompts/strategy/market_neutral.py +96 -0
  283. QuantNodes/prompts/strategy/mean_reversion.py +107 -0
  284. QuantNodes/prompts/strategy/momentum.py +160 -0
  285. QuantNodes/prompts/strategy/pairs_trading.py +107 -0
  286. QuantNodes/prompts/strategy/trend_following.py +96 -0
  287. QuantNodes/research/README.md +106 -0
  288. QuantNodes/research/__init__.py +154 -0
  289. QuantNodes/research/_legacy_3c/__init__.py +61 -0
  290. QuantNodes/research/_legacy_3c/auto_researcher.py +289 -0
  291. QuantNodes/research/_legacy_3c/factor_evaluator.py +560 -0
  292. QuantNodes/research/_legacy_3c/factor_miner.py +318 -0
  293. QuantNodes/research/_legacy_3c/mcts_search.py +324 -0
  294. QuantNodes/research/factor_test/__init__.py +25 -0
  295. QuantNodes/research/factor_test/config.py +184 -0
  296. QuantNodes/research/factor_test/config_builder.py +276 -0
  297. QuantNodes/research/factor_test/e2e/data_prep.py +163 -0
  298. QuantNodes/research/factor_test/e2e/run_evolution_e2e.py +309 -0
  299. QuantNodes/research/factor_test/evolution_adapter.py +231 -0
  300. QuantNodes/research/factor_test/feedback_wrapper.py +102 -0
  301. QuantNodes/research/factor_test/ifind_db/__init__.py +7 -0
  302. QuantNodes/research/factor_test/ifind_db/fetcher.py +224 -0
  303. QuantNodes/research/factor_test/ifind_db/ifind_database.py +689 -0
  304. QuantNodes/research/factor_test/nodes/__init__.py +1 -0
  305. QuantNodes/research/factor_test/nodes/_base.py +91 -0
  306. QuantNodes/research/factor_test/nodes/adjust_date_node.py +48 -0
  307. QuantNodes/research/factor_test/nodes/configs.py +240 -0
  308. QuantNodes/research/factor_test/nodes/factor_neutralize_node.py +87 -0
  309. QuantNodes/research/factor_test/nodes/factor_preprocess_node.py +222 -0
  310. QuantNodes/research/factor_test/nodes/factor_score_node.py +141 -0
  311. QuantNodes/research/factor_test/nodes/factor_test_report_node.py +153 -0
  312. QuantNodes/research/factor_test/nodes/group_analyzer_node.py +317 -0
  313. QuantNodes/research/factor_test/nodes/ic_analyzer_node.py +112 -0
  314. QuantNodes/research/factor_test/nodes/load_data_node.py +100 -0
  315. QuantNodes/research/factor_test/nodes/long_short_node.py +93 -0
  316. QuantNodes/research/factor_test/nodes/neutralizers.py +222 -0
  317. QuantNodes/research/factor_test/nodes/preprocess_strategies.py +277 -0
  318. QuantNodes/research/factor_test/nodes/risk_correlation_node.py +112 -0
  319. QuantNodes/research/factor_test/nodes/sample_pool_filter_node.py +110 -0
  320. QuantNodes/research/factor_test/nodes/tradability_filter_node.py +92 -0
  321. QuantNodes/research/factor_test/pipeline_runner.py +305 -0
  322. QuantNodes/research/factor_test/pipeline_spec.py +216 -0
  323. QuantNodes/research/factor_test/utils/__init__.py +26 -0
  324. QuantNodes/research/factor_test/utils/constants.py +86 -0
  325. QuantNodes/research/factor_test/utils/data_loader.py +141 -0
  326. QuantNodes/research/factor_test/utils/date_utils.py +232 -0
  327. QuantNodes/research/factor_test/utils/file_loaders.py +150 -0
  328. QuantNodes/research/factor_test/utils/labels.py +37 -0
  329. QuantNodes/research/factor_test/utils/metrics_extractor.py +55 -0
  330. QuantNodes/research/factor_test/utils/performance_metrics.py +175 -0
  331. QuantNodes/research/factor_test/utils/safe_load.py +106 -0
  332. QuantNodes/research/quant_alpha/CHANGELOG.md +80 -0
  333. QuantNodes/research/quant_alpha/README.md +142 -0
  334. QuantNodes/research/quant_alpha/__init__.py +45 -0
  335. QuantNodes/research/quant_alpha/adapters/__init__.py +99 -0
  336. QuantNodes/research/quant_alpha/adapters/calculator.py +503 -0
  337. QuantNodes/research/quant_alpha/adapters/expression.py +387 -0
  338. QuantNodes/research/quant_alpha/alpha101_design/__init__.py +50 -0
  339. QuantNodes/research/quant_alpha/alpha101_design/few_shot_examples.py +243 -0
  340. QuantNodes/research/quant_alpha/alpha101_design/philosophy.py +474 -0
  341. QuantNodes/research/quant_alpha/alpha158_design/__init__.py +63 -0
  342. QuantNodes/research/quant_alpha/alpha158_design/few_shot_examples.py +219 -0
  343. QuantNodes/research/quant_alpha/alpha158_design/philosophy.py +240 -0
  344. QuantNodes/research/quant_alpha/evaluation/__init__.py +47 -0
  345. QuantNodes/research/quant_alpha/evaluation/baselines/__init__.py +8 -0
  346. QuantNodes/research/quant_alpha/evaluation/baselines/g1_handcrafted.py +135 -0
  347. QuantNodes/research/quant_alpha/evaluation/baselines/g2_llm_only.py +269 -0
  348. QuantNodes/research/quant_alpha/evaluation/baselines/g3_alpha_gpt.py +152 -0
  349. QuantNodes/research/quant_alpha/evaluation/clickhouse_data_loader.py +227 -0
  350. QuantNodes/research/quant_alpha/evaluation/contracts.py +376 -0
  351. QuantNodes/research/quant_alpha/evaluation/evaluators/__init__.py +6 -0
  352. QuantNodes/research/quant_alpha/evaluation/evaluators/polars_evaluator.py +545 -0
  353. QuantNodes/research/quant_alpha/evaluation/mock_data_loader.py +226 -0
  354. QuantNodes/research/quant_alpha/evaluation/runner.py +243 -0
  355. QuantNodes/research/quant_alpha/llm/__init__.py +38 -0
  356. QuantNodes/research/quant_alpha/llm/parser.py +681 -0
  357. QuantNodes/research/quant_alpha/logic_driven_pipeline.py +411 -0
  358. QuantNodes/research/quant_alpha/logic_mining/__init__.py +74 -0
  359. QuantNodes/research/quant_alpha/logic_mining/compiler.py +457 -0
  360. QuantNodes/research/quant_alpha/logic_mining/generator.py +366 -0
  361. QuantNodes/research/quant_alpha/logic_mining/models.py +252 -0
  362. QuantNodes/research/quant_alpha/logic_mining/parser.py +287 -0
  363. QuantNodes/research/quant_alpha/logic_mining/pipelines.py +297 -0
  364. QuantNodes/research/quant_alpha/logic_mining/sources.py +149 -0
  365. QuantNodes/research/quant_alpha/mcts/__init__.py +66 -0
  366. QuantNodes/research/quant_alpha/mcts/cache.py +262 -0
  367. QuantNodes/research/quant_alpha/mcts/extension_ops.py +320 -0
  368. QuantNodes/research/quant_alpha/mcts/feedback.py +825 -0
  369. QuantNodes/research/quant_alpha/mcts/op_prior.py +180 -0
  370. QuantNodes/research/quant_alpha/mcts/search.py +540 -0
  371. QuantNodes/research/quant_alpha/mcts/tree.py +201 -0
  372. QuantNodes/research/quant_alpha/operator_vocab/__init__.py +50 -0
  373. QuantNodes/research/quant_alpha/operator_vocab/config.py +54 -0
  374. QuantNodes/research/quant_alpha/operator_vocab/metadata.py +263 -0
  375. QuantNodes/research/quant_alpha/operator_vocab/vocabulary.py +481 -0
  376. QuantNodes/research/quant_alpha/pipeline.py +1027 -0
  377. QuantNodes/research/quant_alpha/types/__init__.py +27 -0
  378. QuantNodes/research/quant_alpha/types/constants.py +28 -0
  379. QuantNodes/research/quant_alpha/types/state.py +205 -0
  380. QuantNodes/research/quant_alpha/workflow/__init__.py +32 -0
  381. QuantNodes/research/quant_alpha/workflow/alpha_gpt.py +911 -0
  382. QuantNodes/research/quant_alpha/workflow/alpha_logics.py +416 -0
  383. QuantNodes/research/quant_alpha/workflow/state.py +27 -0
  384. QuantNodes/research/report_reproducer.py +485 -0
  385. QuantNodes/research/wiki.py +1155 -0
  386. QuantNodes/symbolic/__init__.py +51 -0
  387. QuantNodes/symbolic/compiler.py +113 -0
  388. QuantNodes/symbolic/dialect.py +260 -0
  389. QuantNodes/symbolic/executor.py +147 -0
  390. QuantNodes/symbolic/expression.py +234 -0
  391. QuantNodes/symbolic/functions.py +433 -0
  392. QuantNodes/symbolic/optimizer.py +165 -0
  393. QuantNodes/ui_node/__init__.py +30 -0
  394. QuantNodes/ui_node/base.py +222 -0
  395. quantnodes-3.0.0.dist-info/METADATA +463 -0
  396. quantnodes-3.0.0.dist-info/RECORD +399 -0
  397. quantnodes-3.0.0.dist-info/WHEEL +5 -0
  398. quantnodes-3.0.0.dist-info/entry_points.txt +24 -0
  399. quantnodes-3.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,136 @@
1
+ # coding=utf-8
2
+ """
3
+ 组合算子
4
+
5
+ 本模块包含所有组合(multi-section)相关的因子运算算子。
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from typing import Dict, List, Optional, Union
11
+
12
+ from polars import Expr
13
+
14
+ from QuantNodes.factor_node.factor_functions._helpers import (
15
+ OperatorCategory,
16
+ register_operator,
17
+ _ensure_expr,
18
+ _make_aggr_wrapper,
19
+ )
20
+
21
+
22
+ # ==============================================================================
23
+ # Multi-Section 算子
24
+ # ==============================================================================
25
+
26
+ @register_operator(OperatorCategory.MULTI_SECTION)
27
+ def aggregate(f: Union[Expr, str], group_by: str, method: str = "mean", **kwargs) -> Expr:
28
+ """按组聚合"""
29
+ f = _ensure_expr(f)
30
+ method_map = {
31
+ "mean": f.mean().over(group_by),
32
+ "sum": f.sum().over(group_by),
33
+ "std": f.std().over(group_by),
34
+ "var": f.var().over(group_by),
35
+ "median": f.median().over(group_by),
36
+ "min": f.min().over(group_by),
37
+ "max": f.max().over(group_by),
38
+ "first": f.first().over(group_by),
39
+ "last": f.last().over(group_by),
40
+ "count": f.count().over(group_by),
41
+ }
42
+ return method_map.get(method, f.mean().over(group_by))
43
+
44
+
45
+ @register_operator(OperatorCategory.MULTI_SECTION)
46
+ def disaggregate(f: Union[Expr, str], group_by: str, **kwargs) -> Expr:
47
+ """解聚合 (将聚合值展开到组内每个成员)"""
48
+ return _ensure_expr(f).over(group_by)
49
+
50
+
51
+ # Aggr 系列工厂
52
+ _MAKE_AGGR_DOCS = {
53
+ "sum": "聚合求和",
54
+ "mean": "聚合均值",
55
+ "max": "聚合最大值",
56
+ "min": "聚合最小值",
57
+ "std": "聚合标准差",
58
+ "var": "聚合方差",
59
+ "median": "聚合中位数",
60
+ "count": "聚合计数",
61
+ }
62
+
63
+ for _method, _doc in _MAKE_AGGR_DOCS.items():
64
+ _make_aggr_wrapper(_method, _doc)
65
+
66
+ del _method, _doc, _MAKE_AGGR_DOCS
67
+
68
+
69
+ @register_operator(OperatorCategory.MULTI_SECTION)
70
+ def aggr_prod(f: Union[Expr, str], group_by: str, **kwargs) -> Expr:
71
+ """聚合求积"""
72
+ return _ensure_expr(f).log().sum().over(group_by).exp()
73
+
74
+
75
+ @register_operator(OperatorCategory.MULTI_SECTION)
76
+ def aggr_quantile(f: Union[Expr, str], group_by: str,
77
+ quantile: float = 0.5, **kwargs) -> Expr:
78
+ """聚合分位数"""
79
+ return _ensure_expr(f).quantile(quantile).over(group_by)
80
+
81
+
82
+ @register_operator(OperatorCategory.MULTI_SECTION)
83
+ def merge(factors: List[Union[Expr, str]], weights: Optional[List[float]] = None,
84
+ method: str = "add", **kwargs) -> Expr:
85
+ """合并多个因子"""
86
+ if weights is None:
87
+ weights = [1.0 / len(factors)] * len(factors)
88
+ factors = [_ensure_expr(f) for f in factors]
89
+ weights = list(weights)
90
+
91
+ if method == "add":
92
+ result = factors[0] * weights[0]
93
+ for i in range(1, len(factors)):
94
+ result = result + factors[i] * weights[i]
95
+ return result
96
+ elif method == "wavg":
97
+ weighted = sum(f * w for f, w in zip(factors, weights))
98
+ return weighted / sum(weights)
99
+ elif method == "rank":
100
+ ranked = [f.rank() for f in factors]
101
+ result = ranked[0] * weights[0]
102
+ for i in range(1, len(ranked)):
103
+ result = result + ranked[i] * weights[i]
104
+ return result
105
+ elif method == "mul":
106
+ result = factors[0] ** weights[0]
107
+ for i in range(1, len(factors)):
108
+ result = result * factors[i] ** weights[i]
109
+ return result
110
+ return factors[0]
111
+
112
+
113
+ @register_operator(OperatorCategory.MULTI_SECTION)
114
+ def chg_ids(f: Union[Expr, str], id_map: Dict[str, str], **kwargs) -> Expr:
115
+ """ID转换"""
116
+ f = _ensure_expr(f)
117
+ return f.replace(list(id_map.keys()), list(id_map.values()))
118
+
119
+
120
+ @register_operator(OperatorCategory.MULTI_SECTION)
121
+ def blend(f1: Union[Expr, str], f2: Union[Expr, str],
122
+ alpha: float = 0.5, **kwargs) -> Expr:
123
+ """混合两个因子"""
124
+ return _ensure_expr(f1) * alpha + _ensure_expr(f2) * (1 - alpha)
125
+
126
+
127
+ @register_operator(OperatorCategory.MULTI_SECTION)
128
+ def nav(f: Union[Expr, str], **kwargs) -> Expr:
129
+ """NAV (单位净值)"""
130
+ return (1 + _ensure_expr(f)).cum_prod()
131
+
132
+
133
+ @register_operator(OperatorCategory.MULTI_SECTION)
134
+ def rebase(f: Union[Expr, str], base: float = 100.0, **kwargs) -> Expr:
135
+ """重定基期"""
136
+ return (_ensure_expr(f) / _ensure_expr(f).first() * base)
@@ -0,0 +1,433 @@
1
+ # coding=utf-8
2
+ """
3
+ 数学算子
4
+
5
+ 本模块包含所有数学(point-wise)相关的因子运算算子。
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from typing import Any, Callable, List, Optional, Union
11
+
12
+ import polars as pl
13
+ from polars import Expr
14
+
15
+ from QuantNodes.factor_node.factor_functions._helpers import (
16
+ OperatorCategory,
17
+ register_operator,
18
+ _ensure_expr,
19
+ _make_nan_wrapper,
20
+ )
21
+
22
+
23
+ # ==============================================================================
24
+ # Point 算子 - 数学运算
25
+ # ==============================================================================
26
+
27
+ @register_operator(OperatorCategory.POINT)
28
+ def abs(f: Union[Expr, str], **kwargs) -> Expr:
29
+ """绝对值"""
30
+ return _ensure_expr(f).abs()
31
+
32
+
33
+ @register_operator(OperatorCategory.POINT)
34
+ def log(f: Union[Expr, str], base: Optional[str] = None, **kwargs) -> Expr:
35
+ """对数
36
+
37
+ base: 对数底数,"e"/"2"/"10" 或 None(自然对数)
38
+ """
39
+ e = _ensure_expr(f)
40
+ col_name = f if isinstance(f, str) else None
41
+
42
+ if base is None or base == "e":
43
+ result = e.log()
44
+ elif base == "2":
45
+ result = e.log() / pl.lit(2).log()
46
+ elif base == "10":
47
+ result = e.log10()
48
+ else:
49
+ result = e.log()
50
+
51
+ if col_name:
52
+ return result.alias(col_name)
53
+ return result
54
+
55
+
56
+ @register_operator(OperatorCategory.POINT)
57
+ def sign(f: Union[Expr, str], **kwargs) -> Expr:
58
+ """符号"""
59
+ return _ensure_expr(f).sign()
60
+
61
+
62
+ @register_operator(OperatorCategory.POINT, "signedpower")
63
+ def signedpower(f: Union[Expr, str], exponent: float = 2.0, **kwargs) -> Expr:
64
+ """保留符号的幂运算(Alpha 101 关键算子)
65
+
66
+ signedpower(x, a) = sign(x) * abs(x) ** a
67
+
68
+ Examples:
69
+ - signedpower(close, 2) → 保留正负号的 close²
70
+ - signedpower(returns, 0.5) → 保留正负号的 sqrt(|returns|)
71
+ """
72
+ e = _ensure_expr(f)
73
+ return e.sign() * e.abs() ** exponent
74
+
75
+
76
+ @register_operator(OperatorCategory.POINT)
77
+ def sqrt(f: Union[Expr, str], **kwargs) -> Expr:
78
+ """平方根"""
79
+ return _ensure_expr(f).sqrt()
80
+
81
+
82
+ @register_operator(OperatorCategory.POINT)
83
+ def square(f: Union[Expr, str], **kwargs) -> Expr:
84
+ """平方"""
85
+ return _ensure_expr(f) ** 2
86
+
87
+
88
+ @register_operator(OperatorCategory.POINT)
89
+ def pow(f: Union[Expr, str], exponent: float = 2.0, **kwargs) -> Expr:
90
+ """幂运算"""
91
+ return _ensure_expr(f) ** exponent
92
+
93
+
94
+ @register_operator(OperatorCategory.POINT)
95
+ def clip(f: Union[Expr, str], lower: Optional[float] = None,
96
+ upper: Optional[float] = None, **kwargs) -> Expr:
97
+ """裁剪"""
98
+ e = _ensure_expr(f)
99
+ if lower is not None and upper is not None:
100
+ return e.clip(lower, upper)
101
+ elif lower is not None:
102
+ return e.clip(lower_bound=lower)
103
+ elif upper is not None:
104
+ return e.clip(upper_bound=upper)
105
+ return e
106
+
107
+
108
+ @register_operator(OperatorCategory.POINT)
109
+ def fill_null(f: Union[Expr, str], value: float = 0.0, **kwargs) -> Expr:
110
+ """填充 null
111
+
112
+ value: 填充值,或 "forward"/"backward" 策略
113
+ """
114
+ e = _ensure_expr(f)
115
+ col_name = f if isinstance(f, str) else None
116
+
117
+ if isinstance(value, str):
118
+ if value == "forward":
119
+ result = e.fill_null(strategy="forward")
120
+ elif value == "backward":
121
+ result = e.fill_null(strategy="backward")
122
+ else:
123
+ result = e.fill_null(0)
124
+ else:
125
+ result = e.fill_null(value)
126
+
127
+ if col_name:
128
+ return result.alias(col_name)
129
+ return result
130
+
131
+
132
+ @register_operator(OperatorCategory.POINT)
133
+ def fill_null_by_strategy(f: Union[Expr, str], strategy: str = "mean", **kwargs) -> Expr:
134
+ """按策略填充 null
135
+
136
+ strategy: mean / median / max / min / zero / one
137
+ """
138
+ f = _ensure_expr(f)
139
+ strategy_map = {
140
+ "mean": f.fill_null(f.mean()),
141
+ "median": f.fill_null(f.median()),
142
+ "max": f.fill_null(f.max()),
143
+ "min": f.fill_null(f.min()),
144
+ "zero": f.fill_null(0),
145
+ "one": f.fill_null(1),
146
+ }
147
+ return strategy_map.get(strategy, f.fill_null(0))
148
+
149
+
150
+ @register_operator(OperatorCategory.POINT)
151
+ def fill_zero(f: Union[Expr, str], **kwargs) -> Expr:
152
+ """填充 0"""
153
+ return _ensure_expr(f).fill_null(0)
154
+
155
+
156
+ @register_operator(OperatorCategory.POINT)
157
+ def nan_to_null(f: Union[Expr, str], **kwargs) -> Expr:
158
+ """NaN 转 null"""
159
+ col_name = f if isinstance(f, str) else None
160
+ e = _ensure_expr(f)
161
+ result = pl.when(e.is_nan()).then(pl.lit(None).cast(pl.Float64)).otherwise(e)
162
+ if col_name:
163
+ return result.alias(col_name)
164
+ return result
165
+
166
+
167
+ @register_operator(OperatorCategory.POINT)
168
+ def isnull(f: Union[Expr, str], **kwargs) -> Expr:
169
+ """判断空值"""
170
+ return _ensure_expr(f).is_null()
171
+
172
+
173
+ @register_operator(OperatorCategory.POINT)
174
+ def notnull(f: Union[Expr, str], **kwargs) -> Expr:
175
+ """判断非空"""
176
+ return _ensure_expr(f).is_not_null()
177
+
178
+
179
+ # ==============================================================================
180
+ # Point 算子 (补充)
181
+ # ==============================================================================
182
+
183
+ @register_operator(OperatorCategory.POINT)
184
+ def ceil(f: Union[Expr, str], **kwargs) -> Expr:
185
+ """向上取整"""
186
+ return _ensure_expr(f).ceil()
187
+
188
+
189
+ @register_operator(OperatorCategory.POINT)
190
+ def floor(f: Union[Expr, str], **kwargs) -> Expr:
191
+ """向下取整"""
192
+ return _ensure_expr(f).floor()
193
+
194
+
195
+ @register_operator(OperatorCategory.POINT)
196
+ def fix(f: Union[Expr, str], **kwargs) -> Expr:
197
+ """向零取整"""
198
+ e = _ensure_expr(f)
199
+ return pl.when(e < 0).then(e.ceil()).otherwise(e.floor())
200
+
201
+
202
+ # ==============================================================================
203
+ # Point 算子 - 三角函数
204
+ # ==============================================================================
205
+
206
+ @register_operator(OperatorCategory.POINT)
207
+ def sin(f: Union[Expr, str], **kwargs) -> Expr:
208
+ """正弦"""
209
+ return _ensure_expr(f).sin()
210
+
211
+
212
+ @register_operator(OperatorCategory.POINT)
213
+ def cos(f: Union[Expr, str], **kwargs) -> Expr:
214
+ """余弦"""
215
+ return _ensure_expr(f).cos()
216
+
217
+
218
+ @register_operator(OperatorCategory.POINT)
219
+ def tan(f: Union[Expr, str], **kwargs) -> Expr:
220
+ """正切"""
221
+ return _ensure_expr(f).tan()
222
+
223
+
224
+ @register_operator(OperatorCategory.POINT)
225
+ def arcsin(f: Union[Expr, str], **kwargs) -> Expr:
226
+ """反正弦"""
227
+ return _ensure_expr(f).arcsin()
228
+
229
+
230
+ @register_operator(OperatorCategory.POINT)
231
+ def arccos(f: Union[Expr, str], **kwargs) -> Expr:
232
+ """反余弦"""
233
+ return _ensure_expr(f).arccos()
234
+
235
+
236
+ @register_operator(OperatorCategory.POINT)
237
+ def arctan(f: Union[Expr, str], **kwargs) -> Expr:
238
+ """反正切"""
239
+ return _ensure_expr(f).arctan()
240
+
241
+
242
+ # ==============================================================================
243
+ # Point 算子 - 补充
244
+ # ==============================================================================
245
+
246
+ @register_operator(OperatorCategory.POINT)
247
+ def log1p(f: Union[Expr, str], **kwargs) -> Expr:
248
+ """log(1+x)"""
249
+ return (_ensure_expr(f) + 1).log()
250
+
251
+
252
+ # ==============================================================================
253
+ # 双因子算子
254
+ # ==============================================================================
255
+
256
+ @register_operator(OperatorCategory.POINT)
257
+ def add(f1: Union[Expr, str], f2: Union[Expr, str], **kwargs) -> Expr:
258
+ """加法"""
259
+ return _ensure_expr(f1) + _ensure_expr(f2)
260
+
261
+
262
+ @register_operator(OperatorCategory.POINT)
263
+ def sub(f1: Union[Expr, str], f2: Union[Expr, str], **kwargs) -> Expr:
264
+ """减法"""
265
+ return _ensure_expr(f1) - _ensure_expr(f2)
266
+
267
+
268
+ @register_operator(OperatorCategory.POINT)
269
+ def mul(f1: Union[Expr, str], f2: Union[Expr, str], **kwargs) -> Expr:
270
+ """乘法"""
271
+ return _ensure_expr(f1) * _ensure_expr(f2)
272
+
273
+
274
+ @register_operator(OperatorCategory.POINT)
275
+ def div(f1: Union[Expr, str], f2: Union[Expr, str], **kwargs) -> Expr:
276
+ """除法"""
277
+ return _ensure_expr(f1) / _ensure_expr(f2)
278
+
279
+
280
+ # ==============================================================================
281
+ # NaN 聚合
282
+ # ==============================================================================
283
+
284
+ _make_nan = lambda name, doc: _make_nan_wrapper(name, name, doc)
285
+
286
+ _make_nan("nanmax", "NaN 忽略的最大值")
287
+ _make_nan("nanmin", "NaN 忽略的最小值")
288
+ _make_nan("nanmean", "NaN 忽略的均值")
289
+ _make_nan("nansum", "NaN 忽略的求和")
290
+ _make_nan("nanstd", "NaN 忽略的标准差")
291
+ _make_nan("nanvar", "NaN 忽略的方差")
292
+ _make_nan("nanargmax", "NaN 忽略的最大值位置")
293
+ _make_nan("nanargmin", "NaN 忽略的最小值位置")
294
+ _make_nan("nanmedian", "NaN 忽略的中位数")
295
+ _make_nan("nancount", "NaN 忽略的计数")
296
+ _make_nan("nanprod", "NaN 忽略的乘积")
297
+
298
+
299
+ @register_operator(OperatorCategory.POINT)
300
+ def nanquantile(f: Union[Expr, str], quantile: float = 0.5,
301
+ interpolation: str = "nearest", **kwargs) -> Expr:
302
+ """NaN 忽略的分位数"""
303
+ return _ensure_expr(f).quantile(quantile, interpolation=interpolation)
304
+
305
+
306
+ # ==============================================================================
307
+ # 其他
308
+ # ==============================================================================
309
+
310
+ @register_operator(OperatorCategory.POINT)
311
+ def applymap(f: Union[Expr, str], func: Callable, **kwargs) -> Expr:
312
+ """应用函数到每个元素"""
313
+ return _ensure_expr(f).map_elements(func, return_dtype=pl.Float64)
314
+
315
+
316
+ @register_operator(OperatorCategory.POINT)
317
+ def astype(f: Union[Expr, str], dtype: Any = "float64", **kwargs) -> Expr:
318
+ """类型转换"""
319
+ if isinstance(dtype, str):
320
+ dtype = getattr(pl, dtype, None) or getattr(pl.datatypes, dtype, None)
321
+ if dtype is None:
322
+ dtype = pl.Float64
323
+ return _ensure_expr(f).cast(dtype)
324
+
325
+
326
+ @register_operator(OperatorCategory.POINT)
327
+ def replace(f: Union[Expr, str], old: Any = None, new: Any = None, **kwargs) -> Expr:
328
+ """替换值"""
329
+ if old is None or new is None:
330
+ return _ensure_expr(f)
331
+ return _ensure_expr(f).replace(old, new)
332
+
333
+
334
+ @register_operator(OperatorCategory.POINT)
335
+ def fetch(f: Union[Expr, str], index: int = 0, **kwargs) -> Expr:
336
+ """取第 n 行"""
337
+ e = _ensure_expr(f)
338
+ if index == 0:
339
+ return e.first()
340
+ elif index == -1:
341
+ return e.last()
342
+ return e.nth(index)
343
+
344
+
345
+ @register_operator(OperatorCategory.POINT)
346
+ def where(condition: Union[Expr, str], true_val: Any = None,
347
+ false_val: Any = None, **kwargs) -> Expr:
348
+ """条件选择"""
349
+ c = _ensure_expr(condition)
350
+ t = _ensure_expr(true_val) if true_val is not None else pl.lit(None)
351
+ f = _ensure_expr(false_val) if false_val is not None else pl.lit(None)
352
+ return pl.when(c).then(t).otherwise(f)
353
+
354
+
355
+ @register_operator(OperatorCategory.POINT)
356
+ def fillna(f: Union[Expr, str], value: Any = None,
357
+ method: str = "value", limit: int = 0, **kwargs) -> Expr:
358
+ """填充空值"""
359
+ e = _ensure_expr(f)
360
+ if value is not None:
361
+ return e.fill_null(value)
362
+ elif method == "ffill":
363
+ return e.forward_fill(limit=limit if limit else None)
364
+ elif method == "bfill":
365
+ return e.backward_fill(limit=limit if limit else None)
366
+ return e
367
+
368
+
369
+ # ==============================================================================
370
+ # 组合算子
371
+ # ==============================================================================
372
+
373
+ @register_operator(OperatorCategory.POINT)
374
+ def weighted_sum(factors: List[Union[Expr, str]], weights: Optional[List[float]] = None,
375
+ **kwargs) -> Expr:
376
+ """加权求和"""
377
+ exprs = [_ensure_expr(f) for f in factors]
378
+ if weights is None:
379
+ weights = [1.0] * len(exprs)
380
+ weights_arr = pl.Series(weights)
381
+ weights_arr = weights_arr / weights_arr.sum()
382
+ return sum(e * w for e, w in zip(exprs, weights_arr))
383
+
384
+
385
+ @register_operator(OperatorCategory.POINT)
386
+ def combine(f1: Union[Expr, str], f2: Union[Expr, str],
387
+ method: str = "add", **kwargs) -> Expr:
388
+ """组合两个因子"""
389
+ e1, e2 = _ensure_expr(f1), _ensure_expr(f2)
390
+ if method == "add":
391
+ return e1 + e2
392
+ elif method == "sub":
393
+ return e1 - e2
394
+ elif method == "mul":
395
+ return e1 * e2
396
+ elif method == "div":
397
+ return e1 / (e2 + 1e-10)
398
+ elif method == "max":
399
+ return pl.max_horizontal(e1, e2)
400
+ elif method == "min":
401
+ return pl.min_horizontal(e1, e2)
402
+ return e1
403
+
404
+
405
+ @register_operator(OperatorCategory.POINT)
406
+ def if_then_else(condition: Union[Expr, str], then: Union[Expr, str],
407
+ else_: Union[Expr, str], **kwargs) -> Expr:
408
+ """条件选择"""
409
+ c = _ensure_expr(condition)
410
+ t = _ensure_expr(then)
411
+ e = _ensure_expr(else_)
412
+ return pl.when(c).then(t).otherwise(e)
413
+
414
+
415
+ @register_operator(OperatorCategory.POINT)
416
+ def market_cap(price: Union[Expr, str], shares: Union[Expr, str],
417
+ **kwargs) -> Expr:
418
+ """市值"""
419
+ return _ensure_expr(price) * _ensure_expr(shares)
420
+
421
+
422
+ @register_operator(OperatorCategory.POINT)
423
+ def book_to_market(book_value: Union[Expr, str], market_cap: Union[Expr, str],
424
+ **kwargs) -> Expr:
425
+ """账面市值比"""
426
+ return _ensure_expr(book_value) / (_ensure_expr(market_cap) + 1e-10)
427
+
428
+
429
+ @register_operator(OperatorCategory.POINT)
430
+ def earnings_to_market(earnings: Union[Expr, str], market_cap: Union[Expr, str],
431
+ **kwargs) -> Expr:
432
+ """盈利市值比 (E/P)"""
433
+ return _ensure_expr(earnings) / (_ensure_expr(market_cap) + 1e-10)