Basic Strategy Example¶
A complete, runnable moving-average crossover strategy. This page includes the source directly from the examples directory so it stays in sync.
Source¶
Run It¶
Notes¶
- Caching: set
cache_enabled=True
on the store for repeated backtests - Timeframes: pass CCXT-style strings via
ccxt_timeframe
(e.g.,"1m"
,"1h"
) - Brokers: use the backtesting broker for research; switch to a live broker via the BrokerFactory for production
Add Stop Loss¶
def next(self):
if not self.position and self.crossover > 0:
# Entry with stop loss
entry_price = self.data.close[0]
stop_price = entry_price * 0.95 # 5% stop loss
self.buy_bracket(
size=size,
price=entry_price,
stopprice=stop_price
)
Next Steps¶
- Multi-Asset Strategy - Trade multiple symbols
- Live Trading - Deploy to production
- Web Dashboard - Monitor with web interface
- Performance Guide - Optimize for speed