Expose latest open order ids to platform runtime
This commit is contained in:
@@ -1155,6 +1155,7 @@ impl PlatformExprStrategy {
|
||||
scope.push("open_sell_order_count", ctx.open_sell_order_count() as i64);
|
||||
scope.push("open_buy_qty", ctx.open_buy_quantity() as i64);
|
||||
scope.push("open_sell_qty", ctx.open_sell_quantity() as i64);
|
||||
scope.push("latest_open_order_id", ctx.latest_open_order_id() as i64);
|
||||
let mut day_factors = Map::new();
|
||||
day_factors.insert("signal_open".into(), Dynamic::from(day.signal_open));
|
||||
day_factors.insert("signal_close".into(), Dynamic::from(day.signal_close));
|
||||
@@ -1219,6 +1220,10 @@ impl PlatformExprStrategy {
|
||||
"open_sell_qty".into(),
|
||||
Dynamic::from(ctx.open_sell_quantity() as i64),
|
||||
);
|
||||
day_factors.insert(
|
||||
"latest_open_order_id".into(),
|
||||
Dynamic::from(ctx.latest_open_order_id() as i64),
|
||||
);
|
||||
scope.push("day_factors", day_factors);
|
||||
if let Some(stock) = stock {
|
||||
let at_upper_limit =
|
||||
@@ -1272,6 +1277,10 @@ impl PlatformExprStrategy {
|
||||
"symbol_open_sell_qty",
|
||||
ctx.symbol_open_sell_quantity(&stock.symbol) as i64,
|
||||
);
|
||||
scope.push(
|
||||
"latest_symbol_open_order_id",
|
||||
ctx.latest_symbol_open_order_id(&stock.symbol) as i64,
|
||||
);
|
||||
scope.push("stock_ma_short", stock.stock_ma_short);
|
||||
scope.push("stock_ma_mid", stock.stock_ma_mid);
|
||||
scope.push("stock_ma_long", stock.stock_ma_long);
|
||||
@@ -1359,6 +1368,10 @@ impl PlatformExprStrategy {
|
||||
"symbol_open_sell_qty".into(),
|
||||
Dynamic::from(ctx.symbol_open_sell_quantity(&stock.symbol) as i64),
|
||||
);
|
||||
factors.insert(
|
||||
"latest_symbol_open_order_id".into(),
|
||||
Dynamic::from(ctx.latest_symbol_open_order_id(&stock.symbol) as i64),
|
||||
);
|
||||
factors.insert("stock_ma5".into(), Dynamic::from(stock.stock_ma5));
|
||||
factors.insert("stock_ma10".into(), Dynamic::from(stock.stock_ma10));
|
||||
factors.insert("stock_ma20".into(), Dynamic::from(stock.stock_ma20));
|
||||
@@ -3441,4 +3454,127 @@ mod tests {
|
||||
let decision = strategy.on_day(&ctx).expect("platform decision");
|
||||
assert_eq!(decision.order_intents.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn platform_strategy_can_cancel_latest_open_order_by_runtime_id() {
|
||||
let date = d(2025, 2, 3);
|
||||
let data = DataSet::from_components(
|
||||
vec![Instrument {
|
||||
symbol: "000001.SZ".to_string(),
|
||||
name: "Ping An Bank".to_string(),
|
||||
board: "SZSE".to_string(),
|
||||
round_lot: 100,
|
||||
listed_at: Some(d(2010, 1, 1)),
|
||||
delisted_at: None,
|
||||
status: "active".to_string(),
|
||||
}],
|
||||
vec![DailyMarketSnapshot {
|
||||
date,
|
||||
symbol: "000001.SZ".to_string(),
|
||||
timestamp: Some("10:18:00".to_string()),
|
||||
day_open: 10.0,
|
||||
open: 10.0,
|
||||
high: 10.2,
|
||||
low: 9.9,
|
||||
close: 10.1,
|
||||
last_price: 10.05,
|
||||
bid1: 10.04,
|
||||
ask1: 10.05,
|
||||
prev_close: 9.95,
|
||||
volume: 1_000_000,
|
||||
tick_volume: 5_000,
|
||||
bid1_volume: 1_000,
|
||||
ask1_volume: 1_000,
|
||||
trading_phase: Some("continuous".to_string()),
|
||||
paused: false,
|
||||
upper_limit: 10.94,
|
||||
lower_limit: 8.96,
|
||||
price_tick: 0.01,
|
||||
}],
|
||||
vec![DailyFactorSnapshot {
|
||||
date,
|
||||
symbol: "000001.SZ".to_string(),
|
||||
market_cap_bn: 12.0,
|
||||
free_float_cap_bn: 10.0,
|
||||
pe_ttm: 8.0,
|
||||
turnover_ratio: Some(22.0),
|
||||
effective_turnover_ratio: Some(18.0),
|
||||
extra_factors: BTreeMap::new(),
|
||||
}],
|
||||
vec![CandidateEligibility {
|
||||
date,
|
||||
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,
|
||||
benchmark: "000852.SH".to_string(),
|
||||
open: 1000.0,
|
||||
close: 1002.0,
|
||||
prev_close: 998.0,
|
||||
volume: 1_000_000,
|
||||
}],
|
||||
)
|
||||
.expect("dataset");
|
||||
let portfolio = PortfolioState::new(1_000_000.0);
|
||||
let open_orders = vec![
|
||||
OpenOrderView {
|
||||
order_id: 41,
|
||||
symbol: "000001.SZ".to_string(),
|
||||
side: crate::OrderSide::Buy,
|
||||
requested_quantity: 100,
|
||||
filled_quantity: 0,
|
||||
remaining_quantity: 100,
|
||||
limit_price: 9.9,
|
||||
reason: "pending_limit_buy".to_string(),
|
||||
},
|
||||
OpenOrderView {
|
||||
order_id: 42,
|
||||
symbol: "000001.SZ".to_string(),
|
||||
side: crate::OrderSide::Sell,
|
||||
requested_quantity: 300,
|
||||
filled_quantity: 100,
|
||||
remaining_quantity: 200,
|
||||
limit_price: 10.2,
|
||||
reason: "pending_limit_sell".to_string(),
|
||||
},
|
||||
];
|
||||
let ctx = StrategyContext {
|
||||
execution_date: date,
|
||||
decision_date: date,
|
||||
decision_index: 0,
|
||||
data: &data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &open_orders,
|
||||
};
|
||||
let mut cfg = PlatformExprStrategyConfig::microcap_rotation();
|
||||
cfg.signal_symbol = "000001.SZ".to_string();
|
||||
cfg.rotation_enabled = false;
|
||||
cfg.benchmark_short_ma_days = 1;
|
||||
cfg.benchmark_long_ma_days = 1;
|
||||
cfg.explicit_actions = vec![PlatformTradeAction::Cancel {
|
||||
kind: PlatformExplicitCancelKind::Order,
|
||||
symbol: Some("000001.SZ".to_string()),
|
||||
order_id_expr: Some("latest_open_order_id".to_string()),
|
||||
when_expr: Some("latest_symbol_open_order_id == 42".to_string()),
|
||||
reason: "cancel_latest_open_order".to_string(),
|
||||
}];
|
||||
let mut strategy = PlatformExprStrategy::new(cfg);
|
||||
|
||||
let decision = strategy.on_day(&ctx).expect("platform decision");
|
||||
assert_eq!(decision.order_intents.len(), 1);
|
||||
match &decision.order_intents[0] {
|
||||
crate::strategy::OrderIntent::CancelOrder { order_id, reason } => {
|
||||
assert_eq!(*order_id, 42);
|
||||
assert_eq!(reason, "cancel_latest_open_order");
|
||||
}
|
||||
other => panic!("unexpected cancel intent: {other:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,6 +134,23 @@ impl StrategyContext<'_> {
|
||||
.sum()
|
||||
}
|
||||
|
||||
pub fn latest_open_order_id(&self) -> u64 {
|
||||
self.open_orders
|
||||
.iter()
|
||||
.map(|order| order.order_id)
|
||||
.max()
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
pub fn latest_symbol_open_order_id(&self, symbol: &str) -> u64 {
|
||||
self.open_orders
|
||||
.iter()
|
||||
.filter(|order| order.symbol == symbol)
|
||||
.map(|order| order.order_id)
|
||||
.max()
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
pub fn available_sellable_qty(&self, symbol: &str, raw_sellable_qty: u32) -> u32 {
|
||||
raw_sellable_qty.saturating_sub(self.symbol_open_sell_quantity(symbol))
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ pub fn built_in_strategy_manual() -> StrategyAiManual {
|
||||
ManualField { name: "cash/available_cash/market_value/total_equity".to_string(), field_type: "float".to_string(), detail: "账户资金与总资产。".to_string() },
|
||||
ManualField { name: "position_count/max_positions/refresh_rate".to_string(), field_type: "int".to_string(), detail: "仓位计数与调仓周期。".to_string() },
|
||||
ManualField { name: "has_open_orders/open_order_count/open_buy_order_count/open_sell_order_count".to_string(), field_type: "bool/int".to_string(), detail: "当前阶段挂单簿摘要。".to_string() },
|
||||
ManualField { name: "open_buy_qty/open_sell_qty".to_string(), field_type: "int".to_string(), detail: "当前阶段未成交买卖挂单的剩余数量汇总。".to_string() },
|
||||
ManualField { name: "open_buy_qty/open_sell_qty/latest_open_order_id".to_string(), field_type: "int".to_string(), detail: "当前阶段未成交买卖挂单的剩余数量汇总,以及最近一笔挂单 id。".to_string() },
|
||||
ManualField { name: "year/month/quarter/day_of_month/day_of_year/week_of_year/weekday".to_string(), field_type: "int".to_string(), detail: "日期维度字段。".to_string() },
|
||||
ManualField { name: "is_month_start/is_month_end".to_string(), field_type: "bool".to_string(), detail: "月初/月末标记。".to_string() },
|
||||
],
|
||||
@@ -156,7 +156,7 @@ pub fn built_in_strategy_manual() -> StrategyAiManual {
|
||||
ManualField { name: "paused/is_st/is_kcb/is_one_yuan/is_new_listing".to_string(), field_type: "bool".to_string(), detail: "可交易性与板块标志。".to_string() },
|
||||
ManualField { name: "allow_buy/allow_sell/at_upper_limit/at_lower_limit".to_string(), field_type: "bool".to_string(), detail: "盘中买卖与涨跌停状态。".to_string() },
|
||||
ManualField { name: "touched_upper_limit/touched_lower_limit/hit_upper_limit/hit_lower_limit".to_string(), field_type: "bool".to_string(), detail: "当日 tick 曾经触达涨跌停。".to_string() },
|
||||
ManualField { name: "symbol_open_order_count/symbol_open_buy_qty/symbol_open_sell_qty".to_string(), field_type: "int".to_string(), detail: "当前证券在挂单簿中的未成交挂单摘要。".to_string() },
|
||||
ManualField { name: "symbol_open_order_count/symbol_open_buy_qty/symbol_open_sell_qty/latest_symbol_open_order_id".to_string(), field_type: "int".to_string(), detail: "当前证券在挂单簿中的未成交挂单摘要和最近挂单 id。".to_string() },
|
||||
ManualField { name: "stock_ma5/stock_ma10/stock_ma20/stock_ma30".to_string(), field_type: "float".to_string(), detail: "个股价格均线内建别名。只内建这几个窗口;15 日、45 日等任意窗口请改用 sma(\"close\", n)。".to_string() },
|
||||
ManualField { name: "stock_volume_ma5/stock_volume_ma10/stock_volume_ma20/stock_volume_ma60".to_string(), field_type: "float".to_string(), detail: "个股成交量均线内建别名。只内建这几个窗口;任意窗口请改用 rolling_mean(\"volume\", n)。".to_string() },
|
||||
ManualField { name: "listed_days".to_string(), field_type: "int".to_string(), detail: "上市天数。".to_string() },
|
||||
|
||||
Reference in New Issue
Block a user