Compare commits
51 Commits
v2026.5.15
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 674e4b0b14 | |||
| 828b55c747 | |||
| 596d64280b | |||
| 1683d875a0 | |||
| ed4658ccd0 | |||
| bc39df0ee5 | |||
| 70695d8c92 | |||
| 0533e2db3a | |||
| 716149c06c | |||
| 0628dd528a | |||
| e146ad6e7d | |||
| cf2c4fd179 | |||
| 6ba61ef80b | |||
| e45f990487 | |||
| 8e6c912a07 | |||
| 9a411f2403 | |||
| d2c65c91b7 | |||
| 5078aec840 | |||
| df949ab8ee | |||
| 2e036783bf | |||
| ff145300b4 | |||
| c2de9d8e83 | |||
| baeda3773d | |||
| 725f1845d9 | |||
| e0949a0eaa | |||
| 5d2bcd8366 | |||
| 5181d0e403 | |||
| 1c31fa80d2 | |||
| d3d08276ae | |||
| 80b34280c2 | |||
| 0cfb7625bf | |||
| 4c3653e009 | |||
| 9512a5dd2f | |||
| 4f5e3f7162 | |||
| 89c2ff58f8 | |||
| 0813ce3ffb | |||
| a030554ab6 | |||
| e1d36fc0c7 | |||
| 0dca8e0eff | |||
| 4cf90d83a3 | |||
| 9b4462f880 | |||
| 87b7b2642d | |||
| 5eee5c7c63 | |||
| c6dc1d1474 | |||
| 8c86918970 | |||
| 200d5d1f41 | |||
| 3499d4aa74 | |||
| 7dbd66b467 | |||
| db8b0bf142 | |||
| 6e54471e57 | |||
| 3f383c1a88 |
+1225
-158
File diff suppressed because it is too large
Load Diff
@@ -53,6 +53,14 @@ impl Default for ChinaAShareCostModel {
|
||||
}
|
||||
|
||||
impl ChinaAShareCostModel {
|
||||
pub fn aiquant_rqalpha_default() -> Self {
|
||||
Self {
|
||||
stamp_tax_rate_before_change: 0.0005,
|
||||
stamp_tax_rate_after_change: 0.0005,
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn commission_for(&self, gross_amount: f64) -> f64 {
|
||||
if gross_amount <= 0.0 {
|
||||
return 0.0;
|
||||
|
||||
+637
-110
File diff suppressed because it is too large
Load Diff
+351
-16
@@ -1,12 +1,12 @@
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use chrono::{Datelike, NaiveDate};
|
||||
use chrono::{Datelike, NaiveDate, NaiveTime};
|
||||
use serde::Serialize;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::broker::{BrokerExecutionReport, BrokerSimulator, MatchingType};
|
||||
use crate::cost::CostModel;
|
||||
use crate::data::{BenchmarkSnapshot, DataSet, DataSetError, PriceField};
|
||||
use crate::data::{BenchmarkSnapshot, DataSet, DataSetError, IntradayExecutionQuote, PriceField};
|
||||
use crate::event_bus::{BacktestProcessMod, BacktestProcessModLoader, ProcessEventBus};
|
||||
use crate::events::{
|
||||
AccountEvent, FillEvent, OrderEvent, OrderSide, OrderStatus, PositionEvent, ProcessEvent,
|
||||
@@ -20,7 +20,10 @@ use crate::metrics::{BacktestMetrics, compute_backtest_metrics};
|
||||
use crate::portfolio::{CashReceivable, HoldingSummary, PortfolioState};
|
||||
use crate::rules::EquityRuleHooks;
|
||||
use crate::scheduler::{ScheduleRule, ScheduleStage, Scheduler, default_stage_time};
|
||||
use crate::strategy::{Strategy, StrategyContext};
|
||||
use crate::strategy::{
|
||||
OpenOrderView, OrderIntent, Strategy, StrategyContext, StrategyDecision,
|
||||
TargetPortfolioOrderPricing,
|
||||
};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum BacktestError {
|
||||
@@ -95,6 +98,18 @@ pub struct BacktestResult {
|
||||
pub metrics: BacktestMetrics,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ExecutionQuoteRequest {
|
||||
pub date: NaiveDate,
|
||||
pub start_time: Option<chrono::NaiveTime>,
|
||||
pub end_time: Option<chrono::NaiveTime>,
|
||||
pub symbols: BTreeSet<String>,
|
||||
}
|
||||
|
||||
type ExecutionQuoteLoader = Box<
|
||||
dyn FnMut(ExecutionQuoteRequest) -> Result<Vec<IntradayExecutionQuote>, BacktestError> + Send,
|
||||
>;
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct AnalyzerTradeRow {
|
||||
#[serde(with = "date_format")]
|
||||
@@ -313,6 +328,8 @@ pub struct BacktestEngine<S, C, R> {
|
||||
broker: BrokerSimulator<C, R>,
|
||||
config: BacktestConfig,
|
||||
dividend_reinvestment: bool,
|
||||
cash_dividends_enabled: bool,
|
||||
cash_dividend_adjusts_cost_basis: bool,
|
||||
process_event_bus: ProcessEventBus,
|
||||
dynamic_universe: Option<BTreeSet<String>>,
|
||||
subscriptions: BTreeSet<String>,
|
||||
@@ -323,6 +340,9 @@ pub struct BacktestEngine<S, C, R> {
|
||||
futures_settlement_price_mode: String,
|
||||
futures_cost_model: FuturesTransactionCostModel,
|
||||
futures_validation_config: FuturesValidationConfig,
|
||||
execution_quote_loader: Option<ExecutionQuoteLoader>,
|
||||
execution_quote_request_cache:
|
||||
BTreeSet<(NaiveDate, String, Option<NaiveTime>, Option<NaiveTime>)>,
|
||||
}
|
||||
|
||||
impl<S, C, R> BacktestEngine<S, C, R> {
|
||||
@@ -338,6 +358,8 @@ impl<S, C, R> BacktestEngine<S, C, R> {
|
||||
broker,
|
||||
config,
|
||||
dividend_reinvestment: false,
|
||||
cash_dividends_enabled: true,
|
||||
cash_dividend_adjusts_cost_basis: true,
|
||||
process_event_bus: ProcessEventBus::new(),
|
||||
dynamic_universe: None,
|
||||
subscriptions: BTreeSet::new(),
|
||||
@@ -348,14 +370,40 @@ impl<S, C, R> BacktestEngine<S, C, R> {
|
||||
futures_settlement_price_mode: "close".to_string(),
|
||||
futures_cost_model: FuturesTransactionCostModel::default(),
|
||||
futures_validation_config: FuturesValidationConfig::default(),
|
||||
execution_quote_loader: None,
|
||||
execution_quote_request_cache: BTreeSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_data(self) -> DataSet {
|
||||
self.data
|
||||
}
|
||||
|
||||
pub fn with_execution_quote_loader<F>(mut self, loader: F) -> Self
|
||||
where
|
||||
F: FnMut(ExecutionQuoteRequest) -> Result<Vec<IntradayExecutionQuote>, BacktestError>
|
||||
+ Send
|
||||
+ 'static,
|
||||
{
|
||||
self.execution_quote_loader = Some(Box::new(loader));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_dividend_reinvestment(mut self, enabled: bool) -> Self {
|
||||
self.dividend_reinvestment = enabled;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_cash_dividends(mut self, enabled: bool) -> Self {
|
||||
self.cash_dividends_enabled = enabled;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_cash_dividend_cost_basis_adjustment(mut self, enabled: bool) -> Self {
|
||||
self.cash_dividend_adjusts_cost_basis = enabled;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_futures_account(mut self, account: FuturesAccountState) -> Self {
|
||||
self.futures_account = Some(account);
|
||||
self
|
||||
@@ -460,6 +508,120 @@ where
|
||||
C: CostModel,
|
||||
R: EquityRuleHooks,
|
||||
{
|
||||
fn ensure_execution_quotes_for_decision(
|
||||
&mut self,
|
||||
execution_date: NaiveDate,
|
||||
portfolio: &PortfolioState,
|
||||
open_orders: &[OpenOrderView],
|
||||
decision: &StrategyDecision,
|
||||
start_time: Option<chrono::NaiveTime>,
|
||||
end_time: Option<chrono::NaiveTime>,
|
||||
) -> Result<(), BacktestError> {
|
||||
if self.execution_quote_loader.is_none() {
|
||||
return Ok(());
|
||||
}
|
||||
if self.broker.execution_price_field() != PriceField::Last
|
||||
&& !decision_has_algo_execution(decision)
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let caller_start_time = start_time;
|
||||
let caller_end_time = end_time;
|
||||
let start_time = caller_start_time.or_else(|| self.broker.intraday_execution_start_time());
|
||||
let mut symbols = execution_quote_symbols_for_decision(decision, portfolio, open_orders);
|
||||
self.load_missing_execution_quotes(execution_date, start_time, end_time, &mut symbols)?;
|
||||
|
||||
if caller_start_time.is_none() && caller_end_time.is_none() {
|
||||
for ((intent_start_time, intent_end_time), mut intent_symbols) in
|
||||
algo_execution_quote_windows_for_decision(decision, portfolio)
|
||||
{
|
||||
self.load_missing_execution_quotes(
|
||||
execution_date,
|
||||
intent_start_time,
|
||||
intent_end_time,
|
||||
&mut intent_symbols,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_missing_execution_quotes(
|
||||
&mut self,
|
||||
execution_date: NaiveDate,
|
||||
start_time: Option<NaiveTime>,
|
||||
end_time: Option<NaiveTime>,
|
||||
symbols: &mut BTreeSet<String>,
|
||||
) -> Result<(), BacktestError> {
|
||||
symbols.retain(|symbol| {
|
||||
let request_key = (execution_date, symbol.clone(), start_time, end_time);
|
||||
if self.execution_quote_request_cache.contains(&request_key) {
|
||||
return false;
|
||||
}
|
||||
if start_time.is_some() && end_time.is_none() {
|
||||
return true;
|
||||
}
|
||||
!has_execution_quote_in_window(&self.data, execution_date, symbol, start_time, end_time)
|
||||
});
|
||||
if symbols.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let requested_symbols = symbols.iter().cloned().collect::<Vec<_>>();
|
||||
let request = ExecutionQuoteRequest {
|
||||
date: execution_date,
|
||||
start_time,
|
||||
end_time,
|
||||
symbols: std::mem::take(symbols),
|
||||
};
|
||||
let quotes = self
|
||||
.execution_quote_loader
|
||||
.as_mut()
|
||||
.expect("checked execution quote loader")
|
||||
.as_mut()(request)?;
|
||||
self.data.add_execution_quotes(quotes);
|
||||
for symbol in requested_symbols {
|
||||
self.execution_quote_request_cache.insert((
|
||||
execution_date,
|
||||
symbol,
|
||||
start_time,
|
||||
end_time,
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn ensure_execution_quotes_for_portfolio_times(
|
||||
&mut self,
|
||||
execution_date: NaiveDate,
|
||||
portfolio: &PortfolioState,
|
||||
quote_times: &[NaiveTime],
|
||||
) -> Result<(), BacktestError> {
|
||||
if self.execution_quote_loader.is_none() || quote_times.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let base_symbols = portfolio
|
||||
.positions()
|
||||
.keys()
|
||||
.cloned()
|
||||
.collect::<BTreeSet<_>>();
|
||||
if base_symbols.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
for quote_time in quote_times {
|
||||
let mut symbols = base_symbols.clone();
|
||||
self.load_missing_execution_quotes(
|
||||
execution_date,
|
||||
Some(*quote_time),
|
||||
None,
|
||||
&mut symbols,
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn apply_strategy_directives(
|
||||
&mut self,
|
||||
execution_date: NaiveDate,
|
||||
@@ -1721,6 +1883,15 @@ where
|
||||
&mut auction_decision,
|
||||
&mut directive_report,
|
||||
)?;
|
||||
let pre_auction_execution_orders = self.open_order_views();
|
||||
self.ensure_execution_quotes_for_decision(
|
||||
execution_date,
|
||||
&portfolio,
|
||||
&pre_auction_execution_orders,
|
||||
&auction_decision,
|
||||
None,
|
||||
None,
|
||||
)?;
|
||||
let mut report = self.broker.execute(
|
||||
execution_date,
|
||||
&mut portfolio,
|
||||
@@ -1779,6 +1950,12 @@ where
|
||||
"on_day:pre",
|
||||
)?;
|
||||
let on_day_open_orders = self.open_order_views();
|
||||
let decision_quote_times = self.strategy.decision_quote_times();
|
||||
self.ensure_execution_quotes_for_portfolio_times(
|
||||
execution_date,
|
||||
&portfolio,
|
||||
&decision_quote_times,
|
||||
)?;
|
||||
let mut decision = decision_slot
|
||||
.map(|(decision_idx, decision_date)| {
|
||||
self.strategy.on_day(&StrategyContext {
|
||||
@@ -1925,6 +2102,15 @@ where
|
||||
&mut directive_report,
|
||||
)?;
|
||||
|
||||
let pre_intraday_execution_orders = self.open_order_views();
|
||||
self.ensure_execution_quotes_for_decision(
|
||||
execution_date,
|
||||
&portfolio,
|
||||
&pre_intraday_execution_orders,
|
||||
&decision,
|
||||
None,
|
||||
None,
|
||||
)?;
|
||||
let mut intraday_report =
|
||||
self.broker
|
||||
.execute(execution_date, &mut portfolio, &self.data, &decision)?;
|
||||
@@ -2082,6 +2268,15 @@ where
|
||||
&mut tick_decision,
|
||||
&mut directive_report,
|
||||
)?;
|
||||
let pre_tick_execution_orders = self.open_order_views();
|
||||
self.ensure_execution_quotes_for_decision(
|
||||
execution_date,
|
||||
&portfolio,
|
||||
&pre_tick_execution_orders,
|
||||
&tick_decision,
|
||||
Some(tick_time),
|
||||
Some(tick_time),
|
||||
)?;
|
||||
let mut tick_report = self.broker.execute_between(
|
||||
execution_date,
|
||||
&mut portfolio,
|
||||
@@ -2521,13 +2716,17 @@ where
|
||||
continue;
|
||||
}
|
||||
|
||||
if action.share_cash.abs() > f64::EPSILON {
|
||||
if self.cash_dividends_enabled && action.share_cash.abs() > f64::EPSILON {
|
||||
let cash_before = portfolio.cash();
|
||||
let (cash_delta, quantity_after, average_cost) = {
|
||||
let position = portfolio
|
||||
.position_mut_if_exists(&action.symbol)
|
||||
.expect("position exists for dividend action");
|
||||
let cash_delta = position.apply_cash_dividend(action.share_cash);
|
||||
let cash_delta = if self.cash_dividend_adjusts_cost_basis {
|
||||
position.apply_cash_dividend(action.share_cash)
|
||||
} else {
|
||||
position.apply_cash_dividend_preserve_cost_basis(action.share_cash)
|
||||
};
|
||||
(cash_delta, position.quantity, position.average_cost)
|
||||
};
|
||||
if cash_delta.abs() > f64::EPSILON {
|
||||
@@ -2990,24 +3189,17 @@ where
|
||||
}
|
||||
|
||||
let quantity = position.quantity;
|
||||
let fallback_reference_price = if position.last_price > 0.0 {
|
||||
let settlement_price = if position.last_price.is_finite() && position.last_price > 0.0 {
|
||||
position.last_price
|
||||
} else {
|
||||
} else if position.average_cost.is_finite() && position.average_cost > 0.0 {
|
||||
position.average_cost
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
let effective_delisted_at = instrument
|
||||
.delisted_at
|
||||
.or_else(|| self.data.calendar().previous_day(date))
|
||||
.unwrap_or(date);
|
||||
let settlement_price = self
|
||||
.data
|
||||
.price_on_or_before(effective_delisted_at, &symbol, PriceField::Close)
|
||||
.or_else(|| {
|
||||
self.data
|
||||
.price_on_or_before(date, &symbol, PriceField::Close)
|
||||
})
|
||||
.filter(|price| price.is_finite() && *price > 0.0)
|
||||
.unwrap_or(fallback_reference_price);
|
||||
if !settlement_price.is_finite() || settlement_price <= 0.0 {
|
||||
return Err(BacktestError::Execution(format!(
|
||||
"missing delisting settlement price for {} on {}",
|
||||
@@ -3077,6 +3269,149 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn has_execution_quote_in_window(
|
||||
data: &DataSet,
|
||||
date: NaiveDate,
|
||||
symbol: &str,
|
||||
start_time: Option<chrono::NaiveTime>,
|
||||
end_time: Option<chrono::NaiveTime>,
|
||||
) -> bool {
|
||||
let start_cursor = start_time.map(|time| date.and_time(time));
|
||||
let end_cursor = end_time.map(|time| date.and_time(time));
|
||||
if let Some(cursor) = start_cursor
|
||||
&& end_cursor.is_none()
|
||||
{
|
||||
return data
|
||||
.execution_quotes_on(date, symbol)
|
||||
.iter()
|
||||
.any(|quote| quote.timestamp <= cursor);
|
||||
}
|
||||
data.execution_quotes_on(date, symbol).iter().any(|quote| {
|
||||
!start_cursor.is_some_and(|cursor| quote.timestamp < cursor)
|
||||
&& !end_cursor.is_some_and(|cursor| quote.timestamp > cursor)
|
||||
})
|
||||
}
|
||||
|
||||
fn decision_has_algo_execution(decision: &StrategyDecision) -> bool {
|
||||
decision.order_intents.iter().any(|intent| {
|
||||
matches!(
|
||||
intent,
|
||||
OrderIntent::AlgoValue { .. }
|
||||
| OrderIntent::AlgoPercent { .. }
|
||||
| OrderIntent::TargetPortfolioSmart {
|
||||
order_prices: Some(TargetPortfolioOrderPricing::AlgoOrder { .. }),
|
||||
..
|
||||
}
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn execution_quote_symbols_for_decision(
|
||||
decision: &StrategyDecision,
|
||||
portfolio: &PortfolioState,
|
||||
open_orders: &[OpenOrderView],
|
||||
) -> BTreeSet<String> {
|
||||
let mut symbols = BTreeSet::new();
|
||||
symbols.extend(open_orders.iter().map(|order| order.symbol.clone()));
|
||||
if decision.rebalance {
|
||||
symbols.extend(portfolio.positions().keys().cloned());
|
||||
symbols.extend(decision.target_weights.keys().cloned());
|
||||
}
|
||||
if !decision.exit_symbols.is_empty() {
|
||||
symbols.extend(decision.exit_symbols.iter().cloned());
|
||||
}
|
||||
|
||||
for intent in &decision.order_intents {
|
||||
match intent {
|
||||
OrderIntent::Shares { symbol, .. }
|
||||
| OrderIntent::LimitShares { symbol, .. }
|
||||
| OrderIntent::Lots { symbol, .. }
|
||||
| OrderIntent::LimitLots { symbol, .. }
|
||||
| OrderIntent::TargetShares { symbol, .. }
|
||||
| OrderIntent::LimitTargetShares { symbol, .. }
|
||||
| OrderIntent::TargetValue { symbol, .. }
|
||||
| OrderIntent::LimitTargetValue { symbol, .. }
|
||||
| OrderIntent::Value { symbol, .. }
|
||||
| OrderIntent::LimitValue { symbol, .. }
|
||||
| OrderIntent::Percent { symbol, .. }
|
||||
| OrderIntent::LimitPercent { symbol, .. }
|
||||
| OrderIntent::TargetPercent { symbol, .. }
|
||||
| OrderIntent::LimitTargetPercent { symbol, .. }
|
||||
| OrderIntent::AlgoValue { symbol, .. }
|
||||
| OrderIntent::AlgoPercent { symbol, .. }
|
||||
| OrderIntent::CancelSymbol { symbol, .. } => {
|
||||
symbols.insert(symbol.clone());
|
||||
}
|
||||
OrderIntent::TargetPortfolioSmart { target_weights, .. } => {
|
||||
symbols.extend(portfolio.positions().keys().cloned());
|
||||
symbols.extend(target_weights.keys().cloned());
|
||||
}
|
||||
OrderIntent::CancelAll { .. } => {
|
||||
symbols.extend(open_orders.iter().map(|order| order.symbol.clone()));
|
||||
}
|
||||
OrderIntent::UpdateUniverse { .. }
|
||||
| OrderIntent::Subscribe { .. }
|
||||
| OrderIntent::Unsubscribe { .. }
|
||||
| OrderIntent::DepositWithdraw { .. }
|
||||
| OrderIntent::FinanceRepay { .. }
|
||||
| OrderIntent::SetManagementFeeRate { .. }
|
||||
| OrderIntent::CancelOrder { .. }
|
||||
| OrderIntent::Futures { .. } => {}
|
||||
}
|
||||
}
|
||||
|
||||
symbols.retain(|symbol| !symbol.trim().is_empty());
|
||||
symbols
|
||||
}
|
||||
|
||||
fn algo_execution_quote_windows_for_decision(
|
||||
decision: &StrategyDecision,
|
||||
portfolio: &PortfolioState,
|
||||
) -> BTreeMap<(Option<NaiveTime>, Option<NaiveTime>), BTreeSet<String>> {
|
||||
let mut groups = BTreeMap::<(Option<NaiveTime>, Option<NaiveTime>), BTreeSet<String>>::new();
|
||||
for intent in &decision.order_intents {
|
||||
match intent {
|
||||
OrderIntent::AlgoValue {
|
||||
symbol,
|
||||
start_time,
|
||||
end_time,
|
||||
..
|
||||
}
|
||||
| OrderIntent::AlgoPercent {
|
||||
symbol,
|
||||
start_time,
|
||||
end_time,
|
||||
..
|
||||
} => {
|
||||
if start_time.is_some() || end_time.is_some() {
|
||||
groups
|
||||
.entry((*start_time, *end_time))
|
||||
.or_default()
|
||||
.insert(symbol.clone());
|
||||
}
|
||||
}
|
||||
OrderIntent::TargetPortfolioSmart {
|
||||
target_weights,
|
||||
order_prices:
|
||||
Some(TargetPortfolioOrderPricing::AlgoOrder {
|
||||
start_time,
|
||||
end_time,
|
||||
..
|
||||
}),
|
||||
..
|
||||
} => {
|
||||
if start_time.is_some() || end_time.is_some() {
|
||||
let symbols = groups.entry((*start_time, *end_time)).or_default();
|
||||
symbols.extend(portfolio.positions().keys().cloned());
|
||||
symbols.extend(target_weights.keys().cloned());
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
groups
|
||||
}
|
||||
|
||||
fn collect_scheduled_decisions<S: Strategy>(
|
||||
strategy: &mut S,
|
||||
scheduler: &Scheduler<'_>,
|
||||
|
||||
@@ -43,7 +43,7 @@ impl Instrument {
|
||||
pub fn is_active_on(&self, date: NaiveDate) -> bool {
|
||||
self.listed_at.is_none_or(|listed_at| listed_at <= date)
|
||||
&& !self.is_delisted_before(date)
|
||||
&& !self.status.eq_ignore_ascii_case("inactive")
|
||||
&& !(self.status.eq_ignore_ascii_case("inactive") && self.delisted_at.is_none())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,13 +12,16 @@ pub mod platform_expr_strategy;
|
||||
pub mod platform_runtime_schema;
|
||||
pub mod platform_strategy_spec;
|
||||
pub mod portfolio;
|
||||
pub mod risk_control;
|
||||
pub mod rules;
|
||||
pub mod scheduler;
|
||||
pub mod strategy;
|
||||
pub mod strategy_ai;
|
||||
pub mod universe;
|
||||
|
||||
pub use broker::{BrokerExecutionReport, BrokerSimulator, MatchingType, SlippageModel};
|
||||
pub use broker::{
|
||||
BrokerExecutionReport, BrokerSimulator, DynamicSlippageConfig, MatchingType, SlippageModel,
|
||||
};
|
||||
pub use calendar::TradingCalendar;
|
||||
pub use cost::{ChinaAShareCostModel, CostModel, TradingCost};
|
||||
pub use data::{
|
||||
@@ -31,7 +34,7 @@ pub use data::{
|
||||
pub use engine::{
|
||||
AnalyzerMonthlyReturnRow, AnalyzerPositionRow, AnalyzerReport, AnalyzerRiskSummary,
|
||||
AnalyzerTradeRow, BacktestConfig, BacktestDayProgress, BacktestEngine, BacktestError,
|
||||
BacktestResult, DailyEquityPoint, FuturesValidationConfig,
|
||||
BacktestResult, DailyEquityPoint, ExecutionQuoteRequest, FuturesValidationConfig,
|
||||
};
|
||||
pub use event_bus::{BacktestProcessMod, BacktestProcessModLoader, ProcessEventBus};
|
||||
pub use events::{
|
||||
@@ -48,8 +51,8 @@ pub use metrics::{BacktestMetrics, compute_backtest_metrics};
|
||||
pub use platform_expr_strategy::{
|
||||
PlatformAccountActionKind, PlatformExplicitActionStage, PlatformExplicitCancelKind,
|
||||
PlatformExplicitOrderKind, PlatformExprStrategy, PlatformExprStrategyConfig,
|
||||
PlatformRebalanceSchedule, PlatformScheduleFrequency, PlatformTradeAction,
|
||||
PlatformUniverseActionKind,
|
||||
PlatformRebalanceSchedule, PlatformScheduleFrequency, PlatformSelectionQuotePlan,
|
||||
PlatformTradeAction, PlatformUniverseActionKind,
|
||||
};
|
||||
pub use platform_runtime_schema::{
|
||||
PLATFORM_RUNTIME_SCHEMA_VERSION, PlatformRuntimeSchema, reserved_scope_names,
|
||||
@@ -66,6 +69,7 @@ pub use platform_strategy_spec::{
|
||||
StrategyRuntimeSpec, platform_expr_config_from_spec, platform_expr_config_from_value,
|
||||
};
|
||||
pub use portfolio::{CashReceivable, HoldingSummary, PendingCashFlow, PortfolioState, Position};
|
||||
pub use risk_control::ChinaAShareRiskControl;
|
||||
pub use rules::{ChinaEquityRuleHooks, EquityRuleHooks, RuleCheck};
|
||||
pub use scheduler::{
|
||||
ScheduleFrequency, ScheduleRule, ScheduleStage, ScheduleTimeRule, Scheduler, default_stage_time,
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -5,9 +5,10 @@ use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::{
|
||||
PlatformAccountActionKind, PlatformExplicitActionStage, PlatformExplicitCancelKind,
|
||||
PlatformExplicitOrderKind, PlatformExprStrategyConfig, PlatformRebalanceSchedule,
|
||||
PlatformScheduleFrequency, PlatformTradeAction, PlatformUniverseActionKind, ScheduleTimeRule,
|
||||
DynamicSlippageConfig, MatchingType, PlatformAccountActionKind, PlatformExplicitActionStage,
|
||||
PlatformExplicitCancelKind, PlatformExplicitOrderKind, PlatformExprStrategyConfig,
|
||||
PlatformRebalanceSchedule, PlatformScheduleFrequency, PlatformTradeAction,
|
||||
PlatformUniverseActionKind, ScheduleTimeRule, SlippageModel,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
|
||||
@@ -22,6 +23,10 @@ pub struct StrategyRuntimeSpec {
|
||||
#[serde(default)]
|
||||
pub universe: Option<StrategyUniverseSpec>,
|
||||
#[serde(default)]
|
||||
pub rebalance: Option<StrategyRebalanceSpec>,
|
||||
#[serde(default)]
|
||||
pub trade_times: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub signal_symbol: Option<String>,
|
||||
#[serde(default)]
|
||||
pub execution: Option<StrategyExecutionSpec>,
|
||||
@@ -49,9 +54,18 @@ pub struct StrategyUniverseSpec {
|
||||
pub exclude: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct StrategyRebalanceSpec {
|
||||
#[serde(default)]
|
||||
pub trade_times: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct StrategyExecutionSpec {
|
||||
#[serde(default)]
|
||||
pub compatibility_profile: Option<String>,
|
||||
#[serde(default)]
|
||||
pub matching_type: Option<String>,
|
||||
#[serde(default)]
|
||||
@@ -59,6 +73,20 @@ pub struct StrategyExecutionSpec {
|
||||
#[serde(default)]
|
||||
pub slippage_value: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub slippage_impact_coefficient: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub slippage_volatility_coefficient: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub slippage_max_value: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub commission_rate: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub minimum_commission: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub stamp_tax_rate_before_change: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub stamp_tax_rate_after_change: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub strict_value_budget: Option<bool>,
|
||||
}
|
||||
|
||||
@@ -94,6 +122,20 @@ pub struct StrategyEngineConfig {
|
||||
#[serde(default)]
|
||||
pub slippage_value: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub slippage_impact_coefficient: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub slippage_volatility_coefficient: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub slippage_max_value: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub commission_rate: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub minimum_commission: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub stamp_tax_rate_before_change: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub stamp_tax_rate_after_change: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub strict_value_budget: Option<bool>,
|
||||
#[serde(default)]
|
||||
pub dividend_reinvestment: Option<bool>,
|
||||
@@ -135,6 +177,10 @@ pub struct MovingAverageFilterConfig {
|
||||
#[serde(default)]
|
||||
pub long_days: Option<usize>,
|
||||
#[serde(default)]
|
||||
pub volume_short_days: Option<usize>,
|
||||
#[serde(default)]
|
||||
pub volume_long_days: Option<usize>,
|
||||
#[serde(default)]
|
||||
pub rsi_rate: Option<f64>,
|
||||
}
|
||||
|
||||
@@ -244,6 +290,8 @@ pub struct StrategyExpressionTradingConfig {
|
||||
#[serde(default)]
|
||||
pub stage: Option<String>,
|
||||
#[serde(default)]
|
||||
pub refresh_rate_expr: Option<String>,
|
||||
#[serde(default)]
|
||||
pub schedule: Option<StrategyExpressionScheduleConfig>,
|
||||
#[serde(default)]
|
||||
pub rotation_enabled: Option<bool>,
|
||||
@@ -252,6 +300,12 @@ pub struct StrategyExpressionTradingConfig {
|
||||
#[serde(default)]
|
||||
pub retry_empty_rebalance: Option<bool>,
|
||||
#[serde(default)]
|
||||
pub delayed_limit_open_exit: Option<bool>,
|
||||
#[serde(default)]
|
||||
pub delayed_limit_open_exit_time: Option<String>,
|
||||
#[serde(default)]
|
||||
pub release_slot_on_exit_signal: Option<bool>,
|
||||
#[serde(default)]
|
||||
pub subscription_guard_required: Option<bool>,
|
||||
#[serde(default)]
|
||||
pub actions: Vec<StrategyExpressionActionConfig>,
|
||||
@@ -327,6 +381,231 @@ pub fn platform_expr_config_from_value(
|
||||
))
|
||||
}
|
||||
|
||||
fn valid_non_negative(value: Option<f64>) -> Option<f64> {
|
||||
value.filter(|item| item.is_finite() && *item >= 0.0)
|
||||
}
|
||||
|
||||
fn apply_cost_overrides(
|
||||
cfg: &mut PlatformExprStrategyConfig,
|
||||
commission_rate: Option<f64>,
|
||||
minimum_commission: Option<f64>,
|
||||
stamp_tax_rate_before_change: Option<f64>,
|
||||
stamp_tax_rate_after_change: Option<f64>,
|
||||
) {
|
||||
if let Some(value) = valid_non_negative(commission_rate) {
|
||||
cfg.commission_rate = Some(value);
|
||||
}
|
||||
if let Some(value) = valid_non_negative(minimum_commission) {
|
||||
cfg.minimum_commission = Some(value);
|
||||
}
|
||||
if let Some(value) = valid_non_negative(stamp_tax_rate_before_change) {
|
||||
cfg.stamp_tax_rate_before_change = Some(value);
|
||||
}
|
||||
if let Some(value) = valid_non_negative(stamp_tax_rate_after_change) {
|
||||
cfg.stamp_tax_rate_after_change = Some(value);
|
||||
}
|
||||
}
|
||||
|
||||
fn normalize_model_name(value: &str) -> String {
|
||||
value.trim().to_ascii_lowercase().replace('-', "_")
|
||||
}
|
||||
|
||||
fn parse_matching_type(value: Option<&str>) -> Option<MatchingType> {
|
||||
match normalize_model_name(value?).as_str() {
|
||||
"open_auction" => Some(MatchingType::OpenAuction),
|
||||
"current_bar_close" => Some(MatchingType::CurrentBarClose),
|
||||
"next_bar_open" => Some(MatchingType::NextBarOpen),
|
||||
"next_tick_last" => Some(MatchingType::NextTickLast),
|
||||
"next_tick_best_own" => Some(MatchingType::NextTickBestOwn),
|
||||
"next_tick_best_counterparty" => Some(MatchingType::NextTickBestCounterparty),
|
||||
"counterparty_offer" => Some(MatchingType::CounterpartyOffer),
|
||||
"vwap" => Some(MatchingType::Vwap),
|
||||
"twap" => Some(MatchingType::Twap),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_slippage_model(
|
||||
model: Option<&str>,
|
||||
value: Option<f64>,
|
||||
impact_coefficient: Option<f64>,
|
||||
volatility_coefficient: Option<f64>,
|
||||
max_value: Option<f64>,
|
||||
) -> Option<SlippageModel> {
|
||||
let value = valid_non_negative(value);
|
||||
let impact_coefficient = valid_non_negative(impact_coefficient);
|
||||
let volatility_coefficient = valid_non_negative(volatility_coefficient);
|
||||
let max_value = valid_non_negative(max_value);
|
||||
let model = model
|
||||
.map(normalize_model_name)
|
||||
.filter(|item| !item.is_empty())
|
||||
.unwrap_or_else(|| {
|
||||
if value.is_some_and(|item| item > 0.0) {
|
||||
"price_ratio".to_string()
|
||||
} else {
|
||||
"none".to_string()
|
||||
}
|
||||
});
|
||||
|
||||
match model.as_str() {
|
||||
"none" => Some(SlippageModel::None),
|
||||
"price_ratio" => Some(SlippageModel::PriceRatio(value.unwrap_or(0.0))),
|
||||
"tick_size" => Some(SlippageModel::TickSize(value.unwrap_or(0.0))),
|
||||
"limit_price" => Some(SlippageModel::LimitPrice),
|
||||
"dynamic" | "dynamic_volume_volatility" => {
|
||||
Some(SlippageModel::Dynamic(DynamicSlippageConfig::new(
|
||||
impact_coefficient.unwrap_or(0.5),
|
||||
volatility_coefficient.unwrap_or(0.3),
|
||||
max_value.or(value).unwrap_or(0.01),
|
||||
)))
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_execution_behavior_overrides(
|
||||
cfg: &mut PlatformExprStrategyConfig,
|
||||
matching_type: Option<&str>,
|
||||
slippage_model: Option<&str>,
|
||||
slippage_value: Option<f64>,
|
||||
slippage_impact_coefficient: Option<f64>,
|
||||
slippage_volatility_coefficient: Option<f64>,
|
||||
slippage_max_value: Option<f64>,
|
||||
strict_value_budget: Option<bool>,
|
||||
) {
|
||||
if let Some(matching_type) = parse_matching_type(matching_type) {
|
||||
cfg.matching_type = matching_type;
|
||||
}
|
||||
if slippage_model.is_some()
|
||||
|| slippage_value.is_some()
|
||||
|| slippage_impact_coefficient.is_some()
|
||||
|| slippage_volatility_coefficient.is_some()
|
||||
|| slippage_max_value.is_some()
|
||||
{
|
||||
if let Some(parsed) = parse_slippage_model(
|
||||
slippage_model,
|
||||
slippage_value,
|
||||
slippage_impact_coefficient,
|
||||
slippage_volatility_coefficient,
|
||||
slippage_max_value,
|
||||
) {
|
||||
cfg.slippage_model = parsed;
|
||||
}
|
||||
}
|
||||
if let Some(enabled) = strict_value_budget {
|
||||
cfg.strict_value_budget = enabled;
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_usize_after(text: &str, start: usize) -> Option<(usize, usize)> {
|
||||
let bytes = text.as_bytes();
|
||||
let mut end = start;
|
||||
while end < bytes.len() && bytes[end].is_ascii_digit() {
|
||||
end += 1;
|
||||
}
|
||||
if end == start {
|
||||
return None;
|
||||
}
|
||||
text[start..end]
|
||||
.parse::<usize>()
|
||||
.ok()
|
||||
.filter(|value| *value > 0)
|
||||
.map(|value| (value, end))
|
||||
}
|
||||
|
||||
fn prefixed_ma_lookbacks(expr: &str, prefix: &str) -> Vec<usize> {
|
||||
let lower = expr.to_ascii_lowercase();
|
||||
let mut values = Vec::new();
|
||||
let mut cursor = 0;
|
||||
while let Some(offset) = lower[cursor..].find(prefix) {
|
||||
let start = cursor + offset + prefix.len();
|
||||
if let Some((value, end)) = parse_usize_after(&lower, start) {
|
||||
values.push(value);
|
||||
cursor = end;
|
||||
} else {
|
||||
cursor = start;
|
||||
}
|
||||
}
|
||||
values
|
||||
}
|
||||
|
||||
fn compact_ascii_whitespace(value: &str) -> String {
|
||||
value
|
||||
.chars()
|
||||
.filter(|ch| !ch.is_ascii_whitespace())
|
||||
.collect::<String>()
|
||||
.to_ascii_lowercase()
|
||||
}
|
||||
|
||||
fn rolling_mean_lookbacks(expr: &str, field: &str) -> Vec<usize> {
|
||||
let compact = compact_ascii_whitespace(expr);
|
||||
let patterns = [
|
||||
format!("rolling_mean(\"{field}\","),
|
||||
format!("rolling_mean('{field}',"),
|
||||
];
|
||||
let mut values = Vec::new();
|
||||
for pattern in patterns {
|
||||
let mut cursor = 0;
|
||||
while let Some(offset) = compact[cursor..].find(&pattern) {
|
||||
let start = cursor + offset + pattern.len();
|
||||
if let Some((value, end)) = parse_usize_after(&compact, start) {
|
||||
values.push(value);
|
||||
cursor = end;
|
||||
} else {
|
||||
cursor = start;
|
||||
}
|
||||
}
|
||||
}
|
||||
values
|
||||
}
|
||||
|
||||
fn sorted_unique_positive(mut values: Vec<usize>) -> Vec<usize> {
|
||||
values.retain(|value| *value > 0);
|
||||
values.sort_unstable();
|
||||
values.dedup();
|
||||
values
|
||||
}
|
||||
|
||||
fn infer_expression_windows(
|
||||
cfg: &mut PlatformExprStrategyConfig,
|
||||
benchmark_short_explicit: bool,
|
||||
benchmark_long_explicit: bool,
|
||||
stock_short_explicit: bool,
|
||||
stock_mid_explicit: bool,
|
||||
stock_long_explicit: bool,
|
||||
) {
|
||||
let mut benchmark_days = Vec::new();
|
||||
for expr in [&cfg.exposure_expr, &cfg.buy_scale_expr] {
|
||||
benchmark_days.extend(prefixed_ma_lookbacks(expr, "benchmark_ma"));
|
||||
benchmark_days.extend(rolling_mean_lookbacks(expr, "benchmark_close"));
|
||||
}
|
||||
let benchmark_days = sorted_unique_positive(benchmark_days);
|
||||
if !benchmark_short_explicit && let Some(short) = benchmark_days.first().copied() {
|
||||
cfg.benchmark_short_ma_days = short;
|
||||
}
|
||||
if !benchmark_long_explicit && let Some(long) = benchmark_days.last().copied() {
|
||||
cfg.benchmark_long_ma_days = long;
|
||||
}
|
||||
|
||||
let mut stock_days = Vec::new();
|
||||
for expr in [&cfg.stock_filter_expr, &cfg.buy_scale_expr] {
|
||||
stock_days.extend(prefixed_ma_lookbacks(expr, "stock_ma"));
|
||||
stock_days.extend(rolling_mean_lookbacks(expr, "close"));
|
||||
}
|
||||
let stock_days = sorted_unique_positive(stock_days);
|
||||
if !stock_short_explicit && let Some(short) = stock_days.first().copied() {
|
||||
cfg.stock_short_ma_days = short;
|
||||
}
|
||||
if !stock_mid_explicit {
|
||||
if let Some(mid) = stock_days.get(1).copied() {
|
||||
cfg.stock_mid_ma_days = mid;
|
||||
}
|
||||
}
|
||||
if !stock_long_explicit && let Some(long) = stock_days.last().copied() {
|
||||
cfg.stock_long_ma_days = long;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn platform_expr_config_from_spec(
|
||||
strategy_id: &str,
|
||||
signal_symbol: &str,
|
||||
@@ -341,6 +620,11 @@ pub fn platform_expr_config_from_spec(
|
||||
let Some(spec) = strategy_spec else {
|
||||
return cfg;
|
||||
};
|
||||
let mut benchmark_short_explicit = false;
|
||||
let mut benchmark_long_explicit = false;
|
||||
let mut stock_short_explicit = false;
|
||||
let mut stock_mid_explicit = false;
|
||||
let mut stock_long_explicit = false;
|
||||
|
||||
if let Some(spec_strategy_id) = spec
|
||||
.strategy_id
|
||||
@@ -370,23 +654,35 @@ pub fn platform_expr_config_from_spec(
|
||||
{
|
||||
cfg.rebalance_schedule = Some(schedule);
|
||||
}
|
||||
if let Some(time) = engine
|
||||
.rebalance_schedule
|
||||
.as_ref()
|
||||
.and_then(parse_schedule_execution_time)
|
||||
{
|
||||
cfg.intraday_execution_time = Some(time);
|
||||
}
|
||||
if let Some(stock_ma_filter) = engine.stock_ma_filter.as_ref() {
|
||||
if let Some(days) = stock_ma_filter.short_days.filter(|value| *value > 0) {
|
||||
cfg.stock_short_ma_days = days;
|
||||
stock_short_explicit = true;
|
||||
}
|
||||
if let Some(days) = stock_ma_filter.mid_days.filter(|value| *value > 0) {
|
||||
cfg.stock_mid_ma_days = days;
|
||||
stock_mid_explicit = true;
|
||||
}
|
||||
if let Some(days) = stock_ma_filter.long_days.filter(|value| *value > 0) {
|
||||
cfg.stock_long_ma_days = days;
|
||||
stock_long_explicit = true;
|
||||
}
|
||||
}
|
||||
if let Some(index_throttle) = engine.index_throttle.as_ref() {
|
||||
if let Some(days) = index_throttle.short_days.filter(|value| *value > 0) {
|
||||
cfg.benchmark_short_ma_days = days;
|
||||
benchmark_short_explicit = true;
|
||||
}
|
||||
if let Some(days) = index_throttle.long_days.filter(|value| *value > 0) {
|
||||
cfg.benchmark_long_ma_days = days;
|
||||
benchmark_long_explicit = true;
|
||||
}
|
||||
}
|
||||
if !engine.skip_windows.is_empty() {
|
||||
@@ -417,6 +713,23 @@ pub fn platform_expr_config_from_spec(
|
||||
{
|
||||
cfg.benchmark_symbol = spec_benchmark_symbol.clone();
|
||||
}
|
||||
apply_cost_overrides(
|
||||
&mut cfg,
|
||||
engine.commission_rate,
|
||||
engine.minimum_commission,
|
||||
engine.stamp_tax_rate_before_change,
|
||||
engine.stamp_tax_rate_after_change,
|
||||
);
|
||||
apply_execution_behavior_overrides(
|
||||
&mut cfg,
|
||||
engine.matching_type.as_deref(),
|
||||
engine.slippage_model.as_deref(),
|
||||
engine.slippage_value,
|
||||
engine.slippage_impact_coefficient,
|
||||
engine.slippage_volatility_coefficient,
|
||||
engine.slippage_max_value,
|
||||
engine.strict_value_budget,
|
||||
);
|
||||
}
|
||||
|
||||
if let Some(spec_signal_symbol) = spec
|
||||
@@ -499,6 +812,13 @@ pub fn platform_expr_config_from_spec(
|
||||
{
|
||||
cfg.rebalance_schedule = Some(schedule);
|
||||
}
|
||||
if let Some(time) = runtime_expr
|
||||
.schedule
|
||||
.as_ref()
|
||||
.and_then(parse_schedule_execution_time)
|
||||
{
|
||||
cfg.intraday_execution_time = Some(time);
|
||||
}
|
||||
if let Some(selection) = runtime_expr.selection.as_ref() {
|
||||
if let Some(expr) = selection
|
||||
.limit_expr
|
||||
@@ -591,6 +911,13 @@ pub fn platform_expr_config_from_spec(
|
||||
}
|
||||
}
|
||||
if let Some(trading) = runtime_expr.trading.as_ref() {
|
||||
if let Some(expr) = trading
|
||||
.refresh_rate_expr
|
||||
.as_ref()
|
||||
.filter(|value| !value.trim().is_empty())
|
||||
{
|
||||
cfg.refresh_rate_expr = expr.clone();
|
||||
}
|
||||
if let Some(enabled) = trading.rotation_enabled {
|
||||
cfg.rotation_enabled = enabled;
|
||||
}
|
||||
@@ -600,6 +927,20 @@ pub fn platform_expr_config_from_spec(
|
||||
if let Some(enabled) = trading.retry_empty_rebalance {
|
||||
cfg.retry_empty_rebalance = enabled;
|
||||
}
|
||||
if let Some(enabled) = trading.release_slot_on_exit_signal {
|
||||
cfg.release_slot_on_exit_signal = enabled;
|
||||
}
|
||||
if let Some(enabled) = trading.delayed_limit_open_exit {
|
||||
cfg.delayed_limit_open_exit_enabled = enabled;
|
||||
if enabled {
|
||||
cfg.delayed_limit_open_exit_time = trading
|
||||
.delayed_limit_open_exit_time
|
||||
.as_deref()
|
||||
.and_then(|value| parse_schedule_clock_time(Some(value)));
|
||||
} else {
|
||||
cfg.delayed_limit_open_exit_time = None;
|
||||
}
|
||||
}
|
||||
if let Some(enabled) = spec
|
||||
.engine_config
|
||||
.as_ref()
|
||||
@@ -628,6 +969,13 @@ pub fn platform_expr_config_from_spec(
|
||||
{
|
||||
cfg.explicit_action_schedule = Some(schedule);
|
||||
}
|
||||
if let Some(time) = trading
|
||||
.schedule
|
||||
.as_ref()
|
||||
.and_then(parse_schedule_execution_time)
|
||||
{
|
||||
cfg.intraday_execution_time = Some(time);
|
||||
}
|
||||
cfg.explicit_actions = trading
|
||||
.actions
|
||||
.iter()
|
||||
@@ -682,12 +1030,91 @@ pub fn platform_expr_config_from_spec(
|
||||
cfg.selection_limit_expr = cfg.max_positions.to_string();
|
||||
}
|
||||
|
||||
infer_expression_windows(
|
||||
&mut cfg,
|
||||
benchmark_short_explicit,
|
||||
benchmark_long_explicit,
|
||||
stock_short_explicit,
|
||||
stock_mid_explicit,
|
||||
stock_long_explicit,
|
||||
);
|
||||
|
||||
if !cfg.signal_symbol.trim().is_empty() {
|
||||
cfg.signal_symbol = normalize_symbol(&cfg.signal_symbol, None);
|
||||
}
|
||||
if !cfg.benchmark_symbol.trim().is_empty() {
|
||||
cfg.benchmark_symbol = normalize_symbol(&cfg.benchmark_symbol, None);
|
||||
}
|
||||
let aiquant_compat = spec
|
||||
.execution
|
||||
.as_ref()
|
||||
.and_then(|execution| execution.compatibility_profile.as_deref())
|
||||
.map(|value| value.trim().to_ascii_lowercase())
|
||||
.is_some_and(|value| value == "aiquant_rqalpha" || value == "aiquant");
|
||||
if aiquant_compat {
|
||||
cfg.aiquant_transaction_cost = true;
|
||||
let trading = spec
|
||||
.runtime_expressions
|
||||
.as_ref()
|
||||
.and_then(|runtime_expr| runtime_expr.trading.as_ref());
|
||||
if trading.and_then(|item| item.daily_top_up).is_none() {
|
||||
cfg.daily_top_up_enabled = true;
|
||||
}
|
||||
if trading
|
||||
.and_then(|item| item.retry_empty_rebalance)
|
||||
.is_none()
|
||||
{
|
||||
cfg.retry_empty_rebalance = true;
|
||||
}
|
||||
}
|
||||
let trade_times = spec_trade_times(spec);
|
||||
if let Some(main_trade_time) = trade_times.last().copied() {
|
||||
cfg.intraday_execution_time = Some(main_trade_time);
|
||||
}
|
||||
let delayed_limit_open_exit_explicit = spec
|
||||
.runtime_expressions
|
||||
.as_ref()
|
||||
.and_then(|runtime_expr| runtime_expr.trading.as_ref())
|
||||
.and_then(|trading| trading.delayed_limit_open_exit)
|
||||
.is_some();
|
||||
if aiquant_compat && !delayed_limit_open_exit_explicit && trade_times.len() > 1 {
|
||||
let delayed_time = trade_times[0];
|
||||
if trade_times
|
||||
.last()
|
||||
.copied()
|
||||
.map(|main_time| main_time != delayed_time)
|
||||
.unwrap_or(true)
|
||||
{
|
||||
cfg.delayed_limit_open_exit_enabled = true;
|
||||
cfg.delayed_limit_open_exit_time = Some(delayed_time);
|
||||
}
|
||||
}
|
||||
if let Some(execution) = spec.execution.as_ref() {
|
||||
apply_cost_overrides(
|
||||
&mut cfg,
|
||||
execution.commission_rate,
|
||||
execution.minimum_commission,
|
||||
execution.stamp_tax_rate_before_change,
|
||||
execution.stamp_tax_rate_after_change,
|
||||
);
|
||||
apply_execution_behavior_overrides(
|
||||
&mut cfg,
|
||||
execution.matching_type.as_deref(),
|
||||
execution.slippage_model.as_deref(),
|
||||
execution.slippage_value,
|
||||
execution.slippage_impact_coefficient,
|
||||
execution.slippage_volatility_coefficient,
|
||||
execution.slippage_max_value,
|
||||
execution.strict_value_budget,
|
||||
);
|
||||
}
|
||||
if cfg.aiquant_transaction_cost
|
||||
&& cfg
|
||||
.minimum_commission
|
||||
.is_some_and(|value| value.is_finite() && value <= 0.0)
|
||||
{
|
||||
cfg.minimum_commission = None;
|
||||
}
|
||||
|
||||
cfg
|
||||
}
|
||||
@@ -744,6 +1171,16 @@ fn parse_schedule_time_rule(
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_schedule_execution_time(schedule: &StrategyExpressionScheduleConfig) -> Option<NaiveTime> {
|
||||
match parse_schedule_time_rule(schedule)? {
|
||||
ScheduleTimeRule::BeforeTrading => NaiveTime::from_hms_opt(9, 0, 0),
|
||||
ScheduleTimeRule::MinuteOfDay(minutes) => {
|
||||
let seconds = minutes.checked_mul(60)?;
|
||||
NaiveTime::from_num_seconds_from_midnight_opt(seconds, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_schedule_clock_time(raw: Option<&str>) -> Option<NaiveTime> {
|
||||
let value = raw?.trim();
|
||||
if value.is_empty() {
|
||||
@@ -754,6 +1191,24 @@ fn parse_schedule_clock_time(raw: Option<&str>) -> Option<NaiveTime> {
|
||||
.or_else(|| NaiveTime::parse_from_str(value, "%H:%M").ok())
|
||||
}
|
||||
|
||||
fn parse_trade_times(raw: &[String]) -> Vec<NaiveTime> {
|
||||
raw.iter()
|
||||
.filter_map(|item| parse_schedule_clock_time(Some(item.as_str())))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn spec_trade_times(spec: &StrategyRuntimeSpec) -> Vec<NaiveTime> {
|
||||
let rebalance_times = spec
|
||||
.rebalance
|
||||
.as_ref()
|
||||
.map(|rebalance| parse_trade_times(&rebalance.trade_times))
|
||||
.unwrap_or_default();
|
||||
if !rebalance_times.is_empty() {
|
||||
return rebalance_times;
|
||||
}
|
||||
parse_trade_times(&spec.trade_times)
|
||||
}
|
||||
|
||||
fn parse_platform_trade_action(
|
||||
action: &StrategyExpressionActionConfig,
|
||||
) -> Option<PlatformTradeAction> {
|
||||
@@ -1060,6 +1515,7 @@ mod tests {
|
||||
"signalSymbol": "000852.SH",
|
||||
"benchmark": { "instrumentId": "000852.SH" },
|
||||
"universe": { "exclude": ["paused", "st", "kcb", "one_yuan"] },
|
||||
"execution": { "compatibilityProfile": "aiquant_rqalpha" },
|
||||
"runtimeExpressions": {
|
||||
"prelude": "let stocknum = 8;",
|
||||
"selection": {
|
||||
@@ -1069,6 +1525,7 @@ mod tests {
|
||||
"stockFilterExpr": "stock_ma5 > stock_ma10"
|
||||
},
|
||||
"trading": {
|
||||
"refreshRateExpr": "year >= 2024 ? 5 : 20",
|
||||
"rotationEnabled": false,
|
||||
"dailyTopUp": true,
|
||||
"retryEmptyRebalance": true,
|
||||
@@ -1090,14 +1547,261 @@ mod tests {
|
||||
assert_eq!(cfg.strategy_name, "runtime_spec_test");
|
||||
assert_eq!(cfg.signal_symbol, "000852.SH");
|
||||
assert_eq!(cfg.selection_limit_expr, "stocknum");
|
||||
assert_eq!(cfg.refresh_rate_expr, "year >= 2024 ? 5 : 20");
|
||||
assert_eq!(cfg.universe_exclude, ["paused", "st", "kcb", "one_yuan"]);
|
||||
assert!(!cfg.rotation_enabled);
|
||||
assert!(cfg.daily_top_up_enabled);
|
||||
assert!(cfg.retry_empty_rebalance);
|
||||
assert!(!cfg.calendar_rebalance_interval);
|
||||
assert!(cfg.aiquant_transaction_cost);
|
||||
assert_eq!(cfg.explicit_actions.len(), 1);
|
||||
assert_eq!(
|
||||
cfg.explicit_action_stage,
|
||||
PlatformExplicitActionStage::OpenAuction
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_execution_cost_overrides_into_platform_config() {
|
||||
let spec = serde_json::json!({
|
||||
"execution": {
|
||||
"compatibilityProfile": "aiquant_rqalpha",
|
||||
"commissionRate": 0.0003,
|
||||
"minimumCommission": 5.0,
|
||||
"stampTaxRateBeforeChange": 0.0005,
|
||||
"stampTaxRateAfterChange": 0.0005
|
||||
},
|
||||
"engineConfig": {
|
||||
"commissionRate": 0.0008
|
||||
}
|
||||
});
|
||||
|
||||
let cfg = platform_expr_config_from_value("", "", &spec).expect("config");
|
||||
|
||||
assert!(cfg.aiquant_transaction_cost);
|
||||
assert_eq!(cfg.commission_rate, Some(0.0003));
|
||||
assert_eq!(cfg.minimum_commission, Some(5.0));
|
||||
assert_eq!(cfg.stamp_tax_rate_before_change, Some(0.0005));
|
||||
assert_eq!(cfg.stamp_tax_rate_after_change, Some(0.0005));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_execution_slippage_overrides_into_platform_config() {
|
||||
let spec = serde_json::json!({
|
||||
"execution": {
|
||||
"compatibilityProfile": "aiquant_rqalpha",
|
||||
"matchingType": "next_tick_last",
|
||||
"slippageModel": "price_ratio",
|
||||
"slippageValue": 0.001,
|
||||
"strictValueBudget": true
|
||||
},
|
||||
"engineConfig": {
|
||||
"matchingType": "current_bar_close",
|
||||
"slippageModel": "none",
|
||||
"slippageValue": 0.0,
|
||||
"strictValueBudget": false
|
||||
}
|
||||
});
|
||||
|
||||
let cfg = platform_expr_config_from_value("", "", &spec).expect("config");
|
||||
|
||||
assert_eq!(cfg.matching_type, MatchingType::NextTickLast);
|
||||
assert_eq!(cfg.slippage_model, SlippageModel::PriceRatio(0.001));
|
||||
assert!(cfg.strict_value_budget);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_dynamic_slippage_into_platform_config() {
|
||||
let spec = serde_json::json!({
|
||||
"execution": {
|
||||
"slippageModel": "dynamic",
|
||||
"slippageImpactCoefficient": 0.6,
|
||||
"slippageVolatilityCoefficient": 0.2,
|
||||
"slippageMaxValue": 0.015
|
||||
}
|
||||
});
|
||||
|
||||
let cfg = platform_expr_config_from_value("", "", &spec).expect("config");
|
||||
|
||||
assert_eq!(
|
||||
cfg.slippage_model,
|
||||
SlippageModel::Dynamic(DynamicSlippageConfig::new(0.6, 0.2, 0.015))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn aiquant_profile_defaults_to_daily_top_up_and_empty_retry() {
|
||||
let spec = serde_json::json!({
|
||||
"execution": {
|
||||
"compatibilityProfile": "aiquant_rqalpha"
|
||||
}
|
||||
});
|
||||
|
||||
let cfg = platform_expr_config_from_value("", "", &spec).expect("config");
|
||||
|
||||
assert!(cfg.aiquant_transaction_cost);
|
||||
assert!(cfg.daily_top_up_enabled);
|
||||
assert!(cfg.retry_empty_rebalance);
|
||||
|
||||
let explicit_off = serde_json::json!({
|
||||
"execution": {
|
||||
"compatibilityProfile": "aiquant_rqalpha"
|
||||
},
|
||||
"runtimeExpressions": {
|
||||
"trading": {
|
||||
"dailyTopUp": false,
|
||||
"retryEmptyRebalance": false
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let cfg = platform_expr_config_from_value("", "", &explicit_off).expect("config");
|
||||
|
||||
assert!(!cfg.daily_top_up_enabled);
|
||||
assert!(!cfg.retry_empty_rebalance);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runtime_expressions_infer_ma_windows_from_literal_strategy_logic() {
|
||||
let spec = serde_json::json!({
|
||||
"execution": {
|
||||
"compatibilityProfile": "aiquant_rqalpha"
|
||||
},
|
||||
"runtimeExpressions": {
|
||||
"selection": {
|
||||
"stockFilterExpr": "rolling_mean(\"close\", 5) > rolling_mean(\"close\", 10) && rolling_mean(\"close\", 10) > rolling_mean(\"close\", 30)"
|
||||
},
|
||||
"risk": {
|
||||
"exposureExpr": "benchmark_ma5 > benchmark_ma20 ? 1.0 : weak_market_trade_rate"
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let cfg = platform_expr_config_from_value("", "", &spec).expect("config");
|
||||
|
||||
assert_eq!(cfg.benchmark_short_ma_days, 5);
|
||||
assert_eq!(cfg.benchmark_long_ma_days, 20);
|
||||
assert_eq!(cfg.stock_short_ma_days, 5);
|
||||
assert_eq!(cfg.stock_mid_ma_days, 10);
|
||||
assert_eq!(cfg.stock_long_ma_days, 30);
|
||||
|
||||
let explicit = serde_json::json!({
|
||||
"engineConfig": {
|
||||
"stockMaFilter": {
|
||||
"shortDays": 4,
|
||||
"midDays": 9,
|
||||
"longDays": 21
|
||||
},
|
||||
"indexThrottle": {
|
||||
"shortDays": 3,
|
||||
"longDays": 13
|
||||
}
|
||||
},
|
||||
"runtimeExpressions": {
|
||||
"selection": {
|
||||
"stockFilterExpr": "rolling_mean(\"close\", 5) > rolling_mean(\"close\", 10) && rolling_mean(\"close\", 10) > rolling_mean(\"close\", 30)"
|
||||
},
|
||||
"risk": {
|
||||
"exposureExpr": "benchmark_ma5 > benchmark_ma20 ? 1.0 : 0.5"
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let cfg = platform_expr_config_from_value("", "", &explicit).expect("config");
|
||||
|
||||
assert_eq!(cfg.benchmark_short_ma_days, 3);
|
||||
assert_eq!(cfg.benchmark_long_ma_days, 13);
|
||||
assert_eq!(cfg.stock_short_ma_days, 4);
|
||||
assert_eq!(cfg.stock_mid_ma_days, 9);
|
||||
assert_eq!(cfg.stock_long_ma_days, 21);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_daily_schedule_time_for_aiquant_execution_quotes() {
|
||||
let spec = serde_json::json!({
|
||||
"execution": { "compatibilityProfile": "aiquant_rqalpha" },
|
||||
"runtimeExpressions": {
|
||||
"schedule": { "frequency": "daily", "time": "09:33" }
|
||||
}
|
||||
});
|
||||
|
||||
let cfg = platform_expr_config_from_value("", "", &spec).expect("config");
|
||||
|
||||
assert_eq!(cfg.rebalance_schedule, None);
|
||||
assert_eq!(
|
||||
cfg.intraday_execution_time,
|
||||
Some(NaiveTime::from_hms_opt(9, 33, 0).unwrap())
|
||||
);
|
||||
assert!(!cfg.calendar_rebalance_interval);
|
||||
assert!(cfg.aiquant_transaction_cost);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_aiquant_rebalance_trade_times_for_delayed_limit_exit() {
|
||||
let spec = serde_json::json!({
|
||||
"execution": { "compatibilityProfile": "aiquant_rqalpha" },
|
||||
"rebalance": { "tradeTimes": ["10:31", "10:40"] },
|
||||
"runtimeExpressions": {
|
||||
"schedule": { "frequency": "daily", "time": "10:40" }
|
||||
}
|
||||
});
|
||||
|
||||
let cfg = platform_expr_config_from_value("", "", &spec).expect("config");
|
||||
|
||||
assert_eq!(
|
||||
cfg.intraday_execution_time,
|
||||
Some(NaiveTime::from_hms_opt(10, 40, 0).unwrap())
|
||||
);
|
||||
assert!(cfg.delayed_limit_open_exit_enabled);
|
||||
assert_eq!(
|
||||
cfg.delayed_limit_open_exit_time,
|
||||
Some(NaiveTime::from_hms_opt(10, 31, 0).unwrap())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_explicit_delayed_limit_open_exit() {
|
||||
let spec = serde_json::json!({
|
||||
"execution": { "compatibilityProfile": "aiquant_rqalpha" },
|
||||
"runtimeExpressions": {
|
||||
"schedule": { "frequency": "daily", "time": "10:40" },
|
||||
"trading": {
|
||||
"delayedLimitOpenExit": true,
|
||||
"delayedLimitOpenExitTime": "10:31"
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let cfg = platform_expr_config_from_value("", "", &spec).expect("config");
|
||||
|
||||
assert_eq!(
|
||||
cfg.intraday_execution_time,
|
||||
Some(NaiveTime::from_hms_opt(10, 40, 0).unwrap())
|
||||
);
|
||||
assert!(cfg.delayed_limit_open_exit_enabled);
|
||||
assert_eq!(
|
||||
cfg.delayed_limit_open_exit_time,
|
||||
Some(NaiveTime::from_hms_opt(10, 31, 0).unwrap())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_delayed_limit_open_exit_false_overrides_aiquant_trade_times() {
|
||||
let spec = serde_json::json!({
|
||||
"execution": { "compatibilityProfile": "aiquant_rqalpha" },
|
||||
"rebalance": { "tradeTimes": ["10:31", "10:40"] },
|
||||
"runtimeExpressions": {
|
||||
"schedule": { "frequency": "daily", "time": "10:40" },
|
||||
"trading": {
|
||||
"delayedLimitOpenExit": false,
|
||||
"delayedLimitOpenExitTime": "10:31"
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let cfg = platform_expr_config_from_value("", "", &spec).expect("config");
|
||||
|
||||
assert!(!cfg.delayed_limit_open_exit_enabled);
|
||||
assert_eq!(cfg.delayed_limit_open_exit_time, None);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use crate::data::{DataSet, DataSetError, PriceField};
|
||||
pub struct PositionLot {
|
||||
pub acquired_date: NaiveDate,
|
||||
pub quantity: u32,
|
||||
pub entry_price: f64,
|
||||
pub price: f64,
|
||||
}
|
||||
|
||||
@@ -65,6 +66,16 @@ impl Position {
|
||||
}
|
||||
|
||||
pub fn buy(&mut self, date: NaiveDate, quantity: u32, price: f64) {
|
||||
self.buy_with_mark_price(date, quantity, price, price);
|
||||
}
|
||||
|
||||
pub fn buy_with_mark_price(
|
||||
&mut self,
|
||||
date: NaiveDate,
|
||||
quantity: u32,
|
||||
execution_price: f64,
|
||||
mark_price: f64,
|
||||
) {
|
||||
if quantity == 0 {
|
||||
return;
|
||||
}
|
||||
@@ -72,18 +83,28 @@ impl Position {
|
||||
self.lots.push(PositionLot {
|
||||
acquired_date: date,
|
||||
quantity,
|
||||
price,
|
||||
entry_price: execution_price,
|
||||
price: execution_price,
|
||||
});
|
||||
self.quantity += quantity;
|
||||
self.last_price = price;
|
||||
self.last_price = normalized_mark_price(mark_price, execution_price);
|
||||
self.day_trade_quantity_delta += quantity as i32;
|
||||
self.day_buy_quantity += quantity;
|
||||
self.day_buy_value += price * quantity as f64;
|
||||
self.day_buy_value += execution_price * quantity as f64;
|
||||
self.recalculate_average_cost();
|
||||
self.refresh_day_pnl();
|
||||
}
|
||||
|
||||
pub fn sell(&mut self, quantity: u32, price: f64) -> Result<f64, String> {
|
||||
self.sell_with_mark_price(quantity, price, price)
|
||||
}
|
||||
|
||||
pub fn sell_with_mark_price(
|
||||
&mut self,
|
||||
quantity: u32,
|
||||
execution_price: f64,
|
||||
mark_price: f64,
|
||||
) -> Result<f64, String> {
|
||||
if quantity > self.quantity {
|
||||
return Err(format!(
|
||||
"sell quantity {} exceeds current quantity {} for {}",
|
||||
@@ -100,7 +121,7 @@ impl Position {
|
||||
};
|
||||
|
||||
let lot_sell = remaining.min(first_lot.quantity);
|
||||
realized += (price - first_lot.price) * lot_sell as f64;
|
||||
realized += (execution_price - first_lot.price) * lot_sell as f64;
|
||||
first_lot.quantity -= lot_sell;
|
||||
remaining -= lot_sell;
|
||||
|
||||
@@ -110,11 +131,11 @@ impl Position {
|
||||
}
|
||||
|
||||
self.quantity -= quantity;
|
||||
self.last_price = price;
|
||||
self.last_price = normalized_mark_price(mark_price, execution_price);
|
||||
self.realized_pnl += realized;
|
||||
self.day_trade_quantity_delta -= quantity as i32;
|
||||
self.day_sell_quantity += quantity;
|
||||
self.day_sell_value += price * quantity as f64;
|
||||
self.day_sell_value += execution_price * quantity as f64;
|
||||
self.recalculate_average_cost();
|
||||
self.refresh_day_pnl();
|
||||
Ok(realized)
|
||||
@@ -230,13 +251,28 @@ impl Position {
|
||||
}
|
||||
|
||||
pub fn holding_return(&self, price: f64) -> Option<f64> {
|
||||
if self.quantity == 0 || self.average_cost <= 0.0 {
|
||||
let Some(avg_price) = self.average_entry_price() else {
|
||||
return None;
|
||||
};
|
||||
if avg_price <= 0.0 {
|
||||
None
|
||||
} else {
|
||||
Some((price / self.average_cost) - 1.0)
|
||||
Some((price / avg_price) - 1.0)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn average_entry_price(&self) -> Option<f64> {
|
||||
if self.quantity == 0 {
|
||||
return None;
|
||||
}
|
||||
let total = self
|
||||
.lots
|
||||
.iter()
|
||||
.map(|lot| lot.entry_price * lot.quantity as f64)
|
||||
.sum::<f64>();
|
||||
Some(total / self.quantity as f64)
|
||||
}
|
||||
|
||||
fn recalculate_average_cost(&mut self) {
|
||||
if self.quantity == 0 {
|
||||
self.average_cost = 0.0;
|
||||
@@ -253,14 +289,31 @@ impl Position {
|
||||
}
|
||||
|
||||
pub fn apply_cash_dividend(&mut self, dividend_per_share: f64) -> f64 {
|
||||
self.apply_cash_dividend_internal(dividend_per_share, true)
|
||||
}
|
||||
|
||||
pub fn apply_cash_dividend_preserve_cost_basis(&mut self, dividend_per_share: f64) -> f64 {
|
||||
self.apply_cash_dividend_internal(dividend_per_share, false)
|
||||
}
|
||||
|
||||
fn apply_cash_dividend_internal(
|
||||
&mut self,
|
||||
dividend_per_share: f64,
|
||||
adjust_cost_basis: bool,
|
||||
) -> f64 {
|
||||
if self.quantity == 0 || !dividend_per_share.is_finite() || dividend_per_share == 0.0 {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
for lot in &mut self.lots {
|
||||
lot.price -= dividend_per_share;
|
||||
lot.entry_price -= dividend_per_share;
|
||||
if adjust_cost_basis {
|
||||
lot.price -= dividend_per_share;
|
||||
}
|
||||
}
|
||||
if adjust_cost_basis {
|
||||
self.average_cost -= dividend_per_share;
|
||||
}
|
||||
self.average_cost -= dividend_per_share;
|
||||
self.last_price -= dividend_per_share;
|
||||
let cash_delta = self.quantity as f64 * dividend_per_share;
|
||||
self.day_dividend_cash += cash_delta;
|
||||
@@ -280,6 +333,7 @@ impl Position {
|
||||
.map(|lot| PositionLot {
|
||||
acquired_date: lot.acquired_date,
|
||||
quantity: round_half_up_u32(lot.quantity as f64 * ratio),
|
||||
entry_price: lot.entry_price / ratio,
|
||||
price: lot.price / ratio,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
@@ -321,6 +375,14 @@ impl Position {
|
||||
}
|
||||
}
|
||||
|
||||
fn normalized_mark_price(mark_price: f64, fallback: f64) -> f64 {
|
||||
if mark_price.is_finite() && mark_price > 0.0 {
|
||||
mark_price
|
||||
} else {
|
||||
fallback
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct PortfolioState {
|
||||
initial_cash: f64,
|
||||
@@ -759,6 +821,7 @@ impl PortfolioState {
|
||||
.map(|lot| PositionLot {
|
||||
acquired_date: lot.acquired_date,
|
||||
quantity: round_half_up_u32(lot.quantity as f64 * ratio),
|
||||
entry_price: lot.entry_price / ratio,
|
||||
price: lot.price / ratio,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
@@ -855,6 +918,35 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strategy_entry_price_excludes_buy_commission_cost_basis() {
|
||||
let date = NaiveDate::from_ymd_opt(2025, 1, 2).unwrap();
|
||||
let mut position = Position::new("600561.SH");
|
||||
position.buy(date, 22_200, 5.66);
|
||||
position.record_buy_trade_cost(22_200, 100.0);
|
||||
|
||||
assert!(position.average_cost > 5.66);
|
||||
assert!((position.average_entry_price().unwrap() - 5.66).abs() < 1e-12);
|
||||
assert!((position.holding_return(6.06).unwrap() - (6.06 / 5.66 - 1.0)).abs() < 1e-12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cash_dividend_can_preserve_avg_cost_for_aiquant_compatibility() {
|
||||
let date = NaiveDate::from_ymd_opt(2025, 1, 2).unwrap();
|
||||
let mut position = Position::new("603102.SH");
|
||||
position.buy(date, 1000, 46.45);
|
||||
position.record_buy_trade_cost(1000, 37.16);
|
||||
|
||||
let cost_before = position.average_cost;
|
||||
let entry_before = position.average_entry_price().unwrap();
|
||||
let cash = position.apply_cash_dividend_preserve_cost_basis(0.6);
|
||||
|
||||
assert!((cash - 600.0).abs() < 1e-12);
|
||||
assert!((position.average_cost - cost_before).abs() < 1e-12);
|
||||
assert!((position.average_entry_price().unwrap() - (entry_before - 0.6)).abs() < 1e-12);
|
||||
assert!((position.last_price - 45.85).abs() < 1e-12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn portfolio_tracks_dividend_receivable_and_day_pnl() {
|
||||
let prev_date = NaiveDate::from_ymd_opt(2025, 1, 2).unwrap();
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
use chrono::NaiveDate;
|
||||
|
||||
use crate::data::{CandidateEligibility, DailyMarketSnapshot, PriceField};
|
||||
use crate::instrument::Instrument;
|
||||
use crate::portfolio::Position;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct ChinaAShareRiskControl;
|
||||
|
||||
impl ChinaAShareRiskControl {
|
||||
pub fn instrument_rejection_reason(
|
||||
instrument: Option<&Instrument>,
|
||||
date: NaiveDate,
|
||||
) -> Option<&'static str> {
|
||||
let instrument = instrument?;
|
||||
if instrument
|
||||
.listed_at
|
||||
.is_some_and(|listed_at| listed_at > date)
|
||||
{
|
||||
return Some("not_listed");
|
||||
}
|
||||
if instrument
|
||||
.delisted_at
|
||||
.is_some_and(|delisted_at| delisted_at <= date)
|
||||
{
|
||||
return Some("inactive_or_delisted");
|
||||
}
|
||||
let status = instrument.status.trim().to_ascii_lowercase();
|
||||
let terminal_status = matches!(
|
||||
status.as_str(),
|
||||
"inactive" | "delisted" | "terminated" | "expired"
|
||||
) || status.contains("delist");
|
||||
if terminal_status && instrument.delisted_at.is_none() {
|
||||
return Some("inactive_or_delisted");
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn selection_rejection_reason(
|
||||
date: NaiveDate,
|
||||
candidate: &CandidateEligibility,
|
||||
market: &DailyMarketSnapshot,
|
||||
instrument: Option<&Instrument>,
|
||||
) -> Option<&'static str> {
|
||||
if let Some(reason) = Self::baseline_rejection_reason(date, candidate, market, instrument) {
|
||||
return Some(reason);
|
||||
}
|
||||
if !candidate.allow_buy || !candidate.allow_sell {
|
||||
return Some("trade_disabled");
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn baseline_rejection_reason(
|
||||
date: NaiveDate,
|
||||
candidate: &CandidateEligibility,
|
||||
market: &DailyMarketSnapshot,
|
||||
instrument: Option<&Instrument>,
|
||||
) -> Option<&'static str> {
|
||||
if let Some(reason) = Self::instrument_rejection_reason(instrument, date) {
|
||||
return Some(reason);
|
||||
}
|
||||
if market.paused || candidate.is_paused {
|
||||
return Some("paused");
|
||||
}
|
||||
if candidate.is_st {
|
||||
return Some("st");
|
||||
}
|
||||
if candidate.is_new_listing {
|
||||
return Some("new_listing");
|
||||
}
|
||||
if candidate.is_kcb {
|
||||
return Some("kcb");
|
||||
}
|
||||
if candidate.is_one_yuan || market.day_open <= 1.0 {
|
||||
return Some("one_yuan");
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn buy_rejection_reason(
|
||||
date: NaiveDate,
|
||||
candidate: &CandidateEligibility,
|
||||
market: &DailyMarketSnapshot,
|
||||
instrument: Option<&Instrument>,
|
||||
check_price: f64,
|
||||
) -> Option<&'static str> {
|
||||
if let Some(reason) = Self::baseline_rejection_reason(date, candidate, market, instrument) {
|
||||
return Some(reason);
|
||||
}
|
||||
if !candidate.allow_buy {
|
||||
return Some("buy_disabled");
|
||||
}
|
||||
if market.is_at_upper_limit_price(check_price) {
|
||||
return Some("open at or above upper limit");
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn sell_rejection_reason(
|
||||
date: NaiveDate,
|
||||
candidate: &CandidateEligibility,
|
||||
market: &DailyMarketSnapshot,
|
||||
instrument: Option<&Instrument>,
|
||||
position: Option<&Position>,
|
||||
check_price: f64,
|
||||
) -> Option<&'static str> {
|
||||
if let Some(reason) = Self::instrument_rejection_reason(instrument, date) {
|
||||
return Some(reason);
|
||||
}
|
||||
if market.paused || candidate.is_paused {
|
||||
return Some("paused");
|
||||
}
|
||||
if !candidate.allow_sell {
|
||||
return Some("sell_disabled");
|
||||
}
|
||||
if market.is_at_lower_limit_price(check_price) {
|
||||
return Some("open at or below lower limit");
|
||||
}
|
||||
if position.is_some_and(|position| position.sellable_qty(date) == 0) {
|
||||
return Some("t+1 sellable quantity is zero");
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn buy_check_price(market: &DailyMarketSnapshot, price_field: PriceField) -> f64 {
|
||||
market.buy_price(price_field)
|
||||
}
|
||||
|
||||
pub fn sell_check_price(market: &DailyMarketSnapshot, price_field: PriceField) -> f64 {
|
||||
match price_field {
|
||||
PriceField::Last => market.price(PriceField::Last),
|
||||
_ => market.sell_price(price_field),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ use chrono::NaiveDate;
|
||||
|
||||
use crate::data::{CandidateEligibility, DailyMarketSnapshot, PriceField};
|
||||
use crate::portfolio::Position;
|
||||
use crate::risk_control::ChinaAShareRiskControl;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RuleCheck {
|
||||
@@ -47,20 +48,6 @@ pub trait EquityRuleHooks {
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct ChinaEquityRuleHooks;
|
||||
|
||||
impl ChinaEquityRuleHooks {
|
||||
fn at_upper_limit(snapshot: &DailyMarketSnapshot, price_field: PriceField) -> bool {
|
||||
snapshot.is_at_upper_limit_price(snapshot.buy_price(price_field))
|
||||
}
|
||||
|
||||
fn at_lower_limit(snapshot: &DailyMarketSnapshot, price_field: PriceField) -> bool {
|
||||
let check_price = match price_field {
|
||||
PriceField::Last => snapshot.price(PriceField::Last),
|
||||
_ => snapshot.sell_price(price_field),
|
||||
};
|
||||
snapshot.is_at_lower_limit_price(check_price)
|
||||
}
|
||||
}
|
||||
|
||||
impl EquityRuleHooks for ChinaEquityRuleHooks {
|
||||
fn can_buy(
|
||||
&self,
|
||||
@@ -69,14 +56,14 @@ impl EquityRuleHooks for ChinaEquityRuleHooks {
|
||||
candidate: &CandidateEligibility,
|
||||
price_field: PriceField,
|
||||
) -> RuleCheck {
|
||||
if snapshot.paused || candidate.is_paused {
|
||||
return RuleCheck::reject("paused");
|
||||
}
|
||||
if !candidate.allow_buy {
|
||||
return RuleCheck::reject("buy disabled by eligibility flags");
|
||||
}
|
||||
if Self::at_upper_limit(snapshot, price_field) {
|
||||
return RuleCheck::reject("open at or above upper limit");
|
||||
if let Some(reason) = ChinaAShareRiskControl::buy_rejection_reason(
|
||||
_execution_date,
|
||||
candidate,
|
||||
snapshot,
|
||||
None,
|
||||
ChinaAShareRiskControl::buy_check_price(snapshot, price_field),
|
||||
) {
|
||||
return RuleCheck::reject(reason);
|
||||
}
|
||||
|
||||
RuleCheck::allow()
|
||||
@@ -90,17 +77,15 @@ impl EquityRuleHooks for ChinaEquityRuleHooks {
|
||||
position: &Position,
|
||||
price_field: PriceField,
|
||||
) -> RuleCheck {
|
||||
if snapshot.paused || candidate.is_paused {
|
||||
return RuleCheck::reject("paused");
|
||||
}
|
||||
if !candidate.allow_sell {
|
||||
return RuleCheck::reject("sell disabled by eligibility flags");
|
||||
}
|
||||
if Self::at_lower_limit(snapshot, price_field) {
|
||||
return RuleCheck::reject("open at or below lower limit");
|
||||
}
|
||||
if position.sellable_qty(execution_date) == 0 {
|
||||
return RuleCheck::reject("t+1 sellable quantity is zero");
|
||||
if let Some(reason) = ChinaAShareRiskControl::sell_rejection_reason(
|
||||
execution_date,
|
||||
candidate,
|
||||
snapshot,
|
||||
None,
|
||||
Some(position),
|
||||
ChinaAShareRiskControl::sell_check_price(snapshot, price_field),
|
||||
) {
|
||||
return RuleCheck::reject(reason);
|
||||
}
|
||||
|
||||
RuleCheck::allow()
|
||||
|
||||
@@ -17,6 +17,7 @@ use crate::events::{FillEvent, OrderEvent, OrderSide, OrderStatus, ProcessEvent}
|
||||
use crate::futures::{FuturesAccountState, FuturesOrderIntent};
|
||||
use crate::instrument::Instrument;
|
||||
use crate::portfolio::PortfolioState;
|
||||
use crate::risk_control::ChinaAShareRiskControl;
|
||||
use crate::scheduler::ScheduleRule;
|
||||
use crate::universe::{DynamicMarketCapBandSelector, SelectionContext, UniverseSelector};
|
||||
|
||||
@@ -39,6 +40,9 @@ pub trait Strategy {
|
||||
fn schedule_rules(&self) -> Vec<ScheduleRule> {
|
||||
Vec::new()
|
||||
}
|
||||
fn decision_quote_times(&self) -> Vec<NaiveTime> {
|
||||
Vec::new()
|
||||
}
|
||||
fn on_scheduled(
|
||||
&mut self,
|
||||
_ctx: &StrategyContext<'_>,
|
||||
@@ -1535,6 +1539,8 @@ pub struct OmniMicroCapConfig {
|
||||
pub stock_short_ma_days: usize,
|
||||
pub stock_mid_ma_days: usize,
|
||||
pub stock_long_ma_days: usize,
|
||||
pub stock_volume_short_ma_days: usize,
|
||||
pub stock_volume_long_ma_days: usize,
|
||||
pub rsi_rate: f64,
|
||||
pub trade_rate: f64,
|
||||
pub stop_loss_ratio: f64,
|
||||
@@ -1561,6 +1567,8 @@ impl OmniMicroCapConfig {
|
||||
stock_short_ma_days: 5,
|
||||
stock_mid_ma_days: 10,
|
||||
stock_long_ma_days: 20,
|
||||
stock_volume_short_ma_days: 5,
|
||||
stock_volume_long_ma_days: 60,
|
||||
rsi_rate: 1.0001,
|
||||
trade_rate: 0.5,
|
||||
stop_loss_ratio: 0.93,
|
||||
@@ -1589,6 +1597,8 @@ impl OmniMicroCapConfig {
|
||||
stock_short_ma_days: 5,
|
||||
stock_mid_ma_days: 10,
|
||||
stock_long_ma_days: 30,
|
||||
stock_volume_short_ma_days: 5,
|
||||
stock_volume_long_ma_days: 60,
|
||||
rsi_rate: 1.0001,
|
||||
trade_rate: 0.5,
|
||||
stop_loss_ratio: 0.92,
|
||||
@@ -2267,62 +2277,33 @@ impl OmniMicroCapStrategy {
|
||||
return false;
|
||||
};
|
||||
|
||||
// MA filter: ma_short > ma_mid * rsi_rate && ma_mid * rsi_rate > ma_long
|
||||
let ma_pass =
|
||||
ma_short > ma_mid * self.config.rsi_rate && ma_mid * self.config.rsi_rate > ma_long;
|
||||
|
||||
// Debug logging for ALL stocks on first decision date
|
||||
static DEBUG_DATE: std::sync::Mutex<Option<NaiveDate>> = std::sync::Mutex::new(None);
|
||||
let mut debug_date = DEBUG_DATE.lock().unwrap();
|
||||
let should_debug = if let Some(d) = *debug_date {
|
||||
d == date
|
||||
} else {
|
||||
*debug_date = Some(date);
|
||||
true
|
||||
};
|
||||
|
||||
if should_debug {
|
||||
eprintln!(
|
||||
"[MA_FILTER] {} cap={:.2} ma5={:.4} ma10={:.4} ma30={:.4} ma10*rsi={:.4} pass={} ({}>{:.4}? {} && {:.4}>{}? {})",
|
||||
symbol,
|
||||
ctx.data.market_decision_close(date, symbol).unwrap_or(0.0),
|
||||
ma_short,
|
||||
ma_mid,
|
||||
ma_long,
|
||||
ma_mid * self.config.rsi_rate,
|
||||
ma_pass,
|
||||
ma_short,
|
||||
ma_mid * self.config.rsi_rate,
|
||||
ma_short > ma_mid * self.config.rsi_rate,
|
||||
ma_mid * self.config.rsi_rate,
|
||||
ma_long,
|
||||
ma_mid * self.config.rsi_rate > ma_long
|
||||
);
|
||||
}
|
||||
|
||||
if !ma_pass {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Volume filter: V5 < V60 (applied for omni_microcap strategies)
|
||||
if self.config.strategy_name.contains("aiquant")
|
||||
|| self.config.strategy_name.contains("AiQuant")
|
||||
|| self.config.strategy_name.contains("omni")
|
||||
{
|
||||
let Some(volume_ma5) = ctx
|
||||
.data
|
||||
.market_decision_volume_moving_average(date, symbol, 5)
|
||||
else {
|
||||
let Some(volume_ma5) = ctx.data.market_decision_volume_moving_average(
|
||||
date,
|
||||
symbol,
|
||||
self.config.stock_volume_short_ma_days,
|
||||
) else {
|
||||
return false;
|
||||
};
|
||||
let Some(volume_ma60) = ctx
|
||||
.data
|
||||
.market_decision_volume_moving_average(date, symbol, 60)
|
||||
else {
|
||||
let Some(volume_ma_long) = ctx.data.market_decision_volume_moving_average(
|
||||
date,
|
||||
symbol,
|
||||
self.config.stock_volume_long_ma_days,
|
||||
) else {
|
||||
return false;
|
||||
};
|
||||
|
||||
if volume_ma5 >= volume_ma60 {
|
||||
if volume_ma5 >= volume_ma_long {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -2330,18 +2311,6 @@ impl OmniMicroCapStrategy {
|
||||
true
|
||||
}
|
||||
|
||||
fn special_name(&self, ctx: &StrategyContext<'_>, symbol: &str) -> bool {
|
||||
let instrument_name = ctx
|
||||
.data
|
||||
.instruments()
|
||||
.get(symbol)
|
||||
.map(|instrument| instrument.name.as_str())
|
||||
.unwrap_or("");
|
||||
instrument_name.contains("ST")
|
||||
|| instrument_name.contains('*')
|
||||
|| instrument_name.contains('退')
|
||||
}
|
||||
|
||||
fn can_sell_position(&self, ctx: &StrategyContext<'_>, date: NaiveDate, symbol: &str) -> bool {
|
||||
let Some(position) = ctx.portfolio.position(symbol) else {
|
||||
return false;
|
||||
@@ -2355,11 +2324,15 @@ impl OmniMicroCapStrategy {
|
||||
let Ok(candidate) = ctx.data.require_candidate(date, symbol) else {
|
||||
return false;
|
||||
};
|
||||
let lower_limit_check_price = market.price(PriceField::Last);
|
||||
!(market.paused
|
||||
|| candidate.is_paused
|
||||
|| !candidate.allow_sell
|
||||
|| market.is_at_lower_limit_price(lower_limit_check_price))
|
||||
ChinaAShareRiskControl::sell_rejection_reason(
|
||||
date,
|
||||
candidate,
|
||||
market,
|
||||
ctx.data.instrument(symbol),
|
||||
Some(position),
|
||||
ChinaAShareRiskControl::sell_check_price(market, PriceField::Last),
|
||||
)
|
||||
.is_none()
|
||||
}
|
||||
|
||||
fn buy_rejection_reason(
|
||||
@@ -2371,30 +2344,14 @@ impl OmniMicroCapStrategy {
|
||||
let market = ctx.data.require_market(date, symbol)?;
|
||||
let candidate = ctx.data.require_candidate(date, symbol)?;
|
||||
|
||||
if market.paused || candidate.is_paused {
|
||||
return Ok(Some("paused".to_string()));
|
||||
}
|
||||
if candidate.is_st || self.special_name(ctx, symbol) {
|
||||
return Ok(Some("st_or_special_name".to_string()));
|
||||
}
|
||||
if candidate.is_kcb {
|
||||
return Ok(Some("kcb".to_string()));
|
||||
}
|
||||
if !candidate.allow_buy {
|
||||
return Ok(Some("buy_disabled".to_string()));
|
||||
}
|
||||
if market.is_at_upper_limit_price(market.day_open)
|
||||
|| market.is_at_upper_limit_price(market.buy_price(PriceField::Last))
|
||||
{
|
||||
return Ok(Some("upper_limit".to_string()));
|
||||
}
|
||||
if market.is_at_lower_limit_price(market.day_open)
|
||||
|| market.is_at_lower_limit_price(market.sell_price(PriceField::Last))
|
||||
{
|
||||
return Ok(Some("lower_limit".to_string()));
|
||||
}
|
||||
if market.day_open <= 1.0 {
|
||||
return Ok(Some("one_yuan".to_string()));
|
||||
if let Some(reason) = ChinaAShareRiskControl::buy_rejection_reason(
|
||||
date,
|
||||
candidate,
|
||||
market,
|
||||
ctx.data.instrument(symbol),
|
||||
ChinaAShareRiskControl::buy_check_price(market, PriceField::Last),
|
||||
) {
|
||||
return Ok(Some(reason.to_string()));
|
||||
}
|
||||
if !self.truth_selection_contains(date, symbol)
|
||||
&& !self.stock_passes_ma_filter(ctx, date, symbol)
|
||||
@@ -2539,18 +2496,6 @@ fn omni_truth_stock_list_candidates() -> Vec<PathBuf> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let suffix = PathBuf::from("data/demo/engine_truth_stock_list.csv");
|
||||
let manifest_root = Path::new(env!("CARGO_MANIFEST_DIR"));
|
||||
push_unique_truth_path(
|
||||
&mut candidates,
|
||||
manifest_root.join("../../../").join(&suffix),
|
||||
);
|
||||
if let Ok(current_dir) = env::current_dir() {
|
||||
for ancestor in current_dir.ancestors() {
|
||||
push_unique_truth_path(&mut candidates, ancestor.join(&suffix));
|
||||
}
|
||||
}
|
||||
candidates
|
||||
}
|
||||
|
||||
@@ -2719,10 +2664,6 @@ impl Strategy for OmniMicroCapStrategy {
|
||||
};
|
||||
// 使用前一交易日的指数价格计算市值区间(模拟实盘场景)
|
||||
let (band_low, band_high) = self.market_cap_band(prev_index_level);
|
||||
eprintln!(
|
||||
"[DEBUG] date={} current_index={:.2} prev_index={:.2} band=[{:.0}, {:.0}]",
|
||||
date, index_level, prev_index_level, band_low, band_high
|
||||
);
|
||||
let (stock_list, selection_notes) = self.select_symbols(ctx, date, band_low, band_high)?;
|
||||
let periodic_rebalance = ctx.decision_index % self.config.refresh_rate == 0;
|
||||
let mut projected = ctx.portfolio.clone();
|
||||
@@ -2744,10 +2685,10 @@ impl Strategy for OmniMicroCapStrategy {
|
||||
let stop_hit = current_price
|
||||
<= position.average_cost * self.config.stop_loss_ratio
|
||||
+ self.stop_loss_tolerance(market);
|
||||
let profit_hit = !market.is_at_upper_limit_price(current_price)
|
||||
&& current_price / position.average_cost > self.config.take_profit_ratio;
|
||||
let profit_hit = current_price / position.average_cost > self.config.take_profit_ratio;
|
||||
let can_sell = self.can_sell_position(ctx, date, &position.symbol);
|
||||
if stop_hit || profit_hit {
|
||||
let at_upper_limit = market.is_at_upper_limit_price(current_price);
|
||||
if stop_hit || (profit_hit && !at_upper_limit) {
|
||||
let sell_reason = if stop_hit {
|
||||
"stop_loss_exit"
|
||||
} else {
|
||||
|
||||
@@ -546,8 +546,8 @@ pub fn build_optimization_prompt(
|
||||
prompt.push_str("你是 OmniQuant 平台策略脚本优化器。必须输出完整、可运行的平台策略脚本,不要输出解释文本。\n");
|
||||
prompt.push_str("输出格式硬约束:回复第一行必须是 strategy(\"...\")、let、fn、const 或 //;回复中不得包含 Markdown、解释、思考过程、手册复述、JSON 包装或自然语言总结。\n");
|
||||
prompt.push_str("长度硬约束:策略代码目标 80 行以内,只保留必要 let/fn/strategy 块;不要复制下面的手册片段、历史策略全文或字段清单。\n");
|
||||
prompt.push_str("只修改与优化目标相关的少量参数或过滤条件,保留原策略的市场、基准、信号指数和核心风控;不要引入手册未列出的字段或外部平台 API 名称。\n");
|
||||
prompt.push_str("优化可以调整调仓周期、持仓数、市值带、filter.stock_expr、ordering.rank_expr、allocation.buy_scale、止盈止损;如上一轮无交易或质量分过低,必须先放宽过滤条件并优先使用已入库指标因子、rolling_mean/ma/vma/rolling_stddev/pct_change 等支持函数。\n");
|
||||
prompt.push_str("优化不限制在原策略已有参数或少量扰动。只要 OmniQuant/FIDC 已支持,可以自由增加、修改、删除策略代码、参数、候选池、过滤函数、排序、仓位、止盈止损、调仓周期、指标因子和辅助函数;不得引入手册未列出的字段或外部平台 API 名称。\n");
|
||||
prompt.push_str("可以使用所有已入库日频字段、指标因子和表达式函数,例如 rolling_mean/ma/vma/rolling_sum/rolling_stddev/pct_change/factor/factor_value/factors;如上一轮无交易或质量分过低,必须先扩大候选覆盖并修正不可交易过滤,再优化收益。\n");
|
||||
prompt.push_str("优化目标:\n");
|
||||
prompt.push_str(&format!("- {}\n\n", request.objective));
|
||||
prompt.push_str("当前策略代码如下,仅作为输入参考;回复时不要包含 Markdown 代码围栏:\n");
|
||||
|
||||
@@ -175,7 +175,7 @@ fn china_rule_hooks_block_buy_at_limit_up_and_sell_at_limit_down() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn china_rule_hooks_use_tick_size_tolerance_for_price_limits() {
|
||||
fn china_rule_hooks_use_strict_price_limits() {
|
||||
let hooks = ChinaEquityRuleHooks;
|
||||
let candidate = candidate();
|
||||
|
||||
@@ -184,6 +184,13 @@ fn china_rule_hooks_use_tick_size_tolerance_for_price_limits() {
|
||||
..snapshot(10.9995, 11.0, 9.0)
|
||||
};
|
||||
let buy_check = hooks.can_buy(d(2024, 1, 3), &near_upper, &candidate, PriceField::Open);
|
||||
assert!(buy_check.allowed);
|
||||
|
||||
let exact_upper = DailyMarketSnapshot {
|
||||
price_tick: 0.001,
|
||||
..snapshot(11.0, 11.0, 9.0)
|
||||
};
|
||||
let buy_check = hooks.can_buy(d(2024, 1, 3), &exact_upper, &candidate, PriceField::Open);
|
||||
assert!(!buy_check.allowed);
|
||||
|
||||
let near_lower = DailyMarketSnapshot {
|
||||
@@ -199,6 +206,19 @@ fn china_rule_hooks_use_tick_size_tolerance_for_price_limits() {
|
||||
&position,
|
||||
PriceField::Open,
|
||||
);
|
||||
assert!(sell_check.allowed);
|
||||
|
||||
let exact_lower = DailyMarketSnapshot {
|
||||
price_tick: 0.001,
|
||||
..snapshot(9.0, 11.0, 9.0)
|
||||
};
|
||||
let sell_check = hooks.can_sell(
|
||||
d(2024, 1, 3),
|
||||
&exact_lower,
|
||||
&candidate,
|
||||
&position,
|
||||
PriceField::Open,
|
||||
);
|
||||
assert!(!sell_check.allowed);
|
||||
}
|
||||
|
||||
|
||||
@@ -300,7 +300,7 @@ fn engine_reinvests_dividend_receivable_in_round_lots() {
|
||||
PriceField::Open,
|
||||
),
|
||||
BacktestConfig {
|
||||
initial_cash: 11_005.0,
|
||||
initial_cash: 11_008.0,
|
||||
benchmark_code: "000300.SH".to_string(),
|
||||
start_date: Some(buy_date),
|
||||
end_date: Some(payable_date),
|
||||
|
||||
@@ -0,0 +1,459 @@
|
||||
use chrono::{Duration, NaiveDate, NaiveTime};
|
||||
use fidc_core::{
|
||||
BacktestConfig, BacktestEngine, BenchmarkSnapshot, BrokerSimulator, CandidateEligibility,
|
||||
ChinaAShareCostModel, ChinaEquityRuleHooks, DailyFactorSnapshot, DailyMarketSnapshot, DataSet,
|
||||
IntradayExecutionQuote, MatchingType, OrderIntent, PriceField, Strategy, StrategyContext,
|
||||
StrategyDecision,
|
||||
};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
fn d(year: i32, month: u32, day: u32) -> NaiveDate {
|
||||
NaiveDate::from_ymd_opt(year, month, day).expect("valid date")
|
||||
}
|
||||
|
||||
fn t(hour: u32, minute: u32, second: u32) -> NaiveTime {
|
||||
NaiveTime::from_hms_opt(hour, minute, second).expect("valid time")
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct DecisionQuoteReader {
|
||||
day_count: usize,
|
||||
}
|
||||
|
||||
impl Strategy for DecisionQuoteReader {
|
||||
fn name(&self) -> &str {
|
||||
"decision_quote_reader"
|
||||
}
|
||||
|
||||
fn decision_quote_times(&self) -> Vec<NaiveTime> {
|
||||
vec![t(10, 40, 0)]
|
||||
}
|
||||
|
||||
fn on_day(
|
||||
&mut self,
|
||||
ctx: &StrategyContext<'_>,
|
||||
) -> Result<StrategyDecision, fidc_core::BacktestError> {
|
||||
self.day_count += 1;
|
||||
if self.day_count == 1 {
|
||||
return Ok(StrategyDecision {
|
||||
order_intents: vec![OrderIntent::Value {
|
||||
symbol: "000001.SZ".to_string(),
|
||||
value: 5_000.0,
|
||||
reason: "seed_position".to_string(),
|
||||
}],
|
||||
..StrategyDecision::default()
|
||||
});
|
||||
}
|
||||
|
||||
assert!(
|
||||
ctx.portfolio.position("000001.SZ").is_some(),
|
||||
"second day should carry the first day position"
|
||||
);
|
||||
let quote_loaded_before_decision = ctx
|
||||
.data
|
||||
.execution_quotes_on(ctx.execution_date, "000001.SZ")
|
||||
.iter()
|
||||
.any(|quote| quote.timestamp.time() == t(10, 39, 59) && quote.last_price == 11.0);
|
||||
assert!(
|
||||
quote_loaded_before_decision,
|
||||
"engine must load declared decision quote before strategy.on_day"
|
||||
);
|
||||
Ok(StrategyDecision::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn engine_preloads_declared_decision_quotes_for_current_positions() {
|
||||
let first = d(2026, 1, 5);
|
||||
let second = d(2026, 1, 6);
|
||||
let data = DataSet::from_components(
|
||||
Vec::new(),
|
||||
vec![
|
||||
DailyMarketSnapshot {
|
||||
date: first,
|
||||
symbol: "000001.SZ".to_string(),
|
||||
timestamp: Some("2026-01-05 15:00:00".to_string()),
|
||||
day_open: 10.0,
|
||||
open: 10.0,
|
||||
high: 10.2,
|
||||
low: 9.9,
|
||||
close: 10.0,
|
||||
last_price: 10.0,
|
||||
bid1: 10.0,
|
||||
ask1: 10.0,
|
||||
prev_close: 9.8,
|
||||
volume: 10_000,
|
||||
tick_volume: 1_000,
|
||||
bid1_volume: 10_000,
|
||||
ask1_volume: 10_000,
|
||||
trading_phase: Some("continuous".to_string()),
|
||||
paused: false,
|
||||
upper_limit: 10.78,
|
||||
lower_limit: 8.82,
|
||||
price_tick: 0.01,
|
||||
},
|
||||
DailyMarketSnapshot {
|
||||
date: second,
|
||||
symbol: "000001.SZ".to_string(),
|
||||
timestamp: Some("2026-01-06 15:00:00".to_string()),
|
||||
day_open: 10.5,
|
||||
open: 10.5,
|
||||
high: 11.2,
|
||||
low: 10.4,
|
||||
close: 10.6,
|
||||
last_price: 10.6,
|
||||
bid1: 10.6,
|
||||
ask1: 10.6,
|
||||
prev_close: 10.0,
|
||||
volume: 10_000,
|
||||
tick_volume: 1_000,
|
||||
bid1_volume: 10_000,
|
||||
ask1_volume: 10_000,
|
||||
trading_phase: Some("continuous".to_string()),
|
||||
paused: false,
|
||||
upper_limit: 11.0,
|
||||
lower_limit: 9.0,
|
||||
price_tick: 0.01,
|
||||
},
|
||||
],
|
||||
vec![
|
||||
DailyFactorSnapshot {
|
||||
date: first,
|
||||
symbol: "000001.SZ".to_string(),
|
||||
market_cap_bn: 10.0,
|
||||
free_float_cap_bn: 10.0,
|
||||
pe_ttm: 10.0,
|
||||
turnover_ratio: None,
|
||||
effective_turnover_ratio: None,
|
||||
extra_factors: Default::default(),
|
||||
},
|
||||
DailyFactorSnapshot {
|
||||
date: second,
|
||||
symbol: "000001.SZ".to_string(),
|
||||
market_cap_bn: 10.0,
|
||||
free_float_cap_bn: 10.0,
|
||||
pe_ttm: 10.0,
|
||||
turnover_ratio: None,
|
||||
effective_turnover_ratio: None,
|
||||
extra_factors: Default::default(),
|
||||
},
|
||||
],
|
||||
vec![
|
||||
CandidateEligibility {
|
||||
date: first,
|
||||
symbol: "000001.SZ".to_string(),
|
||||
is_st: false,
|
||||
is_new_listing: false,
|
||||
is_paused: false,
|
||||
allow_buy: true,
|
||||
allow_sell: true,
|
||||
is_kcb: false,
|
||||
is_one_yuan: false,
|
||||
},
|
||||
CandidateEligibility {
|
||||
date: second,
|
||||
symbol: "000001.SZ".to_string(),
|
||||
is_st: false,
|
||||
is_new_listing: false,
|
||||
is_paused: false,
|
||||
allow_buy: true,
|
||||
allow_sell: true,
|
||||
is_kcb: false,
|
||||
is_one_yuan: false,
|
||||
},
|
||||
],
|
||||
vec![
|
||||
BenchmarkSnapshot {
|
||||
date: first,
|
||||
benchmark: "000852.SH".to_string(),
|
||||
open: 1000.0,
|
||||
close: 1000.0,
|
||||
prev_close: 990.0,
|
||||
volume: 1_000_000,
|
||||
},
|
||||
BenchmarkSnapshot {
|
||||
date: second,
|
||||
benchmark: "000852.SH".to_string(),
|
||||
open: 1000.0,
|
||||
close: 1001.0,
|
||||
prev_close: 1000.0,
|
||||
volume: 1_000_000,
|
||||
},
|
||||
],
|
||||
)
|
||||
.expect("dataset");
|
||||
|
||||
let broker = BrokerSimulator::new_with_execution_price(
|
||||
ChinaAShareCostModel::default(),
|
||||
ChinaEquityRuleHooks,
|
||||
PriceField::Last,
|
||||
)
|
||||
.with_matching_type(MatchingType::NextTickLast)
|
||||
.with_intraday_execution_start_time(t(10, 40, 0));
|
||||
let config = BacktestConfig {
|
||||
initial_cash: 10_000.0,
|
||||
benchmark_code: "000852.SH".to_string(),
|
||||
start_date: Some(first),
|
||||
end_date: Some(second),
|
||||
decision_lag_trading_days: 0,
|
||||
execution_price_field: PriceField::Last,
|
||||
};
|
||||
let mut engine = BacktestEngine::new(data, DecisionQuoteReader::default(), broker, config)
|
||||
.with_execution_quote_loader(move |request| {
|
||||
assert_eq!(
|
||||
request.end_time, None,
|
||||
"decision quote preload must request latest quote at or before start_time"
|
||||
);
|
||||
Ok(request
|
||||
.symbols
|
||||
.into_iter()
|
||||
.map(|symbol| IntradayExecutionQuote {
|
||||
date: request.date,
|
||||
symbol,
|
||||
timestamp: request.date.and_time(t(10, 39, 59)),
|
||||
last_price: if request.date == second { 11.0 } else { 10.0 },
|
||||
bid1: if request.date == second { 11.0 } else { 10.0 },
|
||||
ask1: if request.date == second { 11.0 } else { 10.0 },
|
||||
bid1_volume: 10_000,
|
||||
ask1_volume: 10_000,
|
||||
volume_delta: 10_000,
|
||||
amount_delta: 100_000.0,
|
||||
trading_phase: Some("continuous".to_string()),
|
||||
})
|
||||
.collect())
|
||||
});
|
||||
|
||||
engine.run().expect("backtest should run");
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct MultiTimeDecisionQuoteReader {
|
||||
day_count: usize,
|
||||
}
|
||||
|
||||
impl Strategy for MultiTimeDecisionQuoteReader {
|
||||
fn name(&self) -> &str {
|
||||
"multi_time_decision_quote_reader"
|
||||
}
|
||||
|
||||
fn decision_quote_times(&self) -> Vec<NaiveTime> {
|
||||
vec![t(10, 31, 0), t(10, 40, 0)]
|
||||
}
|
||||
|
||||
fn on_day(
|
||||
&mut self,
|
||||
ctx: &StrategyContext<'_>,
|
||||
) -> Result<StrategyDecision, fidc_core::BacktestError> {
|
||||
self.day_count += 1;
|
||||
if self.day_count == 1 {
|
||||
return Ok(StrategyDecision {
|
||||
order_intents: vec![OrderIntent::Value {
|
||||
symbol: "000001.SZ".to_string(),
|
||||
value: 5_000.0,
|
||||
reason: "seed_position".to_string(),
|
||||
}],
|
||||
..StrategyDecision::default()
|
||||
});
|
||||
}
|
||||
|
||||
let quote_times = ctx
|
||||
.data
|
||||
.execution_quotes_on(ctx.execution_date, "000001.SZ")
|
||||
.iter()
|
||||
.map(|quote| quote.timestamp.time())
|
||||
.collect::<Vec<_>>();
|
||||
assert!(
|
||||
quote_times.contains(&t(10, 30, 59)),
|
||||
"10:31 decision quote must be loaded"
|
||||
);
|
||||
assert!(
|
||||
quote_times.contains(&t(10, 39, 59)),
|
||||
"10:40 decision quote must not be skipped because 10:31 was loaded"
|
||||
);
|
||||
Ok(StrategyDecision::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn engine_loads_distinct_decision_quote_times_on_same_day() {
|
||||
let first = d(2026, 1, 5);
|
||||
let second = d(2026, 1, 6);
|
||||
let data = DataSet::from_components(
|
||||
Vec::new(),
|
||||
vec![
|
||||
DailyMarketSnapshot {
|
||||
date: first,
|
||||
symbol: "000001.SZ".to_string(),
|
||||
timestamp: Some("2026-01-05 15:00:00".to_string()),
|
||||
day_open: 10.0,
|
||||
open: 10.0,
|
||||
high: 10.2,
|
||||
low: 9.9,
|
||||
close: 10.0,
|
||||
last_price: 10.0,
|
||||
bid1: 10.0,
|
||||
ask1: 10.0,
|
||||
prev_close: 9.8,
|
||||
volume: 10_000,
|
||||
tick_volume: 1_000,
|
||||
bid1_volume: 10_000,
|
||||
ask1_volume: 10_000,
|
||||
trading_phase: Some("continuous".to_string()),
|
||||
paused: false,
|
||||
upper_limit: 10.78,
|
||||
lower_limit: 8.82,
|
||||
price_tick: 0.01,
|
||||
},
|
||||
DailyMarketSnapshot {
|
||||
date: second,
|
||||
symbol: "000001.SZ".to_string(),
|
||||
timestamp: Some("2026-01-06 15:00:00".to_string()),
|
||||
day_open: 10.5,
|
||||
open: 10.5,
|
||||
high: 11.2,
|
||||
low: 10.4,
|
||||
close: 10.6,
|
||||
last_price: 10.6,
|
||||
bid1: 10.6,
|
||||
ask1: 10.6,
|
||||
prev_close: 10.0,
|
||||
volume: 10_000,
|
||||
tick_volume: 1_000,
|
||||
bid1_volume: 10_000,
|
||||
ask1_volume: 10_000,
|
||||
trading_phase: Some("continuous".to_string()),
|
||||
paused: false,
|
||||
upper_limit: 11.0,
|
||||
lower_limit: 9.0,
|
||||
price_tick: 0.01,
|
||||
},
|
||||
],
|
||||
vec![
|
||||
DailyFactorSnapshot {
|
||||
date: first,
|
||||
symbol: "000001.SZ".to_string(),
|
||||
market_cap_bn: 10.0,
|
||||
free_float_cap_bn: 10.0,
|
||||
pe_ttm: 10.0,
|
||||
turnover_ratio: None,
|
||||
effective_turnover_ratio: None,
|
||||
extra_factors: Default::default(),
|
||||
},
|
||||
DailyFactorSnapshot {
|
||||
date: second,
|
||||
symbol: "000001.SZ".to_string(),
|
||||
market_cap_bn: 10.0,
|
||||
free_float_cap_bn: 10.0,
|
||||
pe_ttm: 10.0,
|
||||
turnover_ratio: None,
|
||||
effective_turnover_ratio: None,
|
||||
extra_factors: Default::default(),
|
||||
},
|
||||
],
|
||||
vec![
|
||||
CandidateEligibility {
|
||||
date: first,
|
||||
symbol: "000001.SZ".to_string(),
|
||||
is_st: false,
|
||||
is_new_listing: false,
|
||||
is_paused: false,
|
||||
allow_buy: true,
|
||||
allow_sell: true,
|
||||
is_kcb: false,
|
||||
is_one_yuan: false,
|
||||
},
|
||||
CandidateEligibility {
|
||||
date: second,
|
||||
symbol: "000001.SZ".to_string(),
|
||||
is_st: false,
|
||||
is_new_listing: false,
|
||||
is_paused: false,
|
||||
allow_buy: true,
|
||||
allow_sell: true,
|
||||
is_kcb: false,
|
||||
is_one_yuan: false,
|
||||
},
|
||||
],
|
||||
vec![
|
||||
BenchmarkSnapshot {
|
||||
date: first,
|
||||
benchmark: "000852.SH".to_string(),
|
||||
open: 1000.0,
|
||||
close: 1000.0,
|
||||
prev_close: 990.0,
|
||||
volume: 1_000_000,
|
||||
},
|
||||
BenchmarkSnapshot {
|
||||
date: second,
|
||||
benchmark: "000852.SH".to_string(),
|
||||
open: 1000.0,
|
||||
close: 1001.0,
|
||||
prev_close: 1000.0,
|
||||
volume: 1_000_000,
|
||||
},
|
||||
],
|
||||
)
|
||||
.expect("dataset");
|
||||
|
||||
let broker = BrokerSimulator::new_with_execution_price(
|
||||
ChinaAShareCostModel::default(),
|
||||
ChinaEquityRuleHooks,
|
||||
PriceField::Last,
|
||||
)
|
||||
.with_matching_type(MatchingType::NextTickLast)
|
||||
.with_intraday_execution_start_time(t(10, 40, 0));
|
||||
let config = BacktestConfig {
|
||||
initial_cash: 10_000.0,
|
||||
benchmark_code: "000852.SH".to_string(),
|
||||
start_date: Some(first),
|
||||
end_date: Some(second),
|
||||
decision_lag_trading_days: 0,
|
||||
execution_price_field: PriceField::Last,
|
||||
};
|
||||
let requests = Arc::new(Mutex::new(Vec::<(NaiveDate, NaiveTime)>::new()));
|
||||
let captured_requests = Arc::clone(&requests);
|
||||
let mut engine = BacktestEngine::new(
|
||||
data,
|
||||
MultiTimeDecisionQuoteReader::default(),
|
||||
broker,
|
||||
config,
|
||||
)
|
||||
.with_execution_quote_loader(move |request| {
|
||||
let start_time = request
|
||||
.start_time
|
||||
.expect("decision quote loader request must include start_time");
|
||||
captured_requests
|
||||
.lock()
|
||||
.expect("request mutex")
|
||||
.push((request.date, start_time));
|
||||
Ok(request
|
||||
.symbols
|
||||
.into_iter()
|
||||
.map(|symbol| IntradayExecutionQuote {
|
||||
date: request.date,
|
||||
symbol,
|
||||
timestamp: request.date.and_time(start_time) - Duration::seconds(1),
|
||||
last_price: 10.0,
|
||||
bid1: 10.0,
|
||||
ask1: 10.0,
|
||||
bid1_volume: 10_000,
|
||||
ask1_volume: 10_000,
|
||||
volume_delta: 10_000,
|
||||
amount_delta: 100_000.0,
|
||||
trading_phase: Some("continuous".to_string()),
|
||||
})
|
||||
.collect())
|
||||
});
|
||||
|
||||
engine.run().expect("backtest should run");
|
||||
|
||||
let requests = requests.lock().expect("request mutex").clone();
|
||||
assert!(
|
||||
requests.contains(&(second, t(10, 31, 0))),
|
||||
"second-day 10:31 quote request is required"
|
||||
);
|
||||
assert!(
|
||||
requests.contains(&(second, t(10, 40, 0))),
|
||||
"second-day 10:40 quote request must not be skipped by earlier quote"
|
||||
);
|
||||
}
|
||||
@@ -43,7 +43,8 @@ impl Strategy for BuyThenHoldStrategy {
|
||||
#[test]
|
||||
fn engine_settles_delisted_position_before_missing_market_snapshot_breaks_run() {
|
||||
let date1 = d(2025, 1, 2);
|
||||
let date2 = d(2025, 1, 3);
|
||||
let delist_date = d(2025, 1, 3);
|
||||
let date2 = d(2025, 1, 6);
|
||||
let data = DataSet::from_components(
|
||||
vec![
|
||||
Instrument {
|
||||
@@ -52,8 +53,8 @@ fn engine_settles_delisted_position_before_missing_market_snapshot_breaks_run()
|
||||
board: "SZ".to_string(),
|
||||
round_lot: 100,
|
||||
listed_at: Some(d(2020, 1, 1)),
|
||||
delisted_at: Some(date1),
|
||||
status: "delisted".to_string(),
|
||||
delisted_at: Some(delist_date),
|
||||
status: "active".to_string(),
|
||||
},
|
||||
Instrument {
|
||||
symbol: "000002.SZ".to_string(),
|
||||
@@ -115,7 +116,7 @@ fn engine_settles_delisted_position_before_missing_market_snapshot_breaks_run()
|
||||
DailyMarketSnapshot {
|
||||
date: date2,
|
||||
symbol: "000002.SZ".to_string(),
|
||||
timestamp: Some("2025-01-03 10:18:00".to_string()),
|
||||
timestamp: Some("2025-01-06 10:18:00".to_string()),
|
||||
day_open: 5.1,
|
||||
open: 5.1,
|
||||
high: 5.2,
|
||||
@@ -273,7 +274,7 @@ fn engine_applies_successor_conversion_before_delisted_cash_settlement() {
|
||||
round_lot: 100,
|
||||
listed_at: Some(d(2020, 1, 1)),
|
||||
delisted_at: Some(date2),
|
||||
status: "delisted".to_string(),
|
||||
status: "active".to_string(),
|
||||
},
|
||||
Instrument {
|
||||
symbol: "000002.SZ".to_string(),
|
||||
@@ -492,7 +493,7 @@ fn engine_applies_successor_conversion_before_delisted_cash_settlement() {
|
||||
.iter()
|
||||
.find(|holding| holding.symbol == "000002.SZ")
|
||||
.expect("successor holding exists");
|
||||
assert_eq!(successor_holding.quantity, 500);
|
||||
assert_eq!(successor_holding.quantity, 450);
|
||||
assert!(
|
||||
result
|
||||
.holdings_summary
|
||||
@@ -503,6 +504,6 @@ fn engine_applies_successor_conversion_before_delisted_cash_settlement() {
|
||||
event
|
||||
.note
|
||||
.contains("successor_conversion 000001.SZ->000002.SZ")
|
||||
&& event.note.contains("cash=1000.00")
|
||||
&& event.note.contains("cash=900.00")
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -329,7 +329,7 @@ impl Strategy for AuctionOrderStrategy {
|
||||
exit_symbols: BTreeSet::new(),
|
||||
order_intents: vec![fidc_core::OrderIntent::Value {
|
||||
symbol: "000001.SZ".to_string(),
|
||||
value: 1_000.0,
|
||||
value: 1_010.0,
|
||||
reason: "auction_buy".to_string(),
|
||||
}],
|
||||
notes: Vec::new(),
|
||||
@@ -3734,7 +3734,7 @@ impl Strategy for BuyMissingRowThenHoldStrategy {
|
||||
exit_symbols: BTreeSet::new(),
|
||||
order_intents: vec![OrderIntent::Value {
|
||||
symbol: "601028.SH".to_string(),
|
||||
value: 1_000.0,
|
||||
value: 1_010.0,
|
||||
reason: "seed_position".to_string(),
|
||||
}],
|
||||
notes: Vec::new(),
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use chrono::{NaiveDate, NaiveTime};
|
||||
use fidc_core::{
|
||||
AlgoOrderStyle, BenchmarkSnapshot, BrokerSimulator, CandidateEligibility, ChinaAShareCostModel,
|
||||
ChinaEquityRuleHooks, DailyFactorSnapshot, DailyMarketSnapshot, DataSet, Instrument,
|
||||
IntradayExecutionQuote, MatchingType, OrderIntent, OrderStatus, PortfolioState, PriceField,
|
||||
ProcessEventKind, SlippageModel, StrategyDecision, TargetPortfolioOrderPricing,
|
||||
ChinaEquityRuleHooks, DailyFactorSnapshot, DailyMarketSnapshot, DataSet, DynamicSlippageConfig,
|
||||
Instrument, IntradayExecutionQuote, MatchingType, OrderIntent, OrderStatus, PortfolioState,
|
||||
PriceField, ProcessEventKind, SlippageModel, StrategyDecision, TargetPortfolioOrderPricing,
|
||||
};
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
@@ -1485,6 +1485,110 @@ fn broker_applies_price_ratio_slippage_on_snapshot_fills() {
|
||||
assert!((report.fill_events[0].price - 10.1).abs() < 1e-9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn broker_applies_dynamic_slippage_on_snapshot_fills() {
|
||||
let date = NaiveDate::from_ymd_opt(2024, 1, 10).unwrap();
|
||||
let data = DataSet::from_components(
|
||||
vec![Instrument {
|
||||
symbol: "000002.SZ".to_string(),
|
||||
name: "Test".to_string(),
|
||||
board: "SZ".to_string(),
|
||||
round_lot: 100,
|
||||
listed_at: None,
|
||||
delisted_at: None,
|
||||
status: "active".to_string(),
|
||||
}],
|
||||
vec![DailyMarketSnapshot {
|
||||
date,
|
||||
symbol: "000002.SZ".to_string(),
|
||||
timestamp: Some("2024-01-10 10:18:00".to_string()),
|
||||
day_open: 10.0,
|
||||
open: 10.0,
|
||||
high: 10.1,
|
||||
low: 9.9,
|
||||
close: 10.0,
|
||||
last_price: 10.0,
|
||||
bid1: 9.99,
|
||||
ask1: 10.01,
|
||||
prev_close: 10.0,
|
||||
volume: 100_000,
|
||||
tick_volume: 100_000,
|
||||
bid1_volume: 80_000,
|
||||
ask1_volume: 80_000,
|
||||
trading_phase: Some("continuous".to_string()),
|
||||
paused: false,
|
||||
upper_limit: 11.0,
|
||||
lower_limit: 9.0,
|
||||
price_tick: 0.01,
|
||||
}],
|
||||
vec![DailyFactorSnapshot {
|
||||
date,
|
||||
symbol: "000002.SZ".to_string(),
|
||||
market_cap_bn: 50.0,
|
||||
free_float_cap_bn: 45.0,
|
||||
pe_ttm: 15.0,
|
||||
turnover_ratio: Some(2.0),
|
||||
effective_turnover_ratio: Some(1.8),
|
||||
extra_factors: BTreeMap::new(),
|
||||
}],
|
||||
vec![CandidateEligibility {
|
||||
date,
|
||||
symbol: "000002.SZ".to_string(),
|
||||
is_st: false,
|
||||
is_new_listing: false,
|
||||
is_paused: false,
|
||||
allow_buy: true,
|
||||
allow_sell: true,
|
||||
is_kcb: false,
|
||||
is_one_yuan: false,
|
||||
}],
|
||||
vec![BenchmarkSnapshot {
|
||||
date,
|
||||
benchmark: "000300.SH".to_string(),
|
||||
open: 100.0,
|
||||
close: 100.0,
|
||||
prev_close: 99.0,
|
||||
volume: 1_000_000,
|
||||
}],
|
||||
)
|
||||
.expect("dataset");
|
||||
let mut portfolio = PortfolioState::new(1_000_000.0);
|
||||
let broker = BrokerSimulator::new_with_execution_price(
|
||||
ChinaAShareCostModel::default(),
|
||||
ChinaEquityRuleHooks::default(),
|
||||
PriceField::Open,
|
||||
)
|
||||
.with_slippage_model(SlippageModel::Dynamic(DynamicSlippageConfig::new(
|
||||
0.5, 0.3, 0.1,
|
||||
)));
|
||||
|
||||
let report = broker
|
||||
.execute(
|
||||
date,
|
||||
&mut portfolio,
|
||||
&data,
|
||||
&StrategyDecision {
|
||||
rebalance: false,
|
||||
target_weights: BTreeMap::new(),
|
||||
exit_symbols: BTreeSet::new(),
|
||||
order_intents: vec![OrderIntent::Value {
|
||||
symbol: "000002.SZ".to_string(),
|
||||
value: 100_000.0,
|
||||
reason: "dynamic_slippage".to_string(),
|
||||
}],
|
||||
notes: Vec::new(),
|
||||
diagnostics: Vec::new(),
|
||||
},
|
||||
)
|
||||
.expect("broker execution");
|
||||
|
||||
assert_eq!(report.fill_events.len(), 1);
|
||||
let expected_ratio = ((10.0 * report.fill_events[0].quantity as f64) / (100_000.0 * 10.0))
|
||||
* 0.5
|
||||
+ ((10.1 - 9.9) / 10.0) * 0.3;
|
||||
assert!((report.fill_events[0].price - 10.0 * (1.0 + expected_ratio)).abs() < 1e-9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn broker_applies_tick_size_slippage_on_intraday_last_fills() {
|
||||
let date = NaiveDate::from_ymd_opt(2024, 1, 10).unwrap();
|
||||
@@ -1554,7 +1658,7 @@ fn broker_applies_tick_size_slippage_on_intraday_last_fills() {
|
||||
vec![IntradayExecutionQuote {
|
||||
date,
|
||||
symbol: "000002.SZ".to_string(),
|
||||
timestamp: date.and_hms_opt(10, 18, 3).unwrap(),
|
||||
timestamp: date.and_hms_opt(10, 18, 0).unwrap(),
|
||||
last_price: 10.0,
|
||||
bid1: 9.99,
|
||||
ask1: 10.01,
|
||||
@@ -1597,6 +1701,9 @@ fn broker_applies_tick_size_slippage_on_intraday_last_fills() {
|
||||
|
||||
assert_eq!(report.fill_events.len(), 1);
|
||||
assert!((report.fill_events[0].price - 10.02).abs() < 1e-9);
|
||||
let position = portfolio.position("000002.SZ").expect("position");
|
||||
assert!((position.last_price - 10.0).abs() < 1e-9);
|
||||
assert!((position.market_value() - position.quantity as f64 * 10.0).abs() < 1e-6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1700,7 +1807,7 @@ fn broker_rejects_intraday_last_order_without_execution_quotes() {
|
||||
assert!(
|
||||
report.order_events[0]
|
||||
.reason
|
||||
.contains("no execution quotes after start")
|
||||
.contains("no execution quotes at or before start")
|
||||
);
|
||||
assert!(portfolio.position("000002.SZ").is_none());
|
||||
}
|
||||
@@ -1889,7 +1996,7 @@ fn broker_cancels_market_order_remainder_when_intraday_quote_liquidity_exhausted
|
||||
vec![IntradayExecutionQuote {
|
||||
date,
|
||||
symbol: "000002.SZ".to_string(),
|
||||
timestamp: date.and_hms_opt(10, 18, 3).unwrap(),
|
||||
timestamp: date.and_hms_opt(10, 18, 0).unwrap(),
|
||||
last_price: 10.02,
|
||||
bid1: 10.01,
|
||||
ask1: 10.03,
|
||||
@@ -4130,6 +4237,50 @@ fn broker_uses_limit_price_slippage_for_limit_orders() {
|
||||
assert!((report.fill_events[0].price - 10.1).abs() < 1e-9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn broker_rejects_limit_buy_when_final_execution_price_reaches_upper_limit() {
|
||||
let date = NaiveDate::from_ymd_opt(2024, 1, 10).unwrap();
|
||||
let data = two_day_limit_order_data(10.0, 10.2);
|
||||
let broker = BrokerSimulator::new_with_execution_price(
|
||||
ChinaAShareCostModel::default(),
|
||||
ChinaEquityRuleHooks::default(),
|
||||
PriceField::Open,
|
||||
)
|
||||
.with_slippage_model(SlippageModel::LimitPrice);
|
||||
let mut portfolio = PortfolioState::new(1_000_000.0);
|
||||
|
||||
let report = broker
|
||||
.execute(
|
||||
date,
|
||||
&mut portfolio,
|
||||
&data,
|
||||
&StrategyDecision {
|
||||
rebalance: false,
|
||||
target_weights: BTreeMap::new(),
|
||||
exit_symbols: BTreeSet::new(),
|
||||
order_intents: vec![OrderIntent::LimitShares {
|
||||
symbol: "000002.SZ".to_string(),
|
||||
quantity: 200,
|
||||
limit_price: 11.0,
|
||||
reason: "limit_entry_at_upper_limit".to_string(),
|
||||
}],
|
||||
notes: Vec::new(),
|
||||
diagnostics: Vec::new(),
|
||||
},
|
||||
)
|
||||
.expect("broker execution");
|
||||
|
||||
assert!(report.fill_events.is_empty());
|
||||
assert_eq!(report.order_events.len(), 1);
|
||||
assert_eq!(report.order_events[0].status, OrderStatus::Canceled);
|
||||
assert!(
|
||||
report.order_events[0]
|
||||
.reason
|
||||
.contains("open at or above upper limit")
|
||||
);
|
||||
assert!(portfolio.position("000002.SZ").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn broker_executes_limit_value_and_limit_percent_intents() {
|
||||
let date = NaiveDate::from_ymd_opt(2024, 1, 10).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user