This guide will get you running your first backtest with GlowBack in minutes using Python.
Prerequisites
Before you begin, make sure you have:
Python 3.8 or higher
Rust stable toolchain (for building from source)
Your first backtest
Let’s run a simple buy-and-hold backtest using the Python API.
Import GlowBack
Start by importing the GlowBack Python module: import glowback
from datetime import datetime
Create a data manager
The data manager loads and caches market data: # Initialize data manager
manager = glowback.PyDataManager()
# Add sample data provider (includes AAPL, MSFT, BTC-USD)
manager.add_sample_provider()
The sample provider includes pre-loaded data for common symbols. You can also add CSV or API providers.
Load market data
Load historical price data for your symbol: # Create symbol
symbol = glowback.PySymbol( "AAPL" , "NASDAQ" , "equity" )
# Load daily bars for 2023
bars = manager.load_data(
symbol,
"2023-01-01T00:00:00Z" ,
"2023-12-31T23:59:59Z" ,
"day"
)
print ( f "Loaded { len (bars) } bars" )
Run a buy-and-hold backtest
The simplest way to run a backtest is using the built-in convenience function: # Run buy-and-hold strategy
result = glowback.run_buy_and_hold(
symbols = [ "AAPL" ],
start_date = "2023-01-01T00:00:00Z" ,
end_date = "2023-12-31T23:59:59Z" ,
resolution = "day" ,
initial_capital = 100000.0 , # $100k starting capital
name = "AAPL Buy & Hold"
)
Analyze the results
GlowBack automatically calculates performance metrics: # Access performance metrics
print ( f "Total Return: { result.total_return :.2%} " )
print ( f "Sharpe Ratio: { result.sharpe_ratio :.2f} " )
print ( f "Max Drawdown: { result.max_drawdown :.2%} " )
print ( f "Number of Trades: { result.num_trades } " )
# View final portfolio value
print ( f "Final Value: $ { result.final_value :,.2f} " )
print ( f "Profit/Loss: $ { result.total_pnl :,.2f} " )
Complete example
Here’s the full code in one place:
import glowback
# Initialize and configure data manager
manager = glowback.PyDataManager()
manager.add_sample_provider()
# Run buy-and-hold backtest
result = glowback.run_buy_and_hold(
symbols = [ "AAPL" ],
start_date = "2023-01-01T00:00:00Z" ,
end_date = "2023-12-31T23:59:59Z" ,
resolution = "day" ,
initial_capital = 100000.0 ,
name = "AAPL Buy & Hold"
)
# Print results
print ( f " \n Backtest Results: { result.name } " )
print ( f " { '=' * 50 } " )
print ( f "Total Return: { result.total_return :.2%} " )
print ( f "Sharpe Ratio: { result.sharpe_ratio :.2f} " )
print ( f "Sortino Ratio: { result.sortino_ratio :.2f} " )
print ( f "Max Drawdown: { result.max_drawdown :.2%} " )
print ( f "CAGR: { result.cagr :.2%} " )
print ( f " \n Final Value: $ { result.final_value :,.2f} " )
print ( f "Total Trades: { result.num_trades } " )
Using custom data sources
You can load data from CSV files or market data APIs:
CSV data
manager = glowback.PyDataManager()
# Add CSV provider (expects files in /path/to/data/)
manager.add_csv_provider( "/path/to/data" )
# CSV files should be named: SYMBOL_EXCHANGE.csv
# Example: AAPL_NASDAQ.csv
CSV format:
timestamp, open, high, low, close, volume
2023-01-01T00:00:00Z, 150.00, 152.50, 149.50, 151.00, 10000000
2023-01-02T00:00:00Z, 151.00, 153.00, 150.50, 152.50, 12000000
Alpha Vantage API
manager = glowback.PyDataManager()
# Add Alpha Vantage provider
manager.add_alpha_vantage_provider( "your_api_key_here" )
# Load data (will fetch from API if not cached)
bars = manager.load_data(
symbol,
"2023-01-01T00:00:00Z" ,
"2023-12-31T23:59:59Z" ,
"day"
)
API calls may be rate-limited. Data is automatically cached in Parquet format for faster subsequent loads.
Using the Streamlit UI
For interactive strategy development, launch the Streamlit UI:
The UI will open at http://localhost:8501 with a guided workflow:
Data Loader : Load data from CSV, APIs, or sample datasets
Strategy Editor : Edit strategy code with syntax highlighting
Backtest Runner : Configure and run backtests
Results Dashboard : View performance metrics and charts
Running with Docker
Launch all services with Docker Compose:
docker compose up --build
Services:
UI : http://localhost:8501 - Streamlit interface
API : http://localhost:8000 - FastAPI gateway
Engine : http://localhost:8081 - Rust engine service
Set GLOWBACK_API_KEY environment variable to require authentication: GLOWBACK_API_KEY = your-secret-key docker compose up
Advanced backtesting
For more control, use the backtest engine directly:
import glowback
# Create backtest engine
engine = glowback.PyBacktestEngine(
symbols = [ "AAPL" , "MSFT" ], # Multi-symbol backtest
start_date = "2023-01-01T00:00:00Z" ,
end_date = "2023-12-31T23:59:59Z" ,
resolution = "day" ,
initial_capital = 100000.0 ,
name = "Tech Portfolio"
)
# Run with a specific strategy
result = engine.run_buy_and_hold()
# Access detailed results
for trade in result.trades:
print ( f " { trade.timestamp } : { trade.side } { trade.quantity } @ $ { trade.price } " )
Multi-asset backtesting
GlowBack supports equities and crypto with appropriate market hours and fees:
import glowback
# Mix equities and crypto
aapl = glowback.PySymbol( "AAPL" , "NASDAQ" , "equity" )
btc = glowback.PySymbol( "BTC" , "USD" , "crypto" )
manager = glowback.PyDataManager()
manager.add_sample_provider()
# Equities: market hours apply, integer shares
equity_bars = manager.load_data(
aapl,
"2023-01-01T00:00:00Z" ,
"2023-12-31T23:59:59Z" ,
"day"
)
# Crypto: 24/7 trading, fractional quantities
crypto_bars = manager.load_data(
btc,
"2023-01-01T00:00:00Z" ,
"2023-12-31T23:59:59Z" ,
"day"
)
What’s next?
Core concepts Learn about event-driven backtesting architecture
Strategy development Create custom trading strategies in Rust or Python
Data management Work with data providers, storage, and caching
API reference Explore the complete API documentation