Strategy trait and receive market events to make trading decisions.
Strategy trait
All strategies must implement theStrategy trait, which provides hooks for market events, order events, and lifecycle management:
Strategy context
TheStrategyContext provides access to market data, portfolio state, and pending orders:
Strategy configuration
Configure strategies usingStrategyConfig:
Example: Moving average crossover strategy
Built-in strategies
GlowBack includes several built-in strategies for reference:Buy and hold
Simple strategy that buys on the first bar and holds until the end
Moving average crossover
Trades based on short-term and long-term moving average crossovers
Momentum
Buys when price momentum exceeds a threshold, sells on negative momentum
Mean reversion
Trades when price deviates significantly from its mean (z-score based)
RSI
Trades based on Relative Strength Index overbought/oversold levels
Strategy actions
Strategies returnStrategyAction to communicate with the engine:
Placing orders
Market data buffer
Access historical market data using theMarketDataBuffer:
Best practices
Use Decimal for all calculations
Use Decimal for all calculations
GlowBack uses
rust_decimal::Decimal for financial calculations to avoid floating-point precision errors. Always use Decimal::from() or Decimal::from_f64_retain() when converting from f64.Check initialization state
Check initialization state
Always check
self.initialized at the start of event handlers to ensure the strategy is properly configured before processing events.Handle missing data gracefully
Handle missing data gracefully
Market data may not be available for all symbols at all times. Always check for
None when accessing market data and handle missing data appropriately.Avoid look-ahead bias
Avoid look-ahead bias
Only use data that would have been available at the time of the decision. The
StrategyContext provides data up to current_time.Test with multiple symbols and timeframes
Test with multiple symbols and timeframes
Test your strategy across different symbols, time periods, and market conditions to ensure robustness.
Next steps
Data sources
Learn how to configure data providers
Backtesting
Run backtests to evaluate your strategy
Performance analytics
Analyze strategy performance metrics
Python usage
Use GlowBack from Python