fix: tolerate missing holding market rows
This commit is contained in:
@@ -10,8 +10,9 @@ use fidc_core::{
|
||||
FuturesCommissionType, FuturesContractSpec, FuturesDirection, FuturesOrderIntent,
|
||||
FuturesTradingParameter, FuturesValidationConfig, Instrument, IntradayExecutionQuote,
|
||||
IntradayOrderBookDepthLevel, MatchingType, OpenOrderView, OrderIntent, OrderSide, OrderStatus,
|
||||
PortfolioState, PriceField, ProcessEvent, ProcessEventBus, ProcessEventKind, ScheduleRule,
|
||||
ScheduleStage, ScheduleTimeRule, Strategy, StrategyContext, StrategyDecision,
|
||||
PlatformExprStrategy, PlatformExprStrategyConfig, PortfolioState, PriceField, ProcessEvent,
|
||||
ProcessEventBus, ProcessEventKind, ScheduleRule, ScheduleStage, ScheduleTimeRule, Strategy,
|
||||
StrategyContext, StrategyDecision,
|
||||
};
|
||||
|
||||
fn d(year: i32, month: u32, day: u32) -> NaiveDate {
|
||||
@@ -3714,3 +3715,216 @@ fn engine_exposes_current_process_context_to_strategies() {
|
||||
);
|
||||
assert!(snapshots.iter().any(|item| item == "on_day:on_day:8"));
|
||||
}
|
||||
|
||||
struct BuyMissingRowThenHoldStrategy;
|
||||
|
||||
impl Strategy for BuyMissingRowThenHoldStrategy {
|
||||
fn name(&self) -> &str {
|
||||
"buy-missing-row-then-hold"
|
||||
}
|
||||
|
||||
fn on_day(
|
||||
&mut self,
|
||||
ctx: &StrategyContext<'_>,
|
||||
) -> Result<StrategyDecision, fidc_core::BacktestError> {
|
||||
if ctx.execution_date == d(2025, 5, 26) {
|
||||
return Ok(StrategyDecision {
|
||||
rebalance: false,
|
||||
target_weights: BTreeMap::new(),
|
||||
exit_symbols: BTreeSet::new(),
|
||||
order_intents: vec![OrderIntent::Value {
|
||||
symbol: "601028.SH".to_string(),
|
||||
value: 1_000.0,
|
||||
reason: "seed_position".to_string(),
|
||||
}],
|
||||
notes: Vec::new(),
|
||||
diagnostics: Vec::new(),
|
||||
});
|
||||
}
|
||||
Ok(StrategyDecision::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn engine_carries_position_price_when_current_market_row_is_missing() {
|
||||
let date1 = d(2025, 5, 26);
|
||||
let date2 = d(2025, 5, 27);
|
||||
let data = DataSet::from_components(
|
||||
vec![
|
||||
Instrument {
|
||||
symbol: "601028.SH".to_string(),
|
||||
name: "Missing Row".to_string(),
|
||||
board: "SH".to_string(),
|
||||
round_lot: 100,
|
||||
listed_at: Some(d(2020, 1, 1)),
|
||||
delisted_at: None,
|
||||
status: "active".to_string(),
|
||||
},
|
||||
Instrument {
|
||||
symbol: "000001.SZ".to_string(),
|
||||
name: "Anchor".to_string(),
|
||||
board: "SZ".to_string(),
|
||||
round_lot: 100,
|
||||
listed_at: Some(d(2020, 1, 1)),
|
||||
delisted_at: None,
|
||||
status: "active".to_string(),
|
||||
},
|
||||
],
|
||||
vec![
|
||||
market_row(date1, "601028.SH", 10.0, 10.3),
|
||||
market_row(date2, "000001.SZ", 20.0, 20.2),
|
||||
],
|
||||
vec![
|
||||
factor_row(date1, "601028.SH", BTreeMap::new()),
|
||||
factor_row(date2, "000001.SZ", BTreeMap::new()),
|
||||
],
|
||||
vec![
|
||||
candidate_row(date1, "601028.SH"),
|
||||
candidate_row(date2, "000001.SZ"),
|
||||
],
|
||||
vec![
|
||||
benchmark_row(date1),
|
||||
BenchmarkSnapshot {
|
||||
date: date2,
|
||||
benchmark: "000300.SH".to_string(),
|
||||
open: 101.0,
|
||||
close: 101.0,
|
||||
prev_close: 100.0,
|
||||
volume: 1_100_000,
|
||||
},
|
||||
],
|
||||
)
|
||||
.expect("dataset");
|
||||
let broker = BrokerSimulator::new_with_execution_price(
|
||||
ChinaAShareCostModel::default(),
|
||||
ChinaEquityRuleHooks::default(),
|
||||
PriceField::Open,
|
||||
);
|
||||
let mut engine = BacktestEngine::new(
|
||||
data,
|
||||
BuyMissingRowThenHoldStrategy,
|
||||
broker,
|
||||
BacktestConfig {
|
||||
initial_cash: 100_000.0,
|
||||
benchmark_code: "000300.SH".to_string(),
|
||||
start_date: Some(date1),
|
||||
end_date: Some(date2),
|
||||
decision_lag_trading_days: 0,
|
||||
execution_price_field: PriceField::Open,
|
||||
},
|
||||
);
|
||||
|
||||
let result = engine
|
||||
.run()
|
||||
.expect("backtest should not fail on one missing holding row");
|
||||
assert_eq!(result.equity_curve.len(), 2);
|
||||
assert!(
|
||||
result
|
||||
.daily_holdings
|
||||
.iter()
|
||||
.any(|holding| holding.date == date2 && holding.symbol == "601028.SH")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn platform_strategy_skips_position_stop_take_when_current_market_row_is_missing() {
|
||||
let date1 = d(2025, 5, 26);
|
||||
let date2 = d(2025, 5, 27);
|
||||
let data = DataSet::from_components(
|
||||
vec![
|
||||
Instrument {
|
||||
symbol: "601028.SH".to_string(),
|
||||
name: "Missing Row".to_string(),
|
||||
board: "SH".to_string(),
|
||||
round_lot: 100,
|
||||
listed_at: Some(d(2020, 1, 1)),
|
||||
delisted_at: None,
|
||||
status: "active".to_string(),
|
||||
},
|
||||
Instrument {
|
||||
symbol: "000001.SZ".to_string(),
|
||||
name: "Signal Anchor".to_string(),
|
||||
board: "SZ".to_string(),
|
||||
round_lot: 100,
|
||||
listed_at: Some(d(2020, 1, 1)),
|
||||
delisted_at: None,
|
||||
status: "active".to_string(),
|
||||
},
|
||||
],
|
||||
vec![
|
||||
market_row(date1, "601028.SH", 10.0, 10.3),
|
||||
market_row(date1, "000001.SZ", 20.0, 20.0),
|
||||
market_row(date2, "000001.SZ", 20.0, 20.2),
|
||||
],
|
||||
vec![
|
||||
factor_row(date1, "601028.SH", BTreeMap::new()),
|
||||
factor_row(date2, "000001.SZ", BTreeMap::new()),
|
||||
],
|
||||
vec![
|
||||
candidate_row(date1, "601028.SH"),
|
||||
candidate_row(date2, "000001.SZ"),
|
||||
],
|
||||
vec![
|
||||
benchmark_row(date1),
|
||||
BenchmarkSnapshot {
|
||||
date: date2,
|
||||
benchmark: "000300.SH".to_string(),
|
||||
open: 101.0,
|
||||
close: 101.0,
|
||||
prev_close: 100.0,
|
||||
volume: 1_100_000,
|
||||
},
|
||||
],
|
||||
)
|
||||
.expect("dataset");
|
||||
let mut config = PlatformExprStrategyConfig::microcap_rotation();
|
||||
config.strategy_name = "missing-row-platform-risk".to_string();
|
||||
config.benchmark_symbol = "000300.SH".to_string();
|
||||
config.signal_symbol = "000001.SZ".to_string();
|
||||
config.refresh_rate = 1;
|
||||
config.max_positions = 1;
|
||||
config.prelude.clear();
|
||||
config.universe_exclude.clear();
|
||||
config.market_cap_field = "market_cap".to_string();
|
||||
config.market_cap_lower_expr = "0".to_string();
|
||||
config.market_cap_upper_expr = "200".to_string();
|
||||
config.selection_limit_expr = "1".to_string();
|
||||
config.stock_filter_expr = "true".to_string();
|
||||
config.stop_loss_expr = "0.93".to_string();
|
||||
config.take_profit_expr = "1.07".to_string();
|
||||
config.benchmark_short_ma_days = 1;
|
||||
config.benchmark_long_ma_days = 1;
|
||||
config.stock_short_ma_days = 1;
|
||||
config.stock_mid_ma_days = 1;
|
||||
config.stock_long_ma_days = 1;
|
||||
|
||||
let broker = BrokerSimulator::new_with_execution_price(
|
||||
ChinaAShareCostModel::default(),
|
||||
ChinaEquityRuleHooks::default(),
|
||||
PriceField::Open,
|
||||
);
|
||||
let mut engine = BacktestEngine::new(
|
||||
data,
|
||||
PlatformExprStrategy::new(config),
|
||||
broker,
|
||||
BacktestConfig {
|
||||
initial_cash: 100_000.0,
|
||||
benchmark_code: "000300.SH".to_string(),
|
||||
start_date: Some(date1),
|
||||
end_date: Some(date2),
|
||||
decision_lag_trading_days: 0,
|
||||
execution_price_field: PriceField::Open,
|
||||
},
|
||||
);
|
||||
|
||||
let result = engine
|
||||
.run()
|
||||
.expect("platform strategy should hold through a missing current market row");
|
||||
assert_eq!(result.equity_curve.len(), 2);
|
||||
assert!(
|
||||
result
|
||||
.daily_holdings
|
||||
.iter()
|
||||
.any(|holding| holding.date == date2 && holding.symbol == "601028.SH")
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user