Skip to content

Code-Based Strategies

For advanced users, Quantum Trader supports custom Python strategies with full control over trading logic.

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()
MethodDescription
self.buy(size=1.0)Open long position
self.sell()Close current position
self.positionCurrent position (None if flat)
PropertyDescription
bar.openOpening price
bar.highHigh price
bar.lowLow price
bar.closeClosing price
bar.volumeTrading volume
bar.timeBar timestamp
# Moving Averages
self.sma = Indicator.SMA(period=20)
self.ema = Indicator.EMA(period=20)
# Momentum
self.rsi = Indicator.RSI(period=14)
self.macd = Indicator.MACD(fast=12, slow=26, signal=9)
# Volatility
self.bb = Indicator.BollingerBands(period=20, std=2)
self.atr = Indicator.ATR(period=14)
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()
  1. Write your strategy in a .py file
  2. Go to Strategies > Upload
  3. Select your file
  4. The strategy will be validated and saved