콘텐츠로 이동

빈 전략

최소한의 전략 구현부터 시작하세요. 아직 알파 모델이나 파이프를 추가하지 않고 런타임 훅부터 구성할 수 있습니다.

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();

이 기본 예제는 전략 정의, 모의 거래 브로커와 데이터 피드 연결, 백테스트 실행, 결과 지표 확인에 필요한 전체 과정을 최소한의 코드로 보여줍니다. add_events 호출은 보조 데이터용 타임프레임을 추가로 등록합니다. 주 전략 타임프레임은 별도로 등록하지 않아도 자동으로 실행됩니다.

일반적으로 알파 모델이나 generate_insights() 안에서 인사이트를 생성한 다음, 인사이트 파이프라인이 수량을 산정하고 위험 제어를 추가하며 주문을 제출하도록 합니다.

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);
}

전체 인사이트 모델은 인사이트를 참조하세요. 수량 산정, 검증, 주문 제출은 인사이트 파이프를 참조하세요.