修正next-open策略上下文日期
This commit is contained in:
@@ -156,6 +156,31 @@ pub struct StrategyContext<'a> {
|
||||
}
|
||||
|
||||
impl StrategyContext<'_> {
|
||||
pub fn signal_date(&self) -> NaiveDate {
|
||||
self.decision_date
|
||||
}
|
||||
|
||||
pub fn execution_trade_date(&self) -> NaiveDate {
|
||||
self.execution_date
|
||||
}
|
||||
|
||||
fn current_data_date(&self) -> NaiveDate {
|
||||
let active_date = self
|
||||
.active_datetime
|
||||
.map(|datetime| datetime.date())
|
||||
.unwrap_or(self.decision_date);
|
||||
if self.is_lagged_execution() && active_date > self.decision_date {
|
||||
self.decision_date
|
||||
} else {
|
||||
active_date
|
||||
}
|
||||
}
|
||||
|
||||
fn current_data_datetime(&self) -> Option<NaiveDateTime> {
|
||||
self.active_datetime
|
||||
.map(|datetime| self.current_data_date().and_time(datetime.time()))
|
||||
}
|
||||
|
||||
pub fn is_lagged_execution(&self) -> bool {
|
||||
self.execution_date != self.decision_date
|
||||
}
|
||||
@@ -544,7 +569,7 @@ impl StrategyContext<'_> {
|
||||
}
|
||||
|
||||
pub fn current_snapshot(&self, symbol: &str) -> Option<&DailyMarketSnapshot> {
|
||||
self.data.market(self.execution_date, symbol)
|
||||
self.data.market(self.current_data_date(), symbol)
|
||||
}
|
||||
|
||||
pub fn history_bars(
|
||||
@@ -556,8 +581,8 @@ impl StrategyContext<'_> {
|
||||
include_now: bool,
|
||||
) -> Vec<f64> {
|
||||
self.data.history_bars_at(
|
||||
self.execution_date,
|
||||
self.active_datetime,
|
||||
self.current_data_date(),
|
||||
self.current_data_datetime(),
|
||||
symbol,
|
||||
bar_count,
|
||||
frequency,
|
||||
@@ -573,7 +598,7 @@ impl StrategyContext<'_> {
|
||||
include_now: bool,
|
||||
) -> Vec<DailyMarketSnapshot> {
|
||||
self.data
|
||||
.history_daily_snapshots(self.execution_date, symbol, bar_count, include_now)
|
||||
.history_daily_snapshots(self.current_data_date(), symbol, bar_count, include_now)
|
||||
}
|
||||
|
||||
pub fn history_intraday_quotes(
|
||||
@@ -583,8 +608,8 @@ impl StrategyContext<'_> {
|
||||
include_now: bool,
|
||||
) -> Vec<IntradayExecutionQuote> {
|
||||
self.data.history_intraday_quotes_at(
|
||||
self.execution_date,
|
||||
self.active_datetime,
|
||||
self.current_data_date(),
|
||||
self.current_data_datetime(),
|
||||
symbol,
|
||||
bar_count,
|
||||
include_now,
|
||||
@@ -607,7 +632,8 @@ impl StrategyContext<'_> {
|
||||
}
|
||||
|
||||
pub fn active_instruments(&self, symbols: &[&str]) -> Vec<&Instrument> {
|
||||
self.data.active_instruments(self.execution_date, symbols)
|
||||
self.data
|
||||
.active_instruments(self.current_data_date(), symbols)
|
||||
}
|
||||
|
||||
pub fn all_instruments(&self) -> Vec<&Instrument> {
|
||||
@@ -628,12 +654,12 @@ impl StrategyContext<'_> {
|
||||
|
||||
pub fn is_suspended(&self, symbol: &str, count: usize) -> Vec<bool> {
|
||||
self.data
|
||||
.is_suspended_flags(self.execution_date, symbol, count)
|
||||
.is_suspended_flags(self.current_data_date(), symbol, count)
|
||||
}
|
||||
|
||||
pub fn is_st_stock(&self, symbol: &str, count: usize) -> Vec<bool> {
|
||||
self.data
|
||||
.is_st_stock_flags(self.execution_date, symbol, count)
|
||||
.is_st_stock_flags(self.current_data_date(), symbol, count)
|
||||
}
|
||||
|
||||
pub fn get_price(
|
||||
@@ -647,18 +673,20 @@ impl StrategyContext<'_> {
|
||||
}
|
||||
|
||||
pub fn get_dividend(&self, symbol: &str, start: NaiveDate) -> Vec<DividendRecord> {
|
||||
let current_data_date = self.current_data_date();
|
||||
let end = self
|
||||
.data
|
||||
.previous_trading_date(self.execution_date, 1)
|
||||
.unwrap_or(self.execution_date);
|
||||
.previous_trading_date(current_data_date, 1)
|
||||
.unwrap_or(current_data_date);
|
||||
self.data.get_dividend(symbol, start, end)
|
||||
}
|
||||
|
||||
pub fn get_split(&self, symbol: &str, start: NaiveDate) -> Vec<SplitRecord> {
|
||||
let current_data_date = self.current_data_date();
|
||||
let end = self
|
||||
.data
|
||||
.previous_trading_date(self.execution_date, 1)
|
||||
.unwrap_or(self.execution_date);
|
||||
.previous_trading_date(current_data_date, 1)
|
||||
.unwrap_or(current_data_date);
|
||||
self.data.get_split(symbol, start, end)
|
||||
}
|
||||
|
||||
@@ -693,7 +721,7 @@ impl StrategyContext<'_> {
|
||||
|
||||
pub fn get_margin_stocks(&self, margin_type: &str) -> Vec<String> {
|
||||
self.data
|
||||
.get_margin_stocks(self.execution_date, margin_type)
|
||||
.get_margin_stocks(self.current_data_date(), margin_type)
|
||||
}
|
||||
|
||||
pub fn get_securities_margin(
|
||||
@@ -787,7 +815,7 @@ impl StrategyContext<'_> {
|
||||
|
||||
pub fn get_industry(&self, symbol: &str, source: &str, level: usize) -> Option<FactorValue> {
|
||||
self.data
|
||||
.get_industry(symbol, self.execution_date, source, level)
|
||||
.get_industry(symbol, self.current_data_date(), source, level)
|
||||
}
|
||||
|
||||
pub fn get_industry_name(
|
||||
@@ -797,12 +825,12 @@ impl StrategyContext<'_> {
|
||||
level: usize,
|
||||
) -> Option<FactorTextValue> {
|
||||
self.data
|
||||
.get_industry_name(symbol, self.execution_date, source, level)
|
||||
.get_industry_name(symbol, self.current_data_date(), source, level)
|
||||
}
|
||||
|
||||
pub fn get_dominant_future(&self, underlying_symbol: &str) -> Option<String> {
|
||||
self.data
|
||||
.get_dominant_future(underlying_symbol, self.execution_date)
|
||||
.get_dominant_future(underlying_symbol, self.current_data_date())
|
||||
}
|
||||
|
||||
pub fn get_dominant_future_price(
|
||||
|
||||
Reference in New Issue
Block a user