rusty_strategy/strategy.rs
1use std::sync::Arc;
2
3use anyhow::Result;
4use async_trait::async_trait;
5use rusty_common::collections::SmallSignalVec;
6use rusty_model::{
7 data::{
8 bar::Bar, book_snapshot::OrderBookSnapshot, market_trade::MarketTrade,
9 simd_orderbook::SharedSimdOrderBook,
10 },
11 instruments::InstrumentId,
12};
13
14use crate::signals::Signal;
15
16/// The Strategy trait defines the interface for all trading strategies.
17#[async_trait]
18pub trait Strategy: Send + Sync {
19 /// Returns the unique identifier for this strategy
20 fn id(&self) -> &str;
21
22 /// Returns a list of instruments that this strategy is interested in
23 fn instruments(&self) -> &[InstrumentId];
24
25 /// Processes a new trade event
26 ///
27 /// # Parameters
28 /// * `trade` - The trade event to process
29 ///
30 /// # Returns
31 /// A `SmallSignalVec` of signals generated by the strategy
32 async fn process_trade(&self, trade: MarketTrade) -> Result<SmallSignalVec<Signal>>;
33
34 /// Processes a new order book depth update
35 ///
36 /// # Parameters
37 /// * `depth` - The order book depth update to process
38 ///
39 /// # Returns
40 /// A `SmallSignalVec` of signals generated by the strategy
41 async fn process_depth(&self, depth: OrderBookSnapshot) -> Result<SmallSignalVec<Signal>>;
42
43 /// Processes a new bar (OHLCV) event
44 ///
45 /// # Parameters
46 /// * `bar` - The bar event to process
47 ///
48 /// # Returns
49 /// A `SmallSignalVec` of signals generated by the strategy
50 async fn process_bar(&self, bar: Bar) -> Result<SmallSignalVec<Signal>>;
51
52 /// Updates the strategy's internal state based on a shared SIMD order book
53 ///
54 /// # Parameters
55 /// * `order_book` - The shared SIMD order book to use for updating the strategy's state
56 ///
57 /// # Returns
58 /// A `SmallSignalVec` of signals generated by the strategy
59 async fn update_from_orderbook(
60 &self,
61 order_book: Arc<SharedSimdOrderBook>,
62 ) -> Result<SmallSignalVec<Signal>>;
63
64 /// Called periodically by the strategy engine to allow the strategy to perform time-based operations
65 ///
66 /// # Parameters
67 /// * `timestamp_ns` - The current timestamp in nanoseconds
68 ///
69 /// # Returns
70 /// A `SmallSignalVec` of signals generated by the strategy
71 async fn on_timer(&self, timestamp_ns: u64) -> Result<SmallSignalVec<Signal>>;
72
73 /// Initializes the strategy with required resources
74 ///
75 /// # Returns
76 /// `Ok(())` if initialization was successful, an error otherwise
77 async fn initialize(&self) -> Result<()>;
78
79 /// Shuts down the strategy and cleans up resources
80 ///
81 /// # Returns
82 /// `Ok(())` if shutdown was successful, an error otherwise
83 async fn shutdown(&self) -> Result<()>;
84}