Expose strategy runtime data APIs
This commit is contained in:
@@ -49,6 +49,43 @@ impl TradingCalendar {
|
||||
.and_then(|prev| self.days.get(prev).copied())
|
||||
}
|
||||
|
||||
pub fn previous_trading_date(&self, date: NaiveDate, n: usize) -> Option<NaiveDate> {
|
||||
if n == 0 {
|
||||
return None;
|
||||
}
|
||||
let before_count = match self.days.binary_search(&date) {
|
||||
Ok(idx) => idx,
|
||||
Err(idx) => idx,
|
||||
};
|
||||
before_count
|
||||
.checked_sub(n)
|
||||
.and_then(|idx| self.days.get(idx).copied())
|
||||
}
|
||||
|
||||
pub fn next_trading_date(&self, date: NaiveDate, n: usize) -> Option<NaiveDate> {
|
||||
if n == 0 {
|
||||
return None;
|
||||
}
|
||||
let first_after = match self.days.binary_search(&date) {
|
||||
Ok(idx) => idx.saturating_add(1),
|
||||
Err(idx) => idx,
|
||||
};
|
||||
first_after
|
||||
.checked_add(n.saturating_sub(1))
|
||||
.and_then(|idx| self.days.get(idx).copied())
|
||||
}
|
||||
|
||||
pub fn trading_dates(&self, start: NaiveDate, end: NaiveDate) -> Vec<NaiveDate> {
|
||||
if start > end {
|
||||
return Vec::new();
|
||||
}
|
||||
self.days
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|date| *date >= start && *date <= end)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn trailing_days(&self, end: NaiveDate, lookback: usize) -> Vec<NaiveDate> {
|
||||
let Some(end_idx) = self.index_of(end) else {
|
||||
return Vec::new();
|
||||
|
||||
Reference in New Issue
Block a user