Strategy trait and all related types used to implement custom trading strategies in GlowBack.
Strategy trait
The core trait that all strategies must implement. Defined ingb-types/src/strategy.rs:219.
Lifecycle methods
initialize
- Store the configuration
- Load and validate parameters
- Initialize internal state
- Set up indicators or data structures
config- Strategy configuration with parameters and symbols
Ok(())on successErr(String)with error message on failure
on_market_event
event- The market event (Bar, Tick, or Quote)context- Current strategy context with portfolio and market data
Ok(Vec<StrategyAction>)with actions to take (place orders, log, etc.)Err(String)with error message on failure
on_order_event
event- The order event (Filled, PartiallyFilled, Canceled, Rejected)context- Current strategy context
Ok(Vec<StrategyAction>)with any actions to takeErr(String)with error message on failure
on_day_end
- Daily rebalancing
- End-of-day calculations
- Updating daily metrics
- Closing positions if needed
context- Current strategy context
Ok(Vec<StrategyAction>)with any actions to takeErr(String)with error message on failure
on_stop
- Closing all positions
- Finalizing metrics
- Cleanup operations
context- Current strategy context
Ok(Vec<StrategyAction>)with any final actionsErr(String)with error message on failure
Configuration methods
get_config
get_metrics
StrategyContext
Provides access to portfolio state, market data, and pending orders. Defined ingb-types/src/strategy.rs:13.
Fields
current_time- Current simulation timeportfolio- Portfolio with positions and cashmarket_data- Market data buffers for each symbolpending_orders- Orders that haven’t been filled yetstrategy_id- Unique identifier for this strategy instance
Methods
get_position
get_current_price
get_market_data
get_available_cash
get_portfolio_value
MarketDataBuffer
Stores recent market events for a symbol with a rolling window. Defined ingb-types/src/strategy.rs:54.
Methods
get_current_price
get_latest_bar
get_bars
count most recent bars in chronological order.
StrategyAction
Actions that a strategy can take. Defined ingb-types/src/strategy.rs:108.
Variants
PlaceOrder
Submit an order to the execution engine.CancelOrder
Cancel a pending order by ID.Log
Emit a log message for monitoring and debugging.SetParameter
Dynamically update a strategy parameter.StrategyConfig
Configuration and parameters for a strategy. Defined ingb-types/src/strategy.rs:170.
Methods
new
add_symbol
set_parameter
get_parameter
StrategyMetrics
Performance metrics for a strategy. Defined ingb-types/src/strategy.rs:126.
Fields
total_return- Total return as a decimal (e.g., 0.25 = 25%)annualized_return- Annualized returnvolatility- Standard deviation of returnssharpe_ratio- Risk-adjusted return metricmax_drawdown- Maximum peak-to-trough declinetotal_trades- Number of completed tradeswin_rate- Percentage of winning tradesprofit_factor- Ratio of gross profit to gross loss
LogLevel
Log levels for strategy messages. Defined ingb-types/src/strategy.rs:117.
Debug- Detailed diagnostic informationInfo- General informational messagesWarning- Warning messages for potential issuesError- Error messages for failures
See also
Creating strategies
Step-by-step guide to implementing strategies
Examples
Real-world strategy implementations