Local Development
Tools and workflows for developing and testing trading bots locally.
Hyperparameter Tuning
Define Parameter Grid
class MyBot(Bot):
param_grid = {
"rsi_buy": [65, 70, 75],
"rsi_sell": [25, 30, 35],
"adx_threshold": [15, 20, 25],
}
Run Optimization
bot = MyBot()
# Full workflow: optimize + backtest
bot.local_development()
# Or just optimize
results = bot.local_optimize()
print(f"Best params: {results['best_params']}")
# Or just backtest
results = bot.local_backtest()
print(f"Sharpe: {results['sharpe_ratio']:.2f}")
Backtesting
Requirements:
local_backtest()works fordecisionFunction-based bots (single-asset and multi-asset withtickers=[...]). Bots that overridemakeOneIteration()to call external APIs, AI models, or portfolio-weight optimizers are not automatically backtestable — refactor todecisionFunctionif historical replay is needed.
self.datais available insidedecisionFunction: the base class sets it to the historical slice up to the current bar before each call, so you can access full history (e.g.self.data.tail(50)) without overridingmakeOneIteration().Bots that only need yfinance data should always use
decisionFunction—makeOneIterationoverride is only warranted for external APIs or portfolio-weight rebalancing strategies.
Simple Backtest
from tradingbot.utils.backtest import backtest_bot
bot = MyBot()
results = backtest_bot(bot, initial_capital=10000.0)
print(f"Yearly Return: {results['yearly_return']:.2%}")
print(f"Sharpe Ratio: {results['sharpe_ratio']:.2f}")
print(f"Max Drawdown: {results['maxdrawdown']:.2%}")
Backtest with Pre-fetched Data
# Fetch data once
data = bot.getYFDataWithTA(saveToDB=True, interval="1m", period="7d")
# Reuse for multiple backtests
results1 = backtest_bot(bot, data=data)
results2 = backtest_bot(another_bot, data=data)
Data Caching
For efficient development:
# First run: fetch and save to DB
data = bot.getYFDataWithTA(saveToDB=True, interval="1m", period="1d")
# Subsequent runs: reuse DB data (much faster)
data = bot.getYFDataWithTA(saveToDB=True, interval="1m", period="1d")
Development Workflow
- Create bot with
decisionFunction() - Test locally with
bot.run() - Optimize parameters with
bot.local_development() - Copy best params into
__init__()defaults - Deploy via Helm
Debugging
Check Database
from tradingbot.utils.db import get_db_session, Bot, Trade
with get_db_session() as session:
# Check bot state
bot = session.query(Bot).filter_by(name="MyBot").first()
print(bot.portfolio)
# Check trades
trades = session.query(Trade).filter_by(bot_name="MyBot").all()
for trade in trades:
print(f"{trade.symbol}: {trade.quantity} @ {trade.price}")
View Logs
from tradingbot.utils.db import RunLog
with get_db_session() as session:
logs = session.query(RunLog).filter_by(bot_name="MyBot").all()
for log in logs[-10:]: # Last 10 runs
print(f"{log.start_time}: {log.success} - {log.result}")
QuantStats Report Generation
Backtest results are automatically analyzed and visualized via QuantStats — a professional portfolio analysis library. When you backtest, two HTML reports are generated and uploaded to Google Cloud Storage.
📊 View Example Report - See what a generated report looks like
Automatic Report Generation
When local_backtest() or local_development() completes:
- QuantStats generates a rich HTML report with:
- Cumulative returns chart
- Drawdown analysis
- Monthly/yearly returns heatmap
- Risk metrics (Sharpe, Sortino, max drawdown)
- Win rate and trade statistics
-
Comparison vs. buy-and-hold benchmark
-
Two report variants are created:
sharpewinner/report.html— Optimized for Sharpe ratio-
yearlyreturnwinner/report.html— Optimized for yearly return -
Reports are uploaded to GCS (if credentials are configured):
- Path:
gs://tradingbotrunresults/{BotName}/{metric}/report.html - Example:
gs://tradingbotrunresults/AdaptiveMeanReversionBot/sharpewinner/report.html
Configuring GCS Upload
Add to .env:
GCS_ACCESS_KEY_ID=GOOG1E42PG... # Your GCS HMAC access key
GCS_SECRET_ACCESS_KEY=DEhAhzG6Ok... # Your GCS HMAC secret
GCS_BUCKET_NAME=tradingbotrunresults # Cloud Storage bucket name
To get HMAC credentials: 1. Go to Google Cloud Console → Service Accounts 2. Create or select a service account 3. Create HMAC key (Keys tab → "Create key" → HMAC) 4. Copy Access Key ID and Secret from the JSON
Without GCS configured, backtests still run and generate local insights—reports are simply skipped. This is safe for local development.
What to Look For in Reports
- Cumulative Returns: Should show consistent growth or stay positive during drawdowns
- Monthly Heatmap: Look for consistency across months (avoid concentrated returns)
- Sharpe Ratio: Higher is better; >1.0 is good, >2.0 is excellent
- Max Drawdown: How much the strategy lost at its worst point (smaller is better)
- Win Rate: Percentage of profitable trades
- Comparison vs Benchmark: Did your bot beat buy-and-hold?
Next Steps
- Hyperparameter Tuning API - Complete API docs
- Backtesting API - Backtest details