329 lines
11 KiB
Rust
329 lines
11 KiB
Rust
use chrono::{NaiveDate, NaiveTime};
|
|
use fidc_core::{
|
|
BacktestConfig, BacktestEngine, BrokerSimulator, ChinaAShareCostModel, ChinaEquityRuleHooks,
|
|
DataSet, MatchingType, OrderIntent, PriceField, Strategy, StrategyContext, StrategyDecision,
|
|
};
|
|
use std::{cell::RefCell, collections::BTreeSet, rc::Rc};
|
|
|
|
const SYMBOL: &str = "000001.SZ";
|
|
fn day(value: u32) -> NaiveDate {
|
|
NaiveDate::from_ymd_opt(2026, 9, value).unwrap()
|
|
}
|
|
|
|
fn data() -> DataSet {
|
|
let days = [11, 14, 15].map(day);
|
|
DataSet::from_components_with_actions_and_quotes(
|
|
vec![fidc_core::Instrument {
|
|
symbol: SYMBOL.into(),
|
|
name: "fixture".into(),
|
|
board: "SZ".into(),
|
|
round_lot: 100,
|
|
listed_at: Some(day(1)),
|
|
delisted_at: None,
|
|
status: "active".into(),
|
|
}],
|
|
days.iter()
|
|
.map(|&date| {
|
|
let price = if date == day(11) { 10. } else { 8.95 };
|
|
fidc_core::DailyMarketSnapshot {
|
|
date,
|
|
symbol: SYMBOL.into(),
|
|
timestamp: Some(format!("{date} 15:00:00")),
|
|
day_open: price,
|
|
open: price,
|
|
high: price,
|
|
low: price,
|
|
close: price,
|
|
last_price: price,
|
|
bid1: price,
|
|
ask1: price,
|
|
prev_close: price,
|
|
volume: 100000,
|
|
minute_volume: 100000,
|
|
bid1_volume: 100000,
|
|
ask1_volume: 100000,
|
|
trading_phase: Some("continuous".into()),
|
|
paused: false,
|
|
upper_limit: price * 1.1,
|
|
lower_limit: price * 0.9,
|
|
price_tick: 0.01,
|
|
}
|
|
})
|
|
.collect(),
|
|
days.iter()
|
|
.map(|&date| fidc_core::DailyFactorSnapshot {
|
|
date,
|
|
symbol: SYMBOL.into(),
|
|
market_cap_bn: 10.,
|
|
free_float_cap_bn: 10.,
|
|
pe_ttm: 10.,
|
|
turnover_ratio: None,
|
|
effective_turnover_ratio: None,
|
|
adjustment_factor_backward1: Some(1.),
|
|
extra_factors: Default::default(),
|
|
})
|
|
.collect(),
|
|
days.iter()
|
|
.map(|&date| fidc_core::CandidateEligibility {
|
|
date,
|
|
symbol: SYMBOL.into(),
|
|
is_st: false,
|
|
is_star_st: false,
|
|
is_new_listing: false,
|
|
is_paused: false,
|
|
allow_buy: true,
|
|
allow_sell: true,
|
|
is_kcb: false,
|
|
is_one_yuan: false,
|
|
risk_level_code: None,
|
|
})
|
|
.collect(),
|
|
days.iter()
|
|
.map(|&date| fidc_core::BenchmarkSnapshot {
|
|
date,
|
|
benchmark: "000300.SH".into(),
|
|
open: 100.,
|
|
close: 100.,
|
|
prev_close: 100.,
|
|
volume: 100000,
|
|
})
|
|
.collect(),
|
|
vec![fidc_core::CorporateAction {
|
|
date: day(14),
|
|
symbol: SYMBOL.into(),
|
|
payable_date: Some(day(14)),
|
|
share_cash: 1.05,
|
|
share_bonus: 0.,
|
|
share_gift: 0.,
|
|
issue_quantity: 0.,
|
|
issue_price: 0.,
|
|
reform: false,
|
|
adjust_factor: None,
|
|
successor_symbol: None,
|
|
successor_ratio: None,
|
|
successor_cash: None,
|
|
}],
|
|
[(9, 15), (9, 31)]
|
|
.into_iter()
|
|
.map(|(hour, minute)| fidc_core::IntradayExecutionQuote {
|
|
observation_kind: fidc_core::data::QuoteObservationKind::MinuteBar,
|
|
date: day(14),
|
|
symbol: SYMBOL.into(),
|
|
timestamp: day(14).and_hms_opt(hour, minute, 0).unwrap(),
|
|
last_price: 8.95,
|
|
bid1: 8.95,
|
|
ask1: 8.95,
|
|
bid1_volume: 100000,
|
|
ask1_volume: 100000,
|
|
volume_delta: 10000,
|
|
amount_delta: 89500.,
|
|
trading_phase: Some("continuous".into()),
|
|
})
|
|
.collect(),
|
|
)
|
|
.unwrap()
|
|
}
|
|
|
|
struct Hold {
|
|
seen: Rc<RefCell<Vec<(NaiveTime, u32)>>>,
|
|
}
|
|
impl Strategy for Hold {
|
|
fn name(&self) -> &str {
|
|
"accounting reinvestment contract"
|
|
}
|
|
fn initial_subscriptions(&self) -> BTreeSet<String> {
|
|
[SYMBOL.into()].into()
|
|
}
|
|
fn on_day(
|
|
&mut self,
|
|
ctx: &StrategyContext<'_>,
|
|
) -> Result<StrategyDecision, fidc_core::BacktestError> {
|
|
Ok(if ctx.execution_date == day(11) {
|
|
StrategyDecision {
|
|
order_intents: vec![OrderIntent::Shares {
|
|
symbol: SYMBOL.into(),
|
|
quantity: 1000,
|
|
reason: "initial".into(),
|
|
}],
|
|
..Default::default()
|
|
}
|
|
} else {
|
|
StrategyDecision::default()
|
|
})
|
|
}
|
|
fn on_minute(
|
|
&mut self,
|
|
ctx: &StrategyContext<'_>,
|
|
_: &fidc_core::IntradayExecutionQuote,
|
|
) -> Result<StrategyDecision, fidc_core::BacktestError> {
|
|
self.seen.borrow_mut().push((
|
|
ctx.current_time().unwrap(),
|
|
ctx.portfolio
|
|
.position(SYMBOL)
|
|
.map_or(0, |position| position.quantity),
|
|
));
|
|
Ok(Default::default())
|
|
}
|
|
}
|
|
|
|
fn engine() -> BacktestEngine<Hold, ChinaAShareCostModel, ChinaEquityRuleHooks> {
|
|
BacktestEngine::new(
|
|
data(),
|
|
Hold {
|
|
seen: Rc::new(RefCell::new(Vec::new())),
|
|
},
|
|
BrokerSimulator::new_with_execution_price(
|
|
ChinaAShareCostModel::default()
|
|
.with_commission_rate(0.0008)
|
|
.with_minimum_commission(0.),
|
|
ChinaEquityRuleHooks,
|
|
PriceField::Open,
|
|
)
|
|
.with_matching_type(MatchingType::NextBarOpen)
|
|
.with_volume_limit(false)
|
|
.with_liquidity_limit(false),
|
|
BacktestConfig {
|
|
initial_cash: 50000.,
|
|
benchmark_code: "000300.SH".into(),
|
|
start_date: Some(day(11)),
|
|
end_date: Some(day(15)),
|
|
decision_lag_trading_days: 0,
|
|
execution_price_field: PriceField::Open,
|
|
},
|
|
)
|
|
.with_dividend_reinvestment(true)
|
|
}
|
|
|
|
#[test]
|
|
fn accounting_reinvestment_has_an_explicit_origin_clock_and_progress_delivery() {
|
|
let mut progress = Vec::new();
|
|
let result = engine()
|
|
.run_with_progress(|event| progress.push(event.clone()))
|
|
.unwrap();
|
|
let reinvest = result
|
|
.fills
|
|
.iter()
|
|
.find(|fill| fill.reason == "dividend_reinvestment")
|
|
.unwrap();
|
|
assert_eq!(
|
|
(
|
|
reinvest.quantity,
|
|
reinvest.price,
|
|
reinvest.commission,
|
|
reinvest.order_id
|
|
),
|
|
(100, 8.95, 0., None)
|
|
);
|
|
assert_eq!(
|
|
serde_json::to_value(reinvest).unwrap()["origin"],
|
|
"dividend_reinvestment"
|
|
);
|
|
assert_eq!(reinvest.execution_timestamp, day(14).and_hms_opt(0, 0, 0));
|
|
let received = progress.iter().find(|event| event.date == day(14)).unwrap();
|
|
assert!(
|
|
received
|
|
.fills
|
|
.iter()
|
|
.any(|fill| fill.reason == "dividend_reinvestment")
|
|
);
|
|
assert_eq!(
|
|
progress
|
|
.iter()
|
|
.map(|event| event.daily_fill_count)
|
|
.sum::<usize>(),
|
|
result.fills.len()
|
|
);
|
|
}
|
|
|
|
fn manual_source(delayed: bool) -> fidc_core::manual_execution::ManualExecutionReplay {
|
|
let observed = if delayed {
|
|
"2026-09-14T01:15:00Z"
|
|
} else {
|
|
"2026-09-11T06:00:01Z"
|
|
};
|
|
let mut source: fidc_core::manual_execution::ManualExecutionReplay = serde_json::from_value(serde_json::json!({
|
|
"schema":fidc_core::manual_execution::MANUAL_REPLAY_SCHEMA,"runtimeId":"r","accountId":"a","sourceContractSha256":"a".repeat(64),"contentSha256":"",
|
|
"observationCutoff":"2026-09-15T08:00:00Z","actions":[{"actionId":"manual","source":"manual_security_trade","auditEventIds":["audit"],
|
|
"confirmedAt":"2026-09-11T05:59:59Z","confirmationObservedAt":"2026-09-11T05:59:59Z","outcome":"orders_terminal","orders":[{
|
|
"orderId":"manual-order","sourceAdapter":"paper","symbol":SYMBOL,"side":"Buy","quantity":1000,
|
|
"orderCreatedAt":"2026-09-11T05:59:59Z","terminalObservedAt":observed,"terminalStatus":"filled","fills":[{
|
|
"tradeId":"manual-fill","observationEventId":"receipt","observationSequence":1,"feeObservationEventId":"receipt","feeObservationSequence":1,
|
|
"feeObservedAt":observed,"tradeDate":"2026-09-11","executedAt":"2026-09-11T06:00:00Z","observedAt":observed,
|
|
"timestampPrecision":"second","quantity":1000,"price":"10","totalFee":"1"
|
|
}]
|
|
}]}]
|
|
})).unwrap();
|
|
source.content_sha256 = source.content_digest().unwrap();
|
|
source
|
|
}
|
|
|
|
#[test]
|
|
fn delayed_receipt_before_market_open_reconciles_accounting_not_future_market_fills() {
|
|
let timely = engine()
|
|
.with_observed_manual_executions(manual_source(false))
|
|
.unwrap()
|
|
.run()
|
|
.unwrap();
|
|
let delayed = engine()
|
|
.with_observed_manual_executions(manual_source(true))
|
|
.unwrap()
|
|
.run()
|
|
.unwrap();
|
|
assert_eq!(delayed.holdings_summary[0].quantity, 2200);
|
|
assert_eq!(
|
|
delayed.holdings_summary[0].quantity,
|
|
timely.holdings_summary[0].quantity
|
|
);
|
|
assert_eq!(
|
|
delayed.equity_curve.last().unwrap().cash,
|
|
timely.equity_curve.last().unwrap().cash
|
|
);
|
|
assert_eq!(
|
|
delayed.manual_executions[0]
|
|
.corporate_adjustment
|
|
.as_ref()
|
|
.unwrap()
|
|
.corporate_cash_delta,
|
|
"155"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn weekend_receipts_and_morning_allocations_are_in_the_next_progress_batch() {
|
|
let mut source = manual_source(true);
|
|
let observed = "2026-09-12T02:00:00Z".parse().unwrap();
|
|
let order = &mut source.actions[0].orders[0];
|
|
order.terminal_observed_at = observed;
|
|
order.fills[0].observed_at = observed;
|
|
order.fills[0].fee_observed_at = observed;
|
|
source.content_sha256 = source.content_digest().unwrap();
|
|
let mut progress = Vec::new();
|
|
let result = engine()
|
|
.with_observed_manual_executions(source)
|
|
.unwrap()
|
|
.run_with_progress(|event| progress.push(event.clone()))
|
|
.unwrap();
|
|
let monday = progress.iter().find(|event| event.date == day(14)).unwrap();
|
|
assert_eq!(monday.daily_manual_fill_count, 1);
|
|
assert_eq!(monday.manual_executions[0].observed_at, observed);
|
|
assert!(
|
|
monday
|
|
.fills
|
|
.iter()
|
|
.any(|fill| fill.origin == fidc_core::FillOrigin::DividendReinvestment)
|
|
);
|
|
assert!(
|
|
monday
|
|
.process_events
|
|
.iter()
|
|
.any(|event| event.kind == fidc_core::ProcessEventKind::ManualExecutionObserved)
|
|
);
|
|
assert_eq!(
|
|
progress
|
|
.iter()
|
|
.map(|event| event.daily_fill_count)
|
|
.sum::<usize>(),
|
|
result.fills.len() + result.manual_executions.len()
|
|
);
|
|
}
|