Expose open order state to strategy runtime
This commit is contained in:
@@ -123,6 +123,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
decision_index: 1,
|
||||
data: &data,
|
||||
portfolio: &PortfolioState::new(10_000_000.0),
|
||||
open_orders: &[],
|
||||
})?;
|
||||
eprintln!("DEBUG notes={:?}", decision.notes);
|
||||
eprintln!("DEBUG diagnostics={:?}", decision.diagnostics);
|
||||
|
||||
@@ -12,7 +12,7 @@ use crate::events::{
|
||||
};
|
||||
use crate::portfolio::PortfolioState;
|
||||
use crate::rules::EquityRuleHooks;
|
||||
use crate::strategy::{OrderIntent, StrategyDecision};
|
||||
use crate::strategy::{OpenOrderView, OrderIntent, StrategyDecision};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct BrokerExecutionReport {
|
||||
@@ -174,6 +174,23 @@ impl<C, R> BrokerSimulator<C, R> {
|
||||
self.slippage_model = slippage_model;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn open_order_views(&self) -> Vec<OpenOrderView> {
|
||||
self.open_orders
|
||||
.borrow()
|
||||
.iter()
|
||||
.map(|order| OpenOrderView {
|
||||
order_id: order.order_id,
|
||||
symbol: order.symbol.clone(),
|
||||
side: order.side,
|
||||
requested_quantity: order.requested_quantity,
|
||||
filled_quantity: order.filled_quantity,
|
||||
remaining_quantity: order.remaining_quantity,
|
||||
limit_price: order.limit_price,
|
||||
reason: order.reason.clone(),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, R> BrokerSimulator<C, R>
|
||||
|
||||
@@ -232,12 +232,14 @@ where
|
||||
.map(|decision_idx| (decision_idx, execution_dates[decision_idx]));
|
||||
let (decision_index, decision_date) =
|
||||
decision_slot.unwrap_or((execution_idx, execution_date));
|
||||
let pre_open_orders = self.broker.open_order_views();
|
||||
let daily_context = StrategyContext {
|
||||
execution_date,
|
||||
decision_date,
|
||||
decision_index,
|
||||
data: &self.data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &pre_open_orders,
|
||||
};
|
||||
let schedule_rules = self.strategy.schedule_rules();
|
||||
let mut process_events = Vec::new();
|
||||
@@ -304,12 +306,14 @@ where
|
||||
&self.data,
|
||||
&auction_decision,
|
||||
)?;
|
||||
let post_auction_open_orders = self.broker.open_order_views();
|
||||
let post_auction_context = StrategyContext {
|
||||
execution_date,
|
||||
decision_date,
|
||||
decision_index,
|
||||
data: &self.data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &post_auction_open_orders,
|
||||
};
|
||||
publish_process_events(
|
||||
&mut self.strategy,
|
||||
@@ -337,6 +341,7 @@ where
|
||||
ProcessEventKind::PreOnDay,
|
||||
"on_day:pre",
|
||||
)?;
|
||||
let on_day_open_orders = self.broker.open_order_views();
|
||||
let mut decision = decision_slot
|
||||
.map(|(decision_idx, decision_date)| {
|
||||
self.strategy.on_day(&StrategyContext {
|
||||
@@ -345,6 +350,7 @@ where
|
||||
decision_index: decision_idx,
|
||||
data: &self.data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &on_day_open_orders,
|
||||
})
|
||||
})
|
||||
.transpose()?
|
||||
@@ -361,6 +367,7 @@ where
|
||||
decision_index,
|
||||
data: &self.data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &on_day_open_orders,
|
||||
},
|
||||
&mut process_events,
|
||||
&mut self.process_event_bus,
|
||||
@@ -371,6 +378,7 @@ where
|
||||
decision_index,
|
||||
data: &self.data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &on_day_open_orders,
|
||||
};
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
@@ -385,12 +393,14 @@ where
|
||||
let mut intraday_report =
|
||||
self.broker
|
||||
.execute(execution_date, &mut portfolio, &self.data, &decision)?;
|
||||
let post_intraday_open_orders = self.broker.open_order_views();
|
||||
let post_intraday_context = StrategyContext {
|
||||
execution_date,
|
||||
decision_date,
|
||||
decision_index,
|
||||
data: &self.data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &post_intraday_open_orders,
|
||||
};
|
||||
publish_process_events(
|
||||
&mut self.strategy,
|
||||
@@ -418,12 +428,14 @@ where
|
||||
|
||||
portfolio.update_prices(execution_date, &self.data, PriceField::Close)?;
|
||||
|
||||
let post_trade_open_orders = self.broker.open_order_views();
|
||||
let post_trade_context = StrategyContext {
|
||||
execution_date,
|
||||
decision_date,
|
||||
decision_index,
|
||||
data: &self.data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &post_trade_open_orders,
|
||||
};
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
@@ -457,10 +469,19 @@ where
|
||||
report.position_events.extend(close_report.position_events);
|
||||
report.account_events.extend(close_report.account_events);
|
||||
report.diagnostics.extend(close_report.diagnostics);
|
||||
let post_close_open_orders = self.broker.open_order_views();
|
||||
let post_close_context = StrategyContext {
|
||||
execution_date,
|
||||
decision_date,
|
||||
decision_index,
|
||||
data: &self.data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &post_close_open_orders,
|
||||
};
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_trade_context,
|
||||
&post_close_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
ProcessEventKind::PostAfterTrading,
|
||||
@@ -469,17 +490,17 @@ where
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_trade_context,
|
||||
&post_close_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
ProcessEventKind::PreSettlement,
|
||||
"settlement:pre",
|
||||
)?;
|
||||
self.strategy.on_settlement(&post_trade_context)?;
|
||||
self.strategy.on_settlement(&post_close_context)?;
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_trade_context,
|
||||
&post_close_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
ProcessEventKind::Settlement,
|
||||
@@ -488,7 +509,7 @@ where
|
||||
publish_phase_event(
|
||||
&mut self.strategy,
|
||||
&mut self.process_event_bus,
|
||||
&post_trade_context,
|
||||
&post_close_context,
|
||||
&mut process_events,
|
||||
execution_date,
|
||||
ProcessEventKind::PostSettlement,
|
||||
|
||||
@@ -44,7 +44,7 @@ pub use rules::{ChinaEquityRuleHooks, EquityRuleHooks, RuleCheck};
|
||||
pub use scheduler::{ScheduleFrequency, ScheduleRule, ScheduleStage, Scheduler};
|
||||
pub use strategy::{
|
||||
CnSmallCapRotationConfig, CnSmallCapRotationStrategy, JqMicroCapConfig, JqMicroCapStrategy,
|
||||
OrderIntent, Strategy, StrategyContext, StrategyDecision,
|
||||
OpenOrderView, OrderIntent, Strategy, StrategyContext, StrategyDecision,
|
||||
};
|
||||
pub use strategy_ai::{
|
||||
ManualExample, ManualFactorSource, ManualField, ManualFieldGroup, ManualFunction,
|
||||
|
||||
@@ -1112,6 +1112,7 @@ impl PlatformExprStrategy {
|
||||
|
||||
fn eval_scope(
|
||||
&self,
|
||||
ctx: &StrategyContext<'_>,
|
||||
day: &DayExpressionState,
|
||||
stock: Option<&StockExpressionState>,
|
||||
position: Option<&PositionExpressionState>,
|
||||
@@ -1148,6 +1149,12 @@ impl PlatformExprStrategy {
|
||||
scope.push("is_month_start", day.is_month_start);
|
||||
scope.push("is_month_end", day.is_month_end);
|
||||
scope.push("signal_ma30", day.signal_ma30);
|
||||
scope.push("has_open_orders", ctx.has_open_orders());
|
||||
scope.push("open_order_count", ctx.open_order_count() as i64);
|
||||
scope.push("open_buy_order_count", ctx.open_buy_order_count() as i64);
|
||||
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);
|
||||
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));
|
||||
@@ -1188,6 +1195,30 @@ impl PlatformExprStrategy {
|
||||
day_factors.insert("weekday".into(), Dynamic::from(day.weekday));
|
||||
day_factors.insert("is_month_start".into(), Dynamic::from(day.is_month_start));
|
||||
day_factors.insert("is_month_end".into(), Dynamic::from(day.is_month_end));
|
||||
day_factors.insert(
|
||||
"has_open_orders".into(),
|
||||
Dynamic::from(ctx.has_open_orders()),
|
||||
);
|
||||
day_factors.insert(
|
||||
"open_order_count".into(),
|
||||
Dynamic::from(ctx.open_order_count() as i64),
|
||||
);
|
||||
day_factors.insert(
|
||||
"open_buy_order_count".into(),
|
||||
Dynamic::from(ctx.open_buy_order_count() as i64),
|
||||
);
|
||||
day_factors.insert(
|
||||
"open_sell_order_count".into(),
|
||||
Dynamic::from(ctx.open_sell_order_count() as i64),
|
||||
);
|
||||
day_factors.insert(
|
||||
"open_buy_qty".into(),
|
||||
Dynamic::from(ctx.open_buy_quantity() as i64),
|
||||
);
|
||||
day_factors.insert(
|
||||
"open_sell_qty".into(),
|
||||
Dynamic::from(ctx.open_sell_quantity() as i64),
|
||||
);
|
||||
scope.push("day_factors", day_factors);
|
||||
if let Some(stock) = stock {
|
||||
let at_upper_limit =
|
||||
@@ -1229,6 +1260,18 @@ impl PlatformExprStrategy {
|
||||
scope.push("listed_days", stock.listed_days);
|
||||
scope.push("at_upper_limit", at_upper_limit);
|
||||
scope.push("at_lower_limit", at_lower_limit);
|
||||
scope.push(
|
||||
"symbol_open_order_count",
|
||||
ctx.symbol_open_order_count(&stock.symbol) as i64,
|
||||
);
|
||||
scope.push(
|
||||
"symbol_open_buy_qty",
|
||||
ctx.symbol_open_buy_quantity(&stock.symbol) as i64,
|
||||
);
|
||||
scope.push(
|
||||
"symbol_open_sell_qty",
|
||||
ctx.symbol_open_sell_quantity(&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);
|
||||
@@ -1304,6 +1347,18 @@ impl PlatformExprStrategy {
|
||||
factors.insert("listed_days".into(), Dynamic::from(stock.listed_days));
|
||||
factors.insert("at_upper_limit".into(), Dynamic::from(at_upper_limit));
|
||||
factors.insert("at_lower_limit".into(), Dynamic::from(at_lower_limit));
|
||||
factors.insert(
|
||||
"symbol_open_order_count".into(),
|
||||
Dynamic::from(ctx.symbol_open_order_count(&stock.symbol) as i64),
|
||||
);
|
||||
factors.insert(
|
||||
"symbol_open_buy_qty".into(),
|
||||
Dynamic::from(ctx.symbol_open_buy_quantity(&stock.symbol) as i64),
|
||||
);
|
||||
factors.insert(
|
||||
"symbol_open_sell_qty".into(),
|
||||
Dynamic::from(ctx.symbol_open_sell_quantity(&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));
|
||||
@@ -1357,6 +1412,18 @@ impl PlatformExprStrategy {
|
||||
scope.push("holding_return", position.holding_return);
|
||||
scope.push("quantity", position.quantity);
|
||||
scope.push("sellable_qty", position.sellable_qty);
|
||||
let available_sellable_qty = stock
|
||||
.map(|stock| {
|
||||
ctx.available_sellable_qty(&stock.symbol, position.sellable_qty as u32)
|
||||
})
|
||||
.unwrap_or(position.sellable_qty.max(0) as u32);
|
||||
scope.push("available_sellable_qty", available_sellable_qty as i64);
|
||||
scope.push(
|
||||
"reserved_open_sell_qty",
|
||||
stock
|
||||
.map(|stock| ctx.symbol_open_sell_quantity(&stock.symbol) as i64)
|
||||
.unwrap_or(0),
|
||||
);
|
||||
scope.push("profit_pct", position.holding_return * 100.0);
|
||||
}
|
||||
scope
|
||||
@@ -1370,7 +1437,7 @@ impl PlatformExprStrategy {
|
||||
stock: Option<&StockExpressionState>,
|
||||
position: Option<&PositionExpressionState>,
|
||||
) -> Result<Dynamic, BacktestError> {
|
||||
let mut scope = self.eval_scope(day, stock, position);
|
||||
let mut scope = self.eval_scope(ctx, day, stock, position);
|
||||
let normalized_expr = Self::normalize_expr(expr);
|
||||
let expanded_expr = self.expand_runtime_helpers(ctx, day, stock, &normalized_expr)?;
|
||||
let prelude_declared_identifiers = Self::declared_prelude_identifiers(&self.config.prelude);
|
||||
@@ -2885,7 +2952,7 @@ mod tests {
|
||||
};
|
||||
use crate::{
|
||||
BenchmarkSnapshot, CandidateEligibility, DailyFactorSnapshot, DailyMarketSnapshot, DataSet,
|
||||
Instrument, PortfolioState, Strategy, StrategyContext, TradingCalendar,
|
||||
Instrument, OpenOrderView, PortfolioState, Strategy, StrategyContext, TradingCalendar,
|
||||
};
|
||||
|
||||
fn d(year: i32, month: u32, day: u32) -> NaiveDate {
|
||||
@@ -2999,6 +3066,7 @@ mod tests {
|
||||
decision_index: 0,
|
||||
data: &data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &[],
|
||||
};
|
||||
let mut cfg = PlatformExprStrategyConfig::microcap_rotation();
|
||||
cfg.signal_symbol = "000001.SZ".to_string();
|
||||
@@ -3128,6 +3196,7 @@ mod tests {
|
||||
decision_index: 0,
|
||||
data: &data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &[],
|
||||
};
|
||||
let mut cfg = PlatformExprStrategyConfig::microcap_rotation();
|
||||
cfg.signal_symbol = "000001.SZ".to_string();
|
||||
@@ -3238,6 +3307,7 @@ mod tests {
|
||||
decision_index: 0,
|
||||
data: &data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &[],
|
||||
};
|
||||
let mut cfg = PlatformExprStrategyConfig::microcap_rotation();
|
||||
cfg.signal_symbol = "000001.SZ".to_string();
|
||||
@@ -3264,4 +3334,111 @@ mod tests {
|
||||
let auction_decision = strategy.open_auction(&ctx).expect("auction decision");
|
||||
assert!(auction_decision.order_intents.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn platform_strategy_exposes_open_order_runtime_fields() {
|
||||
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: 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::Order {
|
||||
kind: PlatformExplicitOrderKind::Value,
|
||||
symbol: "000001.SZ".to_string(),
|
||||
amount_expr: "cash * 0.1".to_string(),
|
||||
limit_price_expr: None,
|
||||
when_expr: Some(
|
||||
"has_open_orders && open_order_count == 1 && open_sell_qty == 200 && symbol_open_sell_qty == 200 && symbol_open_order_count == 1".to_string(),
|
||||
),
|
||||
reason: "open_order_aware_entry".to_string(),
|
||||
}];
|
||||
let mut strategy = PlatformExprStrategy::new(cfg);
|
||||
|
||||
let decision = strategy.on_day(&ctx).expect("platform decision");
|
||||
assert_eq!(decision.order_intents.len(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -138,6 +138,8 @@ pub fn built_in_strategy_manual() -> StrategyAiManual {
|
||||
ManualField { name: "benchmark_ma5/benchmark_ma10/benchmark_ma20/benchmark_ma30".to_string(), field_type: "float".to_string(), detail: "基准指数滚动均线。".to_string() },
|
||||
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: "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() },
|
||||
],
|
||||
@@ -154,6 +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: "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() },
|
||||
@@ -168,6 +171,7 @@ pub fn built_in_strategy_manual() -> StrategyAiManual {
|
||||
ManualField { name: "holding_return".to_string(), field_type: "float".to_string(), detail: "持仓收益率,小数。".to_string() },
|
||||
ManualField { name: "profit_pct".to_string(), field_type: "float".to_string(), detail: "持仓收益率,百分比。".to_string() },
|
||||
ManualField { name: "quantity/sellable_qty".to_string(), field_type: "int".to_string(), detail: "持仓数量与可卖数量。".to_string() },
|
||||
ManualField { name: "available_sellable_qty/reserved_open_sell_qty".to_string(), field_type: "int".to_string(), detail: "扣掉未成交卖单占用后的可卖数量,以及当前 symbol 已占用的卖出挂单数量。".to_string() },
|
||||
],
|
||||
},
|
||||
],
|
||||
|
||||
@@ -25,6 +25,7 @@ fn strategy_emits_target_weights_and_diagnostics() {
|
||||
decision_index: 0,
|
||||
data: &data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &[],
|
||||
})
|
||||
.expect("decision");
|
||||
|
||||
@@ -62,6 +63,7 @@ fn jq_strategy_emits_same_day_decision() {
|
||||
decision_index: 0,
|
||||
data: &data,
|
||||
portfolio: &portfolio,
|
||||
open_orders: &[],
|
||||
})
|
||||
.expect("jq decision");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user