Algorithmic Trading & Indicators Beginner

Calculate Chuck LeBeau's Chandelier Exit Trailing Stop in Python Pandas

Compute the Chandelier Exit volatility stop-loss indicator in Python Pandas by hanging a 3x ATR stop from the 22-period rolling Highest High.

#pandas#numpy

Problem Statement

Unlike standard ATR stops calculated from the current Close, the Chandelier Exit hangs the stop-loss strictly from the 22-bar Highest High (for longs) or Lowest Low (for shorts), preventing premature exits during minor pullbacks.

Formula / Core Logic

Chandelier_Long_Exit = Highest_High(22) - 3.0 * ATR(22) Chandelier_Short_Exit = Lowest_Low(22) + 3.0 * ATR(22)

Installation

pip install pandas numpy
chandelier-exit-chuck-lebeau-volatility-stop-python.py
import pandas as pd
import numpy as np

def calculate_chandelier_exit(df, period=22, mult=3.0):
    df = df.copy()
    tr = pd.concat([
        df['High'] - df['Low'],
        (df['High'] - df['Close'].shift(1)).abs(),
        (df['Low'] - df['Close'].shift(1)).abs()
    ], axis=1).max(axis=1)
    atr = tr.ewm(alpha=1/period, adjust=False).mean()
    
    df['Chandelier_Long_Stop'] = df['High'].rolling(period).max() - (mult * atr)
    df['Chandelier_Short_Stop'] = df['Low'].rolling(period).min() + (mult * atr)
    df['Trend'] = np.where(df['Close'] > df['Chandelier_Short_Stop'].shift(1), 1,
                  np.where(df['Close'] < df['Chandelier_Long_Stop'].shift(1), -1, np.nan))
    df['Trend'] = df['Trend'].ffill().fillna(1)
    return df

Get the Complete 100+ Python Quant & Automation Bundle

Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).

Buy Full Bundle on Gumroad ($19) →

Frequently Asked Questions

Q: Why is the default Chandelier Exit lookback set to 22 bars?

On daily charts, 22 trading bars represent exactly one calendar month of high/low extremes.