Code-Based Strategies
For advanced users, Quantum Trader supports custom Python strategies with full control over trading logic.
Strategy Structure
Section titled “Strategy Structure”from quantum_trader import Strategy, Indicator
class MyStrategy(Strategy): """Custom trading strategy."""
def __init__(self): super().__init__() self.name = "My Custom Strategy"
def initialize(self): """Set up indicators and parameters.""" self.sma_fast = Indicator.SMA(period=20) self.sma_slow = Indicator.SMA(period=50)
def on_bar(self, bar): """Called for each price bar.""" if self.sma_fast.value > self.sma_slow.value: if not self.position: self.buy() else: if self.position: self.sell()Available Methods
Section titled “Available Methods”Position Management
Section titled “Position Management”| Method | Description |
|---|---|
self.buy(size=1.0) | Open long position |
self.sell() | Close current position |
self.position | Current position (None if flat) |
Data Access
Section titled “Data Access”| Property | Description |
|---|---|
bar.open | Opening price |
bar.high | High price |
bar.low | Low price |
bar.close | Closing price |
bar.volume | Trading volume |
bar.time | Bar timestamp |
Indicators
Section titled “Indicators”# Moving Averagesself.sma = Indicator.SMA(period=20)self.ema = Indicator.EMA(period=20)
# Momentumself.rsi = Indicator.RSI(period=14)self.macd = Indicator.MACD(fast=12, slow=26, signal=9)
# Volatilityself.bb = Indicator.BollingerBands(period=20, std=2)self.atr = Indicator.ATR(period=14)Example: Dual Moving Average
Section titled “Example: Dual Moving Average”class DualMA(Strategy): def __init__(self, fast_period=10, slow_period=30): super().__init__() self.fast_period = fast_period self.slow_period = slow_period
def initialize(self): self.fast_ma = Indicator.EMA(period=self.fast_period) self.slow_ma = Indicator.EMA(period=self.slow_period)
def on_bar(self, bar): if self.fast_ma.value > self.slow_ma.value: if not self.position: self.buy() elif self.fast_ma.value < self.slow_ma.value: if self.position: self.sell()Uploading Custom Strategies
Section titled “Uploading Custom Strategies”- Write your strategy in a
.pyfile - Go to Strategies > Upload
- Select your file
- The strategy will be validated and saved
Next Steps
Section titled “Next Steps”- Optimization - Optimize strategy parameters
- API Examples - Run strategies via API