Expose open order runtime fields

This commit is contained in:
boris
2026-04-23 19:47:56 -07:00
parent f4030f2607
commit c12a883d28
6 changed files with 132 additions and 5 deletions

View File

@@ -9,7 +9,7 @@ use chrono::{Datelike, Duration, NaiveDate, NaiveDateTime, NaiveTime};
use crate::cost::ChinaAShareCostModel;
use crate::data::{DailyMarketSnapshot, DataSet, IntradayExecutionQuote, PriceBar, PriceField};
use crate::engine::BacktestError;
use crate::events::{OrderSide, ProcessEvent};
use crate::events::{OrderSide, OrderStatus, ProcessEvent};
use crate::instrument::Instrument;
use crate::portfolio::PortfolioState;
use crate::scheduler::ScheduleRule;
@@ -72,6 +72,10 @@ pub struct OpenOrderView {
pub requested_quantity: u32,
pub filled_quantity: u32,
pub remaining_quantity: u32,
pub unfilled_quantity: u32,
pub status: OrderStatus,
pub avg_price: f64,
pub transaction_cost: f64,
pub limit_price: f64,
pub reason: String,
}
@@ -168,6 +172,22 @@ impl StrategyContext<'_> {
.unwrap_or(0)
}
pub fn latest_open_order_status(&self) -> &'static str {
self.open_orders
.iter()
.max_by_key(|order| order.order_id)
.map(|order| order.status.as_str())
.unwrap_or("")
}
pub fn latest_open_order_unfilled_quantity(&self) -> u32 {
self.open_orders
.iter()
.max_by_key(|order| order.order_id)
.map(|order| order.unfilled_quantity)
.unwrap_or(0)
}
pub fn latest_symbol_open_order_id(&self, symbol: &str) -> u64 {
self.open_orders
.iter()
@@ -177,6 +197,24 @@ impl StrategyContext<'_> {
.unwrap_or(0)
}
pub fn latest_symbol_open_order_status(&self, symbol: &str) -> &'static str {
self.open_orders
.iter()
.filter(|order| order.symbol == symbol)
.max_by_key(|order| order.order_id)
.map(|order| order.status.as_str())
.unwrap_or("")
}
pub fn latest_symbol_open_order_unfilled_quantity(&self, symbol: &str) -> u32 {
self.open_orders
.iter()
.filter(|order| order.symbol == symbol)
.max_by_key(|order| order.order_id)
.map(|order| order.unfilled_quantity)
.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))
}