Basic Strategy Example¶
A complete, runnable moving-average crossover strategy from the optional
cracktrader-extras package.
Source¶
Canonical source:
- https://github.com/cracktrader/cracktrader-extras/blob/main/src/cracktrader_extras/examples/moving_average_cross.py
Run It¶
Notes¶
- Caching: set
cache_enabled=Trueon 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