修复回报上下文与盘前意图并在提交前采用最新完整目标
This commit is contained in:
@@ -1535,6 +1535,90 @@ fn engine_executes_futures_order_intents_against_future_account() {
|
||||
assert!((futures_account.cash() - 355_988.0).abs() < 1e-6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn futures_directive_notifications_include_the_actual_recorded_fill() {
|
||||
struct Observed {
|
||||
inner: FuturesOrderStrategy,
|
||||
seen: Rc<RefCell<Vec<u64>>>,
|
||||
}
|
||||
impl Strategy for Observed {
|
||||
fn name(&self) -> &str {
|
||||
"observed-futures-directive"
|
||||
}
|
||||
fn on_day(
|
||||
&mut self,
|
||||
ctx: &StrategyContext<'_>,
|
||||
) -> Result<StrategyDecision, fidc_core::BacktestError> {
|
||||
self.inner.on_day(ctx)
|
||||
}
|
||||
fn on_process_event(
|
||||
&mut self,
|
||||
ctx: &StrategyContext<'_>,
|
||||
event: &ProcessEvent,
|
||||
) -> Result<(), fidc_core::BacktestError> {
|
||||
if event.kind == ProcessEventKind::Trade
|
||||
&& event.symbol.as_deref() == Some("IF2501")
|
||||
{
|
||||
let id = event.order_id.unwrap();
|
||||
assert!(
|
||||
ctx.fills
|
||||
.iter()
|
||||
.any(|fill| fill.order_id == Some(id) && fill.symbol == "IF2501")
|
||||
);
|
||||
assert!(
|
||||
ctx.order_events
|
||||
.iter()
|
||||
.any(|order| order.order_id == Some(id)
|
||||
&& order.status == OrderStatus::Filled)
|
||||
);
|
||||
assert_eq!(
|
||||
ctx.current_datetime().map(|time| time.date()),
|
||||
Some(ctx.execution_date)
|
||||
);
|
||||
self.seen.borrow_mut().push(id);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
let seen = Rc::new(RefCell::new(Vec::new()));
|
||||
let broker = BrokerSimulator::new_with_execution_price(
|
||||
ChinaAShareCostModel::default(),
|
||||
ChinaEquityRuleHooks,
|
||||
PriceField::Open,
|
||||
)
|
||||
.with_volume_capacity_mode(
|
||||
fidc_core::execution_capacity::VolumeCapacityMode::SessionCapacityAudit,
|
||||
);
|
||||
let mut engine = BacktestEngine::new(
|
||||
two_day_futures_data(),
|
||||
Observed {
|
||||
inner: FuturesOrderStrategy,
|
||||
seen: seen.clone(),
|
||||
},
|
||||
broker,
|
||||
BacktestConfig {
|
||||
initial_cash: 100_000.,
|
||||
benchmark_code: "000300.SH".into(),
|
||||
start_date: Some(d(2025, 1, 2)),
|
||||
end_date: Some(d(2025, 1, 3)),
|
||||
decision_lag_trading_days: 0,
|
||||
execution_price_field: PriceField::Open,
|
||||
},
|
||||
)
|
||||
.with_futures_initial_cash(500_000.);
|
||||
let result = engine.run().unwrap();
|
||||
assert_eq!(
|
||||
*seen.borrow(),
|
||||
result
|
||||
.fills
|
||||
.iter()
|
||||
.filter(|fill| fill.symbol == "IF2501")
|
||||
.map(|fill| fill.order_id.unwrap())
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
assert_eq!(seen.borrow().len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn platform_runtime_actions_execute_generic_futures_open_and_close() {
|
||||
let mut cfg = PlatformExprStrategyConfig::generic();
|
||||
|
||||
@@ -224,6 +224,117 @@ fn decision(contract: FrozenStockPoolIntent) -> StrategyDecision {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fresh_zero_target_prevents_resuming_the_previous_unsubmitted_buy_leg() {
|
||||
use fidc_core::{ScheduleRule, ScheduleStage, ScheduleTimeRule, Strategy, StrategyContext};
|
||||
struct Probe;
|
||||
impl Strategy for Probe {
|
||||
fn name(&self) -> &str {
|
||||
"fresh-target-before-resume"
|
||||
}
|
||||
fn requires_minute_callbacks(&self) -> bool {
|
||||
false
|
||||
}
|
||||
fn schedule_rules(&self) -> Vec<ScheduleRule> {
|
||||
vec![
|
||||
ScheduleRule::daily("earlier-pool", ScheduleStage::Minute)
|
||||
.with_time_rule(ScheduleTimeRule::physical_time(9, 30)),
|
||||
]
|
||||
}
|
||||
fn on_scheduled(
|
||||
&mut self,
|
||||
ctx: &StrategyContext<'_>,
|
||||
_: &ScheduleRule,
|
||||
) -> Result<StrategyDecision, fidc_core::BacktestError> {
|
||||
if ctx.execution_date != day(5) {
|
||||
return Ok(StrategyDecision::default());
|
||||
}
|
||||
let mut old = contract(day(5), 2, false);
|
||||
old.out_of_pool_policy = "reduce_to_zero_when_sellable".into();
|
||||
old.rule.window_end = "13:30".into();
|
||||
old.rule.pricing_mode = POOL_PRICE_FORMULA_LIMIT.into();
|
||||
old.generation = "earlier-pool-at-open".into();
|
||||
Ok(decision(old))
|
||||
}
|
||||
fn on_day(
|
||||
&mut self,
|
||||
ctx: &StrategyContext<'_>,
|
||||
) -> Result<StrategyDecision, fidc_core::BacktestError> {
|
||||
if ctx.execution_date == day(2) {
|
||||
return Ok(StrategyDecision {
|
||||
order_intents: vec![OrderIntent::Shares {
|
||||
symbol: code(1),
|
||||
quantity: 100,
|
||||
reason: "original-holding".into(),
|
||||
}],
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
assert!(ctx.open_orders.is_empty());
|
||||
let mut latest = contract(day(5), 2, false);
|
||||
latest.out_of_pool_policy = "reduce_to_zero_when_sellable".into();
|
||||
latest.rule.window_end = "13:30".into();
|
||||
latest.invest_ratio_bps = 0;
|
||||
latest.generation = "fresh-zero-at-1300".into();
|
||||
Ok(decision(latest))
|
||||
}
|
||||
}
|
||||
let mut rows = data(false).snapshot_components();
|
||||
let mut quotes = Vec::new();
|
||||
for mut quote in rows.execution_quotes {
|
||||
if quote.date > day(5) {
|
||||
continue;
|
||||
}
|
||||
let mut afternoon = quote.clone();
|
||||
afternoon.timestamp = quote.date.and_hms_opt(13, 0, 0).unwrap();
|
||||
quotes.push(afternoon);
|
||||
if quote.date == day(5) && quote.symbol == code(1) {
|
||||
quote.volume_delta = 100;
|
||||
quote.amount_delta = quote.last_price * 100.;
|
||||
}
|
||||
quotes.push(quote);
|
||||
}
|
||||
rows.execution_quotes = quotes;
|
||||
let data = DataSet::from_components_with_actions_and_quotes(
|
||||
rows.instruments,
|
||||
rows.market,
|
||||
rows.factors,
|
||||
rows.candidates,
|
||||
rows.benchmarks,
|
||||
rows.corporate_actions,
|
||||
rows.execution_quotes,
|
||||
)
|
||||
.unwrap();
|
||||
let broker = broker(true)
|
||||
.with_matching_type(MatchingType::MinuteLast)
|
||||
.with_intraday_execution_start_time(chrono::NaiveTime::from_hms_opt(13, 0, 0).unwrap());
|
||||
let result = BacktestEngine::new(
|
||||
data,
|
||||
Probe,
|
||||
broker,
|
||||
BacktestConfig {
|
||||
initial_cash: 30_000.,
|
||||
benchmark_code: "000300.SH".into(),
|
||||
start_date: Some(day(2)),
|
||||
end_date: Some(day(5)),
|
||||
decision_lag_trading_days: 0,
|
||||
execution_price_field: PriceField::Last,
|
||||
},
|
||||
)
|
||||
.run()
|
||||
.unwrap();
|
||||
assert_eq!(result.fills.len(), 3, "{:?}", result.fills);
|
||||
assert!(result.fills.iter().all(|fill| fill.symbol == code(1)));
|
||||
assert_eq!(result.fills[1].side, fidc_core::OrderSide::Sell);
|
||||
assert_eq!(
|
||||
result.fills[2].execution_timestamp,
|
||||
day(5).and_hms_opt(13, 0, 0)
|
||||
);
|
||||
assert_eq!(result.fills[1].order_id, result.fills[2].order_id);
|
||||
assert_eq!(result.fills[1].quantity + result.fills[2].quantity, 100);
|
||||
assert!(result.holdings_summary.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paused_execution_day_keeps_the_prior_slot_and_never_submits_an_exit() {
|
||||
let data = data_with_suspension(1_000_000, Some(day(6)));
|
||||
|
||||
Reference in New Issue
Block a user