Volatility & Breakout Strategies
ATR Channel Breakout
Tags: breakout, atr, channel, volatility
Functions: ema, atr
Description: Buys when price breaks above the ATR-based upper channel, capturing breakouts accompanied by volatility expansion.
Code:
ticker = context['ticker']
ema20 = ema(close, 20)
atr_val = atr(high, low, close, 14)
upper_channel = ema20 + atr_val * 2
lower_channel = ema20 - atr_val * 2
# Breakout above upper channel
breakout_up = close > upper_channel
# Exit below EMA
exit_cond = close < ema20
emit(breakout_up, entry(ticker, 'LONG', Weight(1.0)))
emit(exit_cond, exit_position(ticker))
Donchian Channel Breakout
Tags: breakout, donchian, high_low, trend
Functions: ts_mean
Description: Turtle-trading style strategy that buys on a 20-day high breakout and exits on a 10-day low breakdown.
Code:
ticker = context['ticker']
# 20-day high and 10-day low
high_20 = highest(high, 20)
low_10 = lowest(low, 10)
# Breakout
breakout = close > high_20.shift(1)
breakdown = close < low_10.shift(1)
emit(breakout, entry(ticker, 'LONG', Weight(1.0)))
emit(breakdown, exit_position(ticker))
Volatility Contraction Breakout
Tags: breakout, volatility, squeeze, contraction
Functions: bollinger_bands, atr
Description: Buys when price breaks above the upper Bollinger Band after a squeeze, following the Volatility Contraction Pattern (VCP).
Code:
ticker = context['ticker']
upper, middle, lower = bollinger_bands(close, 20, 2)
atr_val = atr(high, low, close, 14)
# Band width as % of price
band_width = (upper - lower) / middle
bw_avg = ts_mean(band_width, 50)
# Squeeze: width < 70% of average
squeeze = band_width < bw_avg * 0.7
# Expansion: price breaks above upper band
expansion = close > upper
# Squeeze followed by expansion
long_cond = (squeeze.shift(1)) & (expansion)
exit_cond = close < middle
emit(long_cond, entry(ticker, 'LONG', Weight(1.0)))
emit(exit_cond, exit_position(ticker))
Inside Bar Breakout
Tags: breakout, candle_pattern, inside_bar, consolidation Functions: None Description: Buys when price breaks above the high of an inside bar (a candle whose range is fully within the prior candle's range). Code:
ticker = context['ticker']
# Inside bar: today's high < yesterday's high AND today's low > yesterday's low
inside_bar = (high < high.shift(1)) & (low > low.shift(1))
# Breakout above inside bar's high
breakout = close > high.shift(1)
# Inside bar yesterday, breakout today
long_cond = (inside_bar.shift(1)) & (breakout)
exit_cond = close < low.shift(2) # Below inside bar's low
emit(long_cond, entry(ticker, 'LONG', Weight(1.0)))
emit(exit_cond, exit_position(ticker))
Range Breakout with Volume
Tags: breakout, range, volume, confirmation
Functions: ts_mean, ts_std_dev
Description: Buys when price breaks above the 20-day range high with a volume spike over 1.5x the average.
Code:
ticker = context['ticker']
# 20-day range
range_high = highest(high, 20)
range_low = lowest(low, 20)
# Volume spike (> 1.5x 20-day average)
vol_avg = ts_mean(volume, 20)
vol_spike = volume > vol_avg * 1.5
# Breakout with volume confirmation
breakout = close > range_high.shift(1)
long_cond = (breakout) & (vol_spike)
# Exit on breakdown
exit_cond = close < range_low
emit(long_cond, entry(ticker, 'LONG', Weight(1.0)))
emit(exit_cond, exit_position(ticker))
Opening Range Breakout
Tags: breakout, opening_range, intraday, momentum
Functions: ts_mean
Description: Day-trading style strategy that enters on a breakout above or breakdown below the first N candles' high/low range.
Code:
ticker = context['ticker']
# First 3 bars of range (adjust based on timeframe)
or_high = highest(high, 3).shift(1)
or_low = lowest(low, 3).shift(1)
# Breakout above OR high
long_cond = close > or_high
# Breakdown below OR low
short_cond = close < or_low
emit(long_cond, entry(ticker, 'LONG', Weight(1.0)))
emit(short_cond, exit_position(ticker))
ATR Trailing Stop Strategy
Tags: trend, atr, trailing_stop, risk_management
Functions: atr, sma
Description: Enters on an MA crossover and exits using an ATR-based trailing stop set 2x ATR below the highest close.
Code:
ticker = context['ticker']
ma20 = sma(close, 20)
atr_val = atr(high, low, close, 14)
# Entry: price crosses above MA
ma_cross_up = (close > ma20) & (close.shift(1) <= ma20)
# Trailing stop: highest close - 2*ATR
highest_close = highest(close, 20)
trailing_stop = highest_close - atr_val * 2
exit_cond = close < trailing_stop
emit(ma_cross_up, entry(ticker, 'LONG', Weight(1.0)))
emit(exit_cond, exit_position(ticker))
Perpetual Volatility Breakout Long/Short
Tags: breakout, volatility, perpetual, long_short, futures
Functions: bollinger_bands
Description: Bidirectional Bollinger Band breakout on futures: goes long above the upper band and short below the lower band.
Code:
ticker = context['ticker']
upper, middle, lower = bollinger_bands(close, 20, 2)
long_cond = close > upper
short_cond = close < lower
exit_cond = (close > lower) & (close < upper)
emit(long_cond, entry(ticker, 'LONG', Weight(1.0)))
emit(short_cond, entry(ticker, 'SHORT', Weight(1.0)))
emit(exit_cond, exit_position(ticker))
Volatility Spike Entry
Tags: volatility, spike, atr, momentum
Functions: atr, ts_mean
Description: Enters when ATR spikes above 1.5x its average alongside a bullish candle, signaling the start of a new trend.
Code:
ticker = context['ticker']
atr_val = atr(high, low, close, 14)
atr_avg = ts_mean(atr_val, 50)
# Volatility spike: ATR > 1.5x average
vol_spike = atr_val > atr_avg * 1.5
# Bullish candle
bullish = close > open
long_cond = (vol_spike) & (bullish)
exit_cond = close < sma(close, 20)
emit(long_cond, entry(ticker, 'LONG', Weight(1.0)))
emit(exit_cond, exit_position(ticker))
Keltner Channel Breakout
Tags: breakout, keltner, atr, channel
Functions: ema, atr
Description: Goes long on a Keltner Channel upper breakout and short on a lower breakdown for futures trading.
Code:
ticker = context['ticker']
ema20 = ema(close, 20)
atr_val = atr(high, low, close, 14)
kc_upper = ema20 + atr_val * 2
kc_lower = ema20 - atr_val * 2
long_cond = close > kc_upper
short_cond = close < kc_lower
exit_cond = (close < kc_upper) & (close > kc_lower)
emit(long_cond, entry(ticker, 'LONG', Weight(1.0)))
emit(short_cond, entry(ticker, 'SHORT', Weight(1.0)))
emit(exit_cond, exit_position(ticker))
Narrow Range Breakout (NR7)
Tags: breakout, narrow_range, nr7, pattern Functions: None Description: Enters on a breakout after an NR7 pattern — the narrowest daily range among the last 7 candles. Code:
ticker = context['ticker']
# Daily range
daily_range = high - low
# 7-day minimum range
min_range_7 = lowest(daily_range, 7)
# NR7: today's range is the smallest in 7 days
nr7 = daily_range == min_range_7
# Breakout next day
breakout_up = close > high.shift(1)
breakout_down = close < low.shift(1)
long_cond = (nr7.shift(1)) & (breakout_up)
short_cond = (nr7.shift(1)) & (breakout_down)
emit(long_cond, entry(ticker, 'LONG', Weight(1.0)))
emit(short_cond, entry(ticker, 'SHORT', Weight(1.0)))