Expose open order state to strategy runtime
This commit is contained in:
@@ -51,12 +51,92 @@ pub trait Strategy {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct OpenOrderView {
|
||||
pub order_id: u64,
|
||||
pub symbol: String,
|
||||
pub side: OrderSide,
|
||||
pub requested_quantity: u32,
|
||||
pub filled_quantity: u32,
|
||||
pub remaining_quantity: u32,
|
||||
pub limit_price: f64,
|
||||
pub reason: String,
|
||||
}
|
||||
|
||||
pub struct StrategyContext<'a> {
|
||||
pub execution_date: NaiveDate,
|
||||
pub decision_date: NaiveDate,
|
||||
pub decision_index: usize,
|
||||
pub data: &'a DataSet,
|
||||
pub portfolio: &'a PortfolioState,
|
||||
pub open_orders: &'a [OpenOrderView],
|
||||
}
|
||||
|
||||
impl StrategyContext<'_> {
|
||||
pub fn has_open_orders(&self) -> bool {
|
||||
!self.open_orders.is_empty()
|
||||
}
|
||||
|
||||
pub fn open_order_count(&self) -> usize {
|
||||
self.open_orders.len()
|
||||
}
|
||||
|
||||
pub fn open_buy_order_count(&self) -> usize {
|
||||
self.open_orders
|
||||
.iter()
|
||||
.filter(|order| order.side == OrderSide::Buy)
|
||||
.count()
|
||||
}
|
||||
|
||||
pub fn open_sell_order_count(&self) -> usize {
|
||||
self.open_orders
|
||||
.iter()
|
||||
.filter(|order| order.side == OrderSide::Sell)
|
||||
.count()
|
||||
}
|
||||
|
||||
pub fn open_buy_quantity(&self) -> u32 {
|
||||
self.open_orders
|
||||
.iter()
|
||||
.filter(|order| order.side == OrderSide::Buy)
|
||||
.map(|order| order.remaining_quantity)
|
||||
.sum()
|
||||
}
|
||||
|
||||
pub fn open_sell_quantity(&self) -> u32 {
|
||||
self.open_orders
|
||||
.iter()
|
||||
.filter(|order| order.side == OrderSide::Sell)
|
||||
.map(|order| order.remaining_quantity)
|
||||
.sum()
|
||||
}
|
||||
|
||||
pub fn symbol_open_order_count(&self, symbol: &str) -> usize {
|
||||
self.open_orders
|
||||
.iter()
|
||||
.filter(|order| order.symbol == symbol)
|
||||
.count()
|
||||
}
|
||||
|
||||
pub fn symbol_open_buy_quantity(&self, symbol: &str) -> u32 {
|
||||
self.open_orders
|
||||
.iter()
|
||||
.filter(|order| order.symbol == symbol && order.side == OrderSide::Buy)
|
||||
.map(|order| order.remaining_quantity)
|
||||
.sum()
|
||||
}
|
||||
|
||||
pub fn symbol_open_sell_quantity(&self, symbol: &str) -> u32 {
|
||||
self.open_orders
|
||||
.iter()
|
||||
.filter(|order| order.symbol == symbol && order.side == OrderSide::Sell)
|
||||
.map(|order| order.remaining_quantity)
|
||||
.sum()
|
||||
}
|
||||
|
||||
pub fn available_sellable_qty(&self, symbol: &str, raw_sellable_qty: u32) -> u32 {
|
||||
raw_sellable_qty.saturating_sub(self.symbol_open_sell_quantity(symbol))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
|
||||
Reference in New Issue
Block a user