Backtest an EMA Crossover Long & Short Strategy
Run a bidirectional fast/slow EMA crossover strategy with long and short legs on spot or perpetual candles, including funding and reversal fills.
Before you start
A bidirectional EMA 20/50 crossover switches between long and short exposure at each moving-average cross and guards both legs with a 2% stop-loss. Testing long/short reversal rules in QTrader lets you inspect reversal commissions, perpetual isolated margin and funding payments, and drawdown across bull and bear cycles.
Follow the workflow
- Open the bundled Python EMA 20/50 long/short template below and inspect how
enter_long()andenter_short()reverse open exposure on opposing crosses. - Start on a 4h spot or perpetual contract at leverage 1, verifying maker/taker fees and funding.
- Check every reversal trade and 2% stop-loss fill in the trade list to confirm how
strategy(stop_loss_percent=2)behaves on opening gaps. - Test separate evaluation periods before assuming a fast/slow EMA pair stays robust across changing volatility regimes.
Worked example
QTrader's bundled Python template combines fast/slow exponential averages with symmetric long and short entries and a 2% protective stop.
fast_length = input.int(20, "Fast EMA", minval=2)
slow_length = input.int(50, "Slow EMA", minval=2)
strategy(stop_loss_percent=2)
def enter_long():
return indicator.crossover(indicator.ema(close, fast_length), indicator.ema(close, slow_length))
def enter_short():
return indicator.crossunder(indicator.ema(close, fast_length), indicator.ema(close, slow_length))
Limits and interpretation
Always-in-the-market reversal strategies accumulate double turnover costs and whipsaw losses in sideways consolidations. On leveraged perpetuals, funding payments and liquidation thresholds also affect net P&L; read futures backtesting rules before testing higher leverage.