Blank Strategy
Start with the smallest possible strategy surface. This gives you the runtime hooks without introducing alpha models or pipes yet.
use aq_engine::core::broker::data_feeds::yahoo::YahooFinanceDataFeed;use aq_engine::core::broker::paper_broker::PaperBroker;use aq_engine::core::broker::types::{AccountType, Asset, BarData};use aq_engine::core::broker::UnifiedBroker;use aq_engine::core::insight::Insight;use aq_engine::core::strategy::{ EventStreamType, Strategy, StrategyContext, StrategyMode, StrategyState,};use aq_engine::core::utils::timeframe::{TimeFrame, TimeFrameUnit};use chrono::{Duration, Utc};use std::collections::HashSet;
pub struct BlankStrategy;
impl Strategy for BlankStrategy { fn on_start(&mut self, ctx: &mut dyn StrategyContext) { // The main strategy timeframe is registered automatically. // This adds a feature bar stream for every symbol in the universe. ctx.add_events( EventStreamType::Bar, Some(TimeFrame::new(15, TimeFrameUnit::Minute)), ); }
fn init(&mut self, _ctx: &mut dyn StrategyContext, _asset: &Asset) {}
fn universe(&self, _ctx: &mut dyn StrategyContext) -> HashSet<String> { HashSet::from([String::from("AAPL")]) }
fn on_bar(&mut self, _ctx: &mut dyn StrategyContext, _symbol: &str, _bar: &BarData) {}
fn generate_insights(&mut self, _ctx: &mut dyn StrategyContext, _symbol: &str) {}
fn insight_pipeline(&mut self, _ctx: &mut dyn StrategyContext, _insight: &Insight) {}
fn on_teardown(&mut self, _ctx: &mut dyn StrategyContext) {}}
let execution = PaperBroker::new(AccountType::Paper, 100_000.0, 1);let data = YahooFinanceDataFeed::new();let broker = UnifiedBroker::new_backtest(execution, data);
let timeframe = TimeFrame::new(1, TimeFrameUnit::Day);let strategy = BlankStrategy;
let mut state = StrategyState::new( "blank-strategy".to_string(), "1.0.0".to_string(), strategy, broker, StrategyMode::Backtest, timeframe.clone(),);
let start = Utc::now() - Duration::days(30);let end = Utc::now();
let results = state.run_backtest(start, end, timeframe).await?;results.print_metrics();This starter block shows the smallest complete path: define a strategy, connect a paper broker and datafeed, run the backtest, and inspect the resulting metrics. The add_events call registers an extra feature timeframe; the main strategy timeframe still runs automatically.
Generating An Insight
Section titled “Generating An Insight”You usually create an insight inside an alpha model or inside generate_insights(), then let the insight pipeline size it, add risk controls, and submit it.
use aq_engine::core::broker::types::OrderSide;use aq_engine::core::insight::{types::StrategyType, Insight};
fn generate_insights(&mut self, ctx: &mut dyn StrategyContext, symbol: &str) { let mut insight = Insight::new( OrderSide::Buy, symbol.to_string(), StrategyType::Testing, ctx.timeframe().clone(), 80, None, );
insight .set_limit_price(Some(200.0)) .set_take_profit_levels(Some(vec![206.0])) .set_stop_loss(Some(197.5)) .set_period_unfilled(Some(5)) .set_period_till_tp(Some(12));
ctx.add_insight(insight);}For the full insight model, see Insights. For sizing, validation, and submission, see Insight Pipes.
We use essential cookies and storage for sign-in, account security, colour theme preferences, this notice, and required PostHog internal usage, session quality, and reliability metrics. We do not use advertising cookies.
