Add rqalpha-style scheduler primitives

This commit is contained in:
boris
2026-04-23 01:49:24 -07:00
parent 452eb3324d
commit e5fe1f0432
5 changed files with 533 additions and 3 deletions

View File

@@ -11,10 +11,21 @@ use crate::data::{DataSet, PriceField};
use crate::engine::BacktestError;
use crate::events::OrderSide;
use crate::portfolio::PortfolioState;
use crate::scheduler::ScheduleRule;
use crate::universe::{DynamicMarketCapBandSelector, SelectionContext, UniverseSelector};
pub trait Strategy {
fn name(&self) -> &str;
fn schedule_rules(&self) -> Vec<ScheduleRule> {
Vec::new()
}
fn on_scheduled(
&mut self,
_ctx: &StrategyContext<'_>,
_rule: &ScheduleRule,
) -> Result<StrategyDecision, BacktestError> {
Ok(StrategyDecision::default())
}
fn before_trading(&mut self, _ctx: &StrategyContext<'_>) -> Result<(), BacktestError> {
Ok(())
}
@@ -51,6 +62,26 @@ pub struct StrategyDecision {
pub diagnostics: Vec<String>,
}
impl StrategyDecision {
pub fn merge_from(&mut self, mut other: StrategyDecision) {
self.rebalance |= other.rebalance;
self.target_weights.append(&mut other.target_weights);
self.exit_symbols.append(&mut other.exit_symbols);
self.order_intents.append(&mut other.order_intents);
self.notes.append(&mut other.notes);
self.diagnostics.append(&mut other.diagnostics);
}
pub fn is_empty(&self) -> bool {
!self.rebalance
&& self.target_weights.is_empty()
&& self.exit_symbols.is_empty()
&& self.order_intents.is_empty()
&& self.notes.is_empty()
&& self.diagnostics.is_empty()
}
}
#[derive(Debug, Clone)]
pub enum OrderIntent {
TargetValue {