Add instrument history helpers

This commit is contained in:
boris
2026-04-23 19:43:36 -07:00
parent ca49b6dbb3
commit f4030f2607
6 changed files with 60 additions and 16 deletions

View File

@@ -436,8 +436,11 @@ impl Strategy for DataApiProbeStrategy {
let tick_price_count = ctx
.get_price("000001.SZ", d(2025, 1, 3), ctx.execution_date, "tick")
.len();
let instrument_history_count =
ctx.instruments_history(&["000001.SZ", "000002.SZ"]).len();
let active_instrument_count = ctx.active_instruments(&["000001.SZ", "000002.SZ"]).len();
self.snapshots.borrow_mut().push(format!(
"daily={daily_close};previous={previous_close};tick={tick_last};previous_tick={previous_tick_last};current={current_close};instrument={instrument_name};all={};range={trading_date_count};prev={prev_date};next={next_date};suspended={suspended};st={st_flags};price_daily={daily_price_count};price_tick={tick_price_count}",
"daily={daily_close};previous={previous_close};tick={tick_last};previous_tick={previous_tick_last};current={current_close};instrument={instrument_name};all={};history={instrument_history_count};active={active_instrument_count};range={trading_date_count};prev={prev_date};next={next_date};suspended={suspended};st={st_flags};price_daily={daily_price_count};price_tick={tick_price_count}",
ctx.all_instruments().len()
));
}
@@ -897,15 +900,26 @@ fn strategy_context_exposes_rqalpha_style_data_helpers() {
let date1 = d(2025, 1, 2);
let date2 = d(2025, 1, 3);
let date3 = d(2025, 1, 6);
let instrument = 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(),
};
let instruments = vec![
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(),
},
Instrument {
symbol: "000002.SZ".to_string(),
name: "Historical".to_string(),
board: "SZ".to_string(),
round_lot: 100,
listed_at: Some(d(2020, 1, 1)),
delisted_at: Some(date2),
status: "active".to_string(),
},
];
let market = [
(date1, 10.0, 10.0, 10.0, 100_000),
(date2, 10.1, 10.1, 10.0, 110_000),
@@ -1026,7 +1040,7 @@ fn strategy_context_exposes_rqalpha_style_data_helpers() {
},
];
let data = DataSet::from_components_with_actions_and_quotes(
vec![instrument],
instruments,
market,
factors,
candidates,
@@ -1065,7 +1079,7 @@ fn strategy_context_exposes_rqalpha_style_data_helpers() {
assert_eq!(
snapshots.borrow().as_slice(),
[
"daily=10.10,10.20;previous=10.00,10.10;tick=10.15,10.25;previous_tick=10.15;current=10.20;instrument=Anchor;all=1;range=3;prev=2025-01-03;next=2025-01-06;suspended=0,1,0;st=0,1,0;price_daily=2;price_tick=3"
"daily=10.10,10.20;previous=10.00,10.10;tick=10.15,10.25;previous_tick=10.15;current=10.20;instrument=Anchor;all=2;history=2;active=1;range=3;prev=2025-01-03;next=2025-01-06;suspended=0,1,0;st=0,1,0;price_daily=2;price_tick=3"
]
);
}