powerquant-cli 0.2.6__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.
- powerquant_cli-0.2.6.dist-info/METADATA +62 -0
- powerquant_cli-0.2.6.dist-info/RECORD +355 -0
- powerquant_cli-0.2.6.dist-info/WHEEL +4 -0
- powerquant_cli-0.2.6.dist-info/entry_points.txt +2 -0
- powerquant_cli-0.2.6.dist-info/licenses/LICENSE +36 -0
- powerquant_cli-0.2.6.dist-info/licenses/licenses/LICENSE.vnpy +21 -0
- pqdata/__init__.py +6 -0
- pqdata/client.py +389 -0
- pqdata/download.py +60 -0
- pqdata/exceptions.py +14 -0
- pqdata/schema/__init__.py +1 -0
- pqdata/schema/adjustment_schema.py +67 -0
- pqdata/schema/bar_content_schema.py +24 -0
- pqdata/schema/bar_schema.py +56 -0
- pqdata/schema/corporate_action_schema.py +46 -0
- pqdata/schema/history_schema.py +61 -0
- pqdata/schema/pit_daily_window.py +16 -0
- pqdata/schema/query_schema.py +53 -0
- pqdata/schema/quote_schema.py +73 -0
- pqdata/schema/reference_schema.py +48 -0
- pqdata/schema/reliability_schema.py +152 -0
- quant_cli/__init__.py +0 -0
- quant_cli/api.py +39 -0
- quant_cli/app/__init__.py +0 -0
- quant_cli/app/backtest/__init__.py +0 -0
- quant_cli/app/backtest/cli/__init__.py +0 -0
- quant_cli/app/backtest/cli/local_backtest_cli.py +72 -0
- quant_cli/app/backtest/local_backtest_dependency.py +10 -0
- quant_cli/app/backtest/local_backtest_schema.py +49 -0
- quant_cli/app/backtest/repository/__init__.py +0 -0
- quant_cli/app/backtest/repository/backtest_local_repository.py +67 -0
- quant_cli/app/backtest/service/__init__.py +0 -0
- quant_cli/app/backtest/service/local_backtest_service.py +271 -0
- quant_cli/app/data/__init__.py +0 -0
- quant_cli/app/data/cli/__init__.py +0 -0
- quant_cli/app/data/cli/data_cli.py +78 -0
- quant_cli/app/data/data_dependency.py +35 -0
- quant_cli/app/data/repository/__init__.py +0 -0
- quant_cli/app/data/repository/parquet_repository.py +124 -0
- quant_cli/app/data/schema/__init__.py +0 -0
- quant_cli/app/data/schema/dataset_audit_schema.py +37 -0
- quant_cli/app/data/schema/dataset_request_schema.py +28 -0
- quant_cli/app/data/schema/parquet_file_schema.py +17 -0
- quant_cli/app/data/service/__init__.py +0 -0
- quant_cli/app/data/service/bar_cache_service.py +287 -0
- quant_cli/app/data/service/dataset_audit_service.py +137 -0
- quant_cli/app/data/service/dataset_load_service.py +115 -0
- quant_cli/app/data/service/dataset_prepare_service.py +187 -0
- quant_cli/app/factor/__init__.py +0 -0
- quant_cli/app/factor/cli/__init__.py +0 -0
- quant_cli/app/factor/cli/factor_definition_cli.py +81 -0
- quant_cli/app/factor/cli/local_factor_cli.py +102 -0
- quant_cli/app/factor/cli/local_selection_cli.py +58 -0
- quant_cli/app/factor/interface/__init__.py +0 -0
- quant_cli/app/factor/interface/local_selection_store.py +12 -0
- quant_cli/app/factor/local_factor_dependency.py +38 -0
- quant_cli/app/factor/repository/__init__.py +0 -0
- quant_cli/app/factor/repository/factor_local_repository.py +154 -0
- quant_cli/app/factor/repository/selection_local_repository.py +67 -0
- quant_cli/app/factor/schema/local_selection_schema.py +56 -0
- quant_cli/app/factor/service/__init__.py +0 -0
- quant_cli/app/factor/service/factor_data_prepare_service.py +181 -0
- quant_cli/app/factor/service/local_factor_query_service.py +146 -0
- quant_cli/app/factor/service/local_factor_service.py +120 -0
- quant_cli/app/factor/service/local_selection_service.py +139 -0
- quant_cli/app/job/__init__.py +0 -0
- quant_cli/app/job/cli/__init__.py +0 -0
- quant_cli/app/job/cli/local_job_cli.py +69 -0
- quant_cli/app/job/schema/__init__.py +0 -0
- quant_cli/app/job/schema/local_job_schema.py +11 -0
- quant_cli/app/job/service/__init__.py +0 -0
- quant_cli/app/job/service/local_job_service.py +60 -0
- quant_cli/app/quant_cli.py +49 -0
- quant_cli/app/research/__init__.py +0 -0
- quant_cli/app/research/cli/__init__.py +0 -0
- quant_cli/app/research/cli/local_research_cli.py +93 -0
- quant_cli/app/research/local_research_dependency.py +10 -0
- quant_cli/app/research/repository/__init__.py +0 -0
- quant_cli/app/research/repository/research_local_repository.py +67 -0
- quant_cli/app/research/schema/__init__.py +0 -0
- quant_cli/app/research/schema/local_research_schema.py +26 -0
- quant_cli/app/research/service/__init__.py +0 -0
- quant_cli/app/research/service/local_research_execution_service.py +105 -0
- quant_cli/app/research/service/local_research_service.py +101 -0
- quant_cli/app/strategy/__init__.py +0 -0
- quant_cli/app/strategy/cli/__init__.py +0 -0
- quant_cli/app/strategy/cli/strategy_cli.py +104 -0
- quant_cli/common/__init__.py +0 -0
- quant_cli/common/constants.py +7 -0
- quant_cli/common/database/__init__.py +0 -0
- quant_cli/common/database/duckdb_connection.py +30 -0
- quant_cli/common/http/__init__.py +0 -0
- quant_cli/common/http/definition_client.py +54 -0
- quant_cli/common/utils/__init__.py +0 -0
- quant_cli/common/utils/cli_utils.py +37 -0
- quant_cli/common/utils/platform_utils.py +11 -0
- quant_cli/common/utils/runtime_environment.py +12 -0
- quant_cli/examples/cached_factor_screen.py +165 -0
- quant_cli/examples/factors/ma_close_10.py +10 -0
- quant_cli/examples/factors/max_drawdown_20d.py +18 -0
- quant_cli/examples/factors/momentum_20d.py +13 -0
- quant_cli/examples/factors/multi_intraday_screen.py +16 -0
- quant_cli/examples/factors/risk_adjusted_momentum_20d.py +18 -0
- quant_cli/examples/factors/volume_ratio_5d.py +14 -0
- quant_cli/examples/factors/volume_weighted_close_pressure.py +14 -0
- quant_cli/examples/full_market_screen.py +189 -0
- quant_cli/examples/local_quant_validation.py +196 -0
- quant_cli/examples/published_definition_validation.py +219 -0
- quant_cli/examples/research/factor_pit_coverage.py +87 -0
- quant_cli/examples/research/ipo_first_day.py +348 -0
- quant_cli/examples/selection/README.md +59 -0
- quant_cli/examples/selection/backtest.json +18 -0
- quant_cli/examples/selection/buy_hold.py +7 -0
- quant_cli/examples/selection/compute.json +15 -0
- quant_cli/examples/selection/prepare.json +13 -0
- quant_cli/examples/selection/select.json +44 -0
- quant_cli/examples/strategies/bar_roundtrip.py +12 -0
- quant_cli/examples/strategies/buy_and_hold.py +11 -0
- quant_cli/examples/strategies/contrarian_timing.py +14 -0
- quant_cli/examples/strategies/factor_gate_vwap.py +22 -0
- quant_cli/examples/strategies/ipo_next_day_vwap.py +31 -0
- quant_cli/examples/strategies/momentum_timing.py +14 -0
- quant_cli/examples/strategies/monthly_equal_weight.py +19 -0
- quant_cli/examples/strategies/session_roundtrip.py +16 -0
- quant_cli/examples/strategies/trend_drawdown_guard.py +22 -0
- quant_cli/examples/strategies/weekly_risk_adjusted_rotation.py +61 -0
- quant_cli/examples/unified_snapshot_validation.py +260 -0
- quant_core/__init__.py +0 -0
- quant_core/app/SPEC.md +18 -0
- quant_core/app/__init__.py +0 -0
- quant_core/app/backtest/SPEC.md +65 -0
- quant_core/app/backtest/__init__.py +1 -0
- quant_core/app/backtest/backtest_input_schema.py +20 -0
- quant_core/app/backtest/backtest_run.py +393 -0
- quant_core/app/backtest/data_guard.py +77 -0
- quant_core/app/backtest/historical_clock.py +26 -0
- quant_core/app/backtest/legacy_result_schema.py +34 -0
- quant_core/app/backtest/metrics.py +194 -0
- quant_core/app/backtest/performance_report.py +61 -0
- quant_core/app/backtest/report_schema.py +64 -0
- quant_core/app/backtest/schema.py +477 -0
- quant_core/app/backtest/service/__init__.py +0 -0
- quant_core/app/backtest/service/backtest_execution_service.py +114 -0
- quant_core/app/data/__init__.py +0 -0
- quant_core/app/data/constants.py +284 -0
- quant_core/app/data/domain/__init__.py +0 -0
- quant_core/app/data/domain/trading_quantity_value.py +57 -0
- quant_core/app/data/exceptions.py +95 -0
- quant_core/app/data/interface/__init__.py +0 -0
- quant_core/app/data/interface/adjustment_factor_query.py +21 -0
- quant_core/app/data/interface/market_data_query.py +71 -0
- quant_core/app/data/interface/market_data_verification.py +20 -0
- quant_core/app/data/interface/market_event_source.py +11 -0
- quant_core/app/data/interface/market_revision_query.py +8 -0
- quant_core/app/data/interface/market_runtime_repository.py +44 -0
- quant_core/app/data/interface/market_time_slice_source.py +11 -0
- quant_core/app/data/repository/__init__.py +0 -0
- quant_core/app/data/schema/__init__.py +1 -0
- quant_core/app/data/schema/adjustment_schema.py +67 -0
- quant_core/app/data/schema/corporate_action_schema.py +46 -0
- quant_core/app/data/schema/event_schema.py +506 -0
- quant_core/app/data/schema/exchange_operating_state.py +37 -0
- quant_core/app/data/schema/exchange_state_schema.py +106 -0
- quant_core/app/data/schema/history_schema.py +48 -0
- quant_core/app/data/schema/instrument_identity.py +69 -0
- quant_core/app/data/schema/market_schema.py +128 -0
- quant_core/app/data/schema/pit_daily_window.py +16 -0
- quant_core/app/data/schema/quote_schema.py +73 -0
- quant_core/app/data/schema/reliability_schema.py +152 -0
- quant_core/app/data/schema/snapshot_schema.py +82 -0
- quant_core/app/data/schema/trading_state_schema.py +211 -0
- quant_core/app/data/service/__init__.py +0 -0
- quant_core/app/data/service/calendar_coverage_service.py +63 -0
- quant_core/app/data/service/prepared_data_verification_service.py +40 -0
- quant_core/app/data/service/prepared_market_service.py +189 -0
- quant_core/app/data/service/price_adjustment_service.py +235 -0
- quant_core/app/factor/LICENSE.vnpy +21 -0
- quant_core/app/factor/__init__.py +13 -0
- quant_core/app/factor/builtin/__init__.py +0 -0
- quant_core/app/factor/builtin/alpha101_recipe.py +1095 -0
- quant_core/app/factor/builtin/alpha158_recipe.py +1284 -0
- quant_core/app/factor/builtin/factor_recipe_catalog.py +100 -0
- quant_core/app/factor/computation/__init__.py +0 -0
- quant_core/app/factor/computation/factor_batch_evaluator.py +141 -0
- quant_core/app/factor/computation/factor_expression.py +106 -0
- quant_core/app/factor/computation/factor_source_loader.py +24 -0
- quant_core/app/factor/computation/operators/__init__.py +0 -0
- quant_core/app/factor/computation/operators/arithmetic_operator.py +182 -0
- quant_core/app/factor/computation/operators/cross_section_operator.py +66 -0
- quant_core/app/factor/computation/operators/time_series_operator.py +342 -0
- quant_core/app/factor/computation/panel_evaluator.py +59 -0
- quant_core/app/factor/computation/panel_expression.py +193 -0
- quant_core/app/factor/computation/python_factor_executor.py +87 -0
- quant_core/app/factor/computation/universe_filter.py +25 -0
- quant_core/app/factor/constants.py +114 -0
- quant_core/app/factor/exceptions.py +35 -0
- quant_core/app/factor/factor_dependency.py +65 -0
- quant_core/app/factor/interface/__init__.py +0 -0
- quant_core/app/factor/interface/factor_compute.py +12 -0
- quant_core/app/factor/interface/factor_execution.py +15 -0
- quant_core/app/factor/interface/factor_local_data.py +42 -0
- quant_core/app/factor/interface/factor_panel_compute.py +18 -0
- quant_core/app/factor/interface/factor_panel_input.py +12 -0
- quant_core/app/factor/interface/factor_query_store.py +39 -0
- quant_core/app/factor/interface/public/__init__.py +0 -0
- quant_core/app/factor/interface/public/factor_diagnostics.py +18 -0
- quant_core/app/factor/interface/public/factor_exposure_query.py +12 -0
- quant_core/app/factor/interface/public/factor_local.py +15 -0
- quant_core/app/factor/interface/public/factor_query.py +57 -0
- quant_core/app/factor/interface/public/factor_selection.py +20 -0
- quant_core/app/factor/model/__init__.py +0 -0
- quant_core/app/factor/repository/__init__.py +0 -0
- quant_core/app/factor/schema/__init__.py +1 -0
- quant_core/app/factor/schema/diagnostic_types.py +42 -0
- quant_core/app/factor/schema/factor_artifact_schema.py +36 -0
- quant_core/app/factor/schema/factor_computation_schema.py +102 -0
- quant_core/app/factor/schema/factor_compute_request.py +24 -0
- quant_core/app/factor/schema/factor_contract.py +257 -0
- quant_core/app/factor/schema/factor_input_schema.py +32 -0
- quant_core/app/factor/schema/factor_panel.py +65 -0
- quant_core/app/factor/schema/factor_publication_schema.py +41 -0
- quant_core/app/factor/schema/factor_query_result_schema.py +12 -0
- quant_core/app/factor/schema/factor_result_schema.py +6 -0
- quant_core/app/factor/schema/local_factor_schema.py +42 -0
- quant_core/app/factor/schema/panel_policy.py +13 -0
- quant_core/app/factor/schema/selection_report.py +116 -0
- quant_core/app/factor/schema/selection_types.py +135 -0
- quant_core/app/factor/service/__init__.py +1 -0
- quant_core/app/factor/service/factor_compute_service.py +140 -0
- quant_core/app/factor/service/factor_diagnostic_runner.py +134 -0
- quant_core/app/factor/service/factor_diagnostics_service.py +140 -0
- quant_core/app/factor/service/factor_exposure_service.py +24 -0
- quant_core/app/factor/service/factor_input_service.py +258 -0
- quant_core/app/factor/service/factor_local_execution_service.py +51 -0
- quant_core/app/factor/service/factor_panel_service.py +86 -0
- quant_core/app/factor/service/factor_runtime_service.py +306 -0
- quant_core/app/factor/service/factor_selection_runner.py +88 -0
- quant_core/app/factor/service/published_factor_execution_service.py +160 -0
- quant_core/app/factor/service/selection_service.py +234 -0
- quant_core/app/factor/visibility.py +22 -0
- quant_core/app/opportunity/SPEC.md +45 -0
- quant_core/app/opportunity/__init__.py +1 -0
- quant_core/app/order_execution/SPEC.md +64 -0
- quant_core/app/order_execution/__init__.py +1 -0
- quant_core/app/order_execution/bar_matcher.py +55 -0
- quant_core/app/order_execution/constants.py +142 -0
- quant_core/app/order_execution/domain/__init__.py +45 -0
- quant_core/app/order_execution/domain/execution_batch.py +108 -0
- quant_core/app/order_execution/domain/order_entity.py +197 -0
- quant_core/app/order_execution/domain/order_event.py +87 -0
- quant_core/app/order_execution/domain/order_policy.py +29 -0
- quant_core/app/order_execution/domain/order_value.py +142 -0
- quant_core/app/order_execution/fee_model.py +60 -0
- quant_core/app/order_execution/interface/__init__.py +0 -0
- quant_core/app/order_execution/interface/order_account.py +10 -0
- quant_core/app/order_execution/interface/order_trading_rules.py +10 -0
- quant_core/app/order_execution/interface/public/__init__.py +0 -0
- quant_core/app/order_execution/interface/public/execution_account.py +53 -0
- quant_core/app/order_execution/interface/public/order_events.py +19 -0
- quant_core/app/order_execution/interface/public/order_execution.py +107 -0
- quant_core/app/order_execution/interface/public/order_runtime.py +30 -0
- quant_core/app/order_execution/interface/public/order_submission.py +17 -0
- quant_core/app/order_execution/order_event_bus.py +28 -0
- quant_core/app/order_execution/order_execution_dependency.py +48 -0
- quant_core/app/order_execution/order_submission_service.py +301 -0
- quant_core/app/order_execution/schema/__init__.py +0 -0
- quant_core/app/order_execution/schema/order_request_schema.py +88 -0
- quant_core/app/order_execution/simulation_broker.py +883 -0
- quant_core/app/order_execution/slippage_model.py +40 -0
- quant_core/app/order_execution/tradability_policy.py +174 -0
- quant_core/app/order_execution/validators.py +40 -0
- quant_core/app/portfolio/SPEC.md +69 -0
- quant_core/app/portfolio/__init__.py +0 -0
- quant_core/app/portfolio/domain/__init__.py +32 -0
- quant_core/app/portfolio/domain/entitlement_entity.py +45 -0
- quant_core/app/portfolio/domain/portfolio_entity.py +511 -0
- quant_core/app/portfolio/domain/portfolio_event.py +59 -0
- quant_core/app/portfolio/domain/portfolio_value.py +21 -0
- quant_core/app/portfolio/domain/position_entity.py +165 -0
- quant_core/app/portfolio/domain/reservation_entity.py +69 -0
- quant_core/app/portfolio/interface/__init__.py +0 -0
- quant_core/app/portfolio/interface/public/__init__.py +0 -0
- quant_core/app/portfolio/interface/public/portfolio.py +77 -0
- quant_core/app/portfolio/interface/public/simulation_account.py +16 -0
- quant_core/app/portfolio/ledger_schema.py +130 -0
- quant_core/app/portfolio/order_account_listener.py +36 -0
- quant_core/app/portfolio/portfolio_dependency.py +21 -0
- quant_core/app/portfolio/portfolio_schema.py +204 -0
- quant_core/app/portfolio/simulated_portfolio.py +427 -0
- quant_core/app/research/SPEC.md +57 -0
- quant_core/app/research/__init__.py +0 -0
- quant_core/app/research/adapter/__init__.py +0 -0
- quant_core/app/research/adapter/market_label_query.py +84 -0
- quant_core/app/research/interface/__init__.py +0 -0
- quant_core/app/research/interface/research_dataset.py +47 -0
- quant_core/app/research/interface/research_evaluation.py +19 -0
- quant_core/app/research/schema/__init__.py +0 -0
- quant_core/app/research/schema/comparison_schema.py +12 -0
- quant_core/app/research/schema/dataset_schema.py +112 -0
- quant_core/app/research/schema/research_schema.py +120 -0
- quant_core/app/research/service/__init__.py +0 -0
- quant_core/app/research/service/research_dataset_service.py +80 -0
- quant_core/app/research/service/research_evaluation_service.py +56 -0
- quant_core/app/research/service/research_service.py +171 -0
- quant_core/app/strategy/SPEC.md +215 -0
- quant_core/app/strategy/__init__.py +1 -0
- quant_core/app/strategy/constants.py +87 -0
- quant_core/app/strategy/exceptions.py +32 -0
- quant_core/app/strategy/interface/__init__.py +0 -0
- quant_core/app/strategy/interface/public/__init__.py +0 -0
- quant_core/app/strategy/interface/public/portfolio_construction.py +19 -0
- quant_core/app/strategy/interface/public/strategy.py +30 -0
- quant_core/app/strategy/interface/public/strategy_market_query.py +53 -0
- quant_core/app/strategy/interface/public/strategy_portfolio_reader.py +33 -0
- quant_core/app/strategy/interface/strategy_factor_reader.py +19 -0
- quant_core/app/strategy/interface/strategy_order_reader.py +14 -0
- quant_core/app/strategy/schema/__init__.py +0 -0
- quant_core/app/strategy/schema/portfolio_schema.py +28 -0
- quant_core/app/strategy/schema/portfolio_target.py +70 -0
- quant_core/app/strategy/schema/python_strategy_schema.py +94 -0
- quant_core/app/strategy/schema/screener_schema.py +51 -0
- quant_core/app/strategy/schema/signal_schema.py +33 -0
- quant_core/app/strategy/schema/strategy_publication_schema.py +32 -0
- quant_core/app/strategy/schema/strategy_schema.py +96 -0
- quant_core/app/strategy/service/__init__.py +0 -0
- quant_core/app/strategy/service/factor_top_n_strategy.py +103 -0
- quant_core/app/strategy/service/portfolio_construction_service.py +142 -0
- quant_core/app/strategy/service/python_strategy_service.py +65 -0
- quant_core/app/strategy/service/screener_service.py +77 -0
- quant_core/app/strategy/service/store_factor_reader.py +120 -0
- quant_core/app/strategy/service/strategy_market_view.py +120 -0
- quant_core/app/strategy/service/strategy_runtime_service.py +319 -0
- quant_core/app/strategy/service/strategy_scheduler.py +50 -0
- quant_core/app/strategy/strategies/__init__.py +7 -0
- quant_core/app/strategy/strategies/momentum_volume_breakout.py +157 -0
- quant_core/app/strategy/strategy_context.py +316 -0
- quant_core/app/strategy/strategy_dependency.py +101 -0
- quant_core/app/trading_runtime/SPEC.md +70 -0
- quant_core/app/trading_runtime/__init__.py +1 -0
- quant_core/app/trading_runtime/domain/__init__.py +0 -0
- quant_core/app/trading_runtime/replay/__init__.py +0 -0
- quant_core/app/trading_runtime/replay/daily_state_expand.py +49 -0
- quant_core/app/trading_runtime/replay/exchange_state_resolver.py +90 -0
- quant_core/app/trading_runtime/replay/market_runtime_service.py +573 -0
- quant_core/app/trading_runtime/replay/market_state_resolver.py +150 -0
- quant_core/app/trading_runtime/runtime_ports.py +64 -0
- quant_core/app/trading_runtime/runtime_schema.py +129 -0
- quant_core/app/trading_runtime/runtime_session.py +361 -0
- quant_core/app/trading_runtime/strategy_portfolio_adapter.py +58 -0
- quant_core/app/trading_runtime/trading_rule_book.py +37 -0
- quant_core/app/trading_runtime/trading_rule_query.py +12 -0
- quant_core/common/__init__.py +0 -0
- quant_core/common/exceptions/__init__.py +0 -0
- quant_core/common/exceptions/base.py +89 -0
- quant_core/common/local_run.py +45 -0
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"""基于 Raw Bar 与复权因子生成显式价格视图。"""
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
import json
|
|
5
|
+
from bisect import bisect_right
|
|
6
|
+
from datetime import date
|
|
7
|
+
from decimal import Decimal
|
|
8
|
+
from zoneinfo import ZoneInfo
|
|
9
|
+
|
|
10
|
+
from quant_core.app.data.exceptions import MarketDataIncompleteException
|
|
11
|
+
from quant_core.app.data.interface.adjustment_factor_query import IAdjustmentFactorQuery
|
|
12
|
+
from quant_core.app.data.schema.adjustment_schema import (
|
|
13
|
+
AdjustedBarResponse,
|
|
14
|
+
AdjustedBarSeries,
|
|
15
|
+
AdjustmentAnchorMode,
|
|
16
|
+
AdjustmentType,
|
|
17
|
+
BarViewRequest,
|
|
18
|
+
)
|
|
19
|
+
from quant_core.app.data.schema.adjustment_schema import (
|
|
20
|
+
AdjustmentFactorResponse as AdjustmentFactor,
|
|
21
|
+
)
|
|
22
|
+
from quant_core.app.data.schema.market_schema import BarResponse
|
|
23
|
+
from quant_core.app.data.schema.pit_daily_window import PITDailyWindow
|
|
24
|
+
|
|
25
|
+
_CN = ZoneInfo("Asia/Shanghai")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class PriceAdjustmentService:
|
|
29
|
+
"""生成研究/策略价格视图;绝不修改 Raw Bar。"""
|
|
30
|
+
|
|
31
|
+
def __init__(self, factors: IAdjustmentFactorQuery):
|
|
32
|
+
self.factors = factors
|
|
33
|
+
|
|
34
|
+
@classmethod
|
|
35
|
+
def pit_daily_windows(
|
|
36
|
+
cls,
|
|
37
|
+
raw_bars: list[BarResponse],
|
|
38
|
+
factor_rows: list[AdjustmentFactor],
|
|
39
|
+
days: list[date],
|
|
40
|
+
next_days: list[date],
|
|
41
|
+
lookback: int,
|
|
42
|
+
) -> tuple[PITDailyWindow, ...]:
|
|
43
|
+
"""复用 Market 复权规则,一次装配多日决策时点窗口。"""
|
|
44
|
+
if lookback < 1 or len(days) != len(next_days):
|
|
45
|
+
raise ValueError("PIT 窗口的 lookback 和交易日映射非法")
|
|
46
|
+
by_date = {row.trade_date: row for row in factor_rows}
|
|
47
|
+
ordered = sorted(raw_bars, key=lambda bar: bar.timestamp)
|
|
48
|
+
ordered_days = [bar.timestamp.astimezone(_CN).date() for bar in ordered]
|
|
49
|
+
windows: list[PITDailyWindow] = []
|
|
50
|
+
for day, anchor_day in zip(days, next_days, strict=True):
|
|
51
|
+
if anchor_day <= day:
|
|
52
|
+
raise ValueError("复权锚点必须是目标日之后的交易日")
|
|
53
|
+
cutoff = bisect_right(ordered_days, day)
|
|
54
|
+
raw_window = ordered[max(0, cutoff - lookback) : cutoff]
|
|
55
|
+
if raw_window:
|
|
56
|
+
anchor = by_date.get(anchor_day)
|
|
57
|
+
if anchor is None:
|
|
58
|
+
raise MarketDataIncompleteException(
|
|
59
|
+
dataset="adjustment_factor",
|
|
60
|
+
instrument_id=raw_window[-1].instrument_id,
|
|
61
|
+
reason=f"缺少决策日锚点因子 {anchor_day}",
|
|
62
|
+
)
|
|
63
|
+
request = BarViewRequest(
|
|
64
|
+
adjustment_type=AdjustmentType.PRE,
|
|
65
|
+
anchor_mode=AdjustmentAnchorMode.FIXED_DATE,
|
|
66
|
+
anchor_date=anchor_day,
|
|
67
|
+
)
|
|
68
|
+
adjusted: list[AdjustedBarResponse] = []
|
|
69
|
+
evidence: list[dict] = []
|
|
70
|
+
for bar in raw_window:
|
|
71
|
+
row = by_date.get(bar.timestamp.astimezone(_CN).date())
|
|
72
|
+
if row is None:
|
|
73
|
+
raise MarketDataIncompleteException(
|
|
74
|
+
dataset="adjustment_factor",
|
|
75
|
+
instrument_id=bar.instrument_id,
|
|
76
|
+
reason=f"缺少 {bar.timestamp.astimezone(_CN).date()} 复权因子",
|
|
77
|
+
)
|
|
78
|
+
adjusted.append(cls._adjust_bar(bar, row.factor, anchor.factor, request))
|
|
79
|
+
evidence.append(
|
|
80
|
+
{
|
|
81
|
+
"bar": bar.model_dump(mode="json"),
|
|
82
|
+
"factor": str(row.factor),
|
|
83
|
+
"revision": row.dataset_revision,
|
|
84
|
+
}
|
|
85
|
+
)
|
|
86
|
+
evidence.append(
|
|
87
|
+
{
|
|
88
|
+
"anchor": anchor_day.isoformat(),
|
|
89
|
+
"factor": str(anchor.factor),
|
|
90
|
+
"revision": anchor.dataset_revision,
|
|
91
|
+
}
|
|
92
|
+
)
|
|
93
|
+
else:
|
|
94
|
+
adjusted = []
|
|
95
|
+
evidence = [{"day": day.isoformat(), "bars": "absent"}]
|
|
96
|
+
digest = hashlib.sha256(
|
|
97
|
+
json.dumps(evidence, sort_keys=True, separators=(",", ":")).encode("utf-8")
|
|
98
|
+
).hexdigest()
|
|
99
|
+
windows.append(
|
|
100
|
+
PITDailyWindow(
|
|
101
|
+
as_of=day,
|
|
102
|
+
anchor_date=anchor_day,
|
|
103
|
+
bars=tuple(adjusted),
|
|
104
|
+
input_digest=digest,
|
|
105
|
+
)
|
|
106
|
+
)
|
|
107
|
+
return tuple(windows)
|
|
108
|
+
|
|
109
|
+
async def adjust(self, bars: list[BarResponse], request: BarViewRequest) -> AdjustedBarSeries:
|
|
110
|
+
if not bars:
|
|
111
|
+
raise MarketDataIncompleteException(dataset="bar", reason="没有可复权的 Bar")
|
|
112
|
+
instrument_id = bars[0].instrument_id
|
|
113
|
+
if request.adjustment_type is AdjustmentType.NONE:
|
|
114
|
+
return AdjustedBarSeries(
|
|
115
|
+
instrument_id=instrument_id,
|
|
116
|
+
adjustment_type=AdjustmentType.NONE,
|
|
117
|
+
anchor_date=None,
|
|
118
|
+
factor_revision=None,
|
|
119
|
+
bars=bars,
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
dates = [bar.timestamp.date() for bar in bars]
|
|
123
|
+
if request.adjustment_type is AdjustmentType.POST:
|
|
124
|
+
return await self._adjust_post(bars, request, instrument_id, dates)
|
|
125
|
+
|
|
126
|
+
anchor_limit = self._anchor_limit(request)
|
|
127
|
+
anchor_row = await self.factors.get_adjustment_factor_on_or_before(
|
|
128
|
+
instrument_id, anchor_limit, request.known_at
|
|
129
|
+
)
|
|
130
|
+
if anchor_row is None:
|
|
131
|
+
raise MarketDataIncompleteException(
|
|
132
|
+
dataset="adjustment_factor",
|
|
133
|
+
instrument_id=instrument_id,
|
|
134
|
+
reason="找不到复权锚点因子",
|
|
135
|
+
)
|
|
136
|
+
anchor = anchor_row.trade_date
|
|
137
|
+
factors = await self.factors.list_adjustment_factors(
|
|
138
|
+
instrument_id, min(dates), max(dates), request.known_at
|
|
139
|
+
)
|
|
140
|
+
by_date = {row.trade_date: row for row in factors}
|
|
141
|
+
missing = sorted(set(dates) - by_date.keys())
|
|
142
|
+
if missing:
|
|
143
|
+
raise MarketDataIncompleteException(
|
|
144
|
+
dataset="adjustment_factor",
|
|
145
|
+
instrument_id=instrument_id,
|
|
146
|
+
start=str(min(missing)),
|
|
147
|
+
end=str(max(missing)),
|
|
148
|
+
reason="复权因子缺失,禁止静默使用原始价",
|
|
149
|
+
)
|
|
150
|
+
anchor_factor = anchor_row.factor
|
|
151
|
+
adjusted = [
|
|
152
|
+
self._adjust_bar(bar, by_date[bar.timestamp.date()].factor, anchor_factor, request)
|
|
153
|
+
for bar in bars
|
|
154
|
+
]
|
|
155
|
+
revisions = sorted(
|
|
156
|
+
{by_date[d].dataset_revision for d in set(dates)} | {anchor_row.dataset_revision}
|
|
157
|
+
)
|
|
158
|
+
return AdjustedBarSeries(
|
|
159
|
+
instrument_id=instrument_id,
|
|
160
|
+
adjustment_type=request.adjustment_type,
|
|
161
|
+
anchor_date=anchor,
|
|
162
|
+
factor_revision=",".join(revisions),
|
|
163
|
+
bars=[*adjusted],
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
@staticmethod
|
|
167
|
+
def _anchor_limit(request: BarViewRequest) -> date | None:
|
|
168
|
+
if request.anchor_mode is AdjustmentAnchorMode.FIXED_DATE:
|
|
169
|
+
assert request.anchor_date is not None
|
|
170
|
+
return request.anchor_date
|
|
171
|
+
if request.anchor_mode is AdjustmentAnchorMode.DECISION_TIME:
|
|
172
|
+
assert request.decision_time is not None
|
|
173
|
+
return request.decision_time.date()
|
|
174
|
+
return None
|
|
175
|
+
|
|
176
|
+
async def _adjust_post(
|
|
177
|
+
self,
|
|
178
|
+
bars: list[BarResponse],
|
|
179
|
+
request: BarViewRequest,
|
|
180
|
+
instrument_id: str,
|
|
181
|
+
dates: list[date],
|
|
182
|
+
) -> AdjustedBarSeries:
|
|
183
|
+
factors = await self.factors.list_adjustment_factors(
|
|
184
|
+
instrument_id, min(dates), max(dates), request.known_at
|
|
185
|
+
)
|
|
186
|
+
by_date = {row.trade_date: row for row in factors}
|
|
187
|
+
missing = sorted(set(dates) - by_date.keys())
|
|
188
|
+
if missing:
|
|
189
|
+
raise MarketDataIncompleteException(
|
|
190
|
+
dataset="adjustment_factor",
|
|
191
|
+
instrument_id=instrument_id,
|
|
192
|
+
start=str(min(missing)),
|
|
193
|
+
end=str(max(missing)),
|
|
194
|
+
reason="复权因子缺失,禁止静默使用原始价",
|
|
195
|
+
)
|
|
196
|
+
adjusted = [
|
|
197
|
+
self._adjust_bar(bar, by_date[bar.timestamp.date()].factor, Decimal("1"), request)
|
|
198
|
+
for bar in bars
|
|
199
|
+
]
|
|
200
|
+
revisions = sorted({by_date[d].dataset_revision for d in set(dates)})
|
|
201
|
+
return AdjustedBarSeries(
|
|
202
|
+
instrument_id=instrument_id,
|
|
203
|
+
adjustment_type=request.adjustment_type,
|
|
204
|
+
anchor_date=None,
|
|
205
|
+
factor_revision=",".join(revisions),
|
|
206
|
+
bars=[*adjusted],
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
@staticmethod
|
|
210
|
+
def _adjust_bar(
|
|
211
|
+
bar: BarResponse,
|
|
212
|
+
factor: Decimal,
|
|
213
|
+
anchor_factor: Decimal,
|
|
214
|
+
request: BarViewRequest,
|
|
215
|
+
) -> AdjustedBarResponse:
|
|
216
|
+
ratio = factor if request.adjustment_type is AdjustmentType.POST else factor / anchor_factor
|
|
217
|
+
if ratio <= 0:
|
|
218
|
+
raise MarketDataIncompleteException(
|
|
219
|
+
dataset="adjustment_factor",
|
|
220
|
+
instrument_id=bar.instrument_id,
|
|
221
|
+
reason="复权比例必须大于零",
|
|
222
|
+
)
|
|
223
|
+
return AdjustedBarResponse(
|
|
224
|
+
**bar.model_dump(
|
|
225
|
+
exclude={"open", "high", "low", "close", "previous_close", "volume", "amount"}
|
|
226
|
+
),
|
|
227
|
+
open=bar.open * ratio,
|
|
228
|
+
high=bar.high * ratio,
|
|
229
|
+
low=bar.low * ratio,
|
|
230
|
+
close=bar.close * ratio,
|
|
231
|
+
previous_close=(bar.previous_close * ratio if bar.previous_close is not None else None),
|
|
232
|
+
volume=Decimal(bar.volume) / ratio,
|
|
233
|
+
# 成交额是实际成交事实,不复权。
|
|
234
|
+
amount=bar.amount,
|
|
235
|
+
)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015-present, Xiaoyou Chen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"""Factor Domain - 来源无关的 Canonical 因子边界(T1 MVP)
|
|
2
|
+
|
|
3
|
+
依赖方向(Group TASK-20260907-2308-factor-runtime-mvp 决策):
|
|
4
|
+
|
|
5
|
+
Tushare / Local Calculator / Future Rust or ML
|
|
6
|
+
→ Provider Adapter (T3)
|
|
7
|
+
→ Canonical Factor Store (T2)
|
|
8
|
+
→ Factor Query Runtime (T4)
|
|
9
|
+
→ Strategy / Selection / Agent
|
|
10
|
+
|
|
11
|
+
本域只拥有 Canonical Contract(T1)与统一查询边界;Provider 专有字段
|
|
12
|
+
止步于 Adapter,Strategy 侧不得感知 Provider / Repository。
|
|
13
|
+
"""
|
|
File without changes
|