Chandelier Exit Indicator for ThinkorSwim

Miket

Member
Hello- I am looking for a script to the 'Chandelier Exit' (CE)- Does anyone have that script for ThinkorSwim? I also want to optimize the atr_length and atr_mult inputs.
 
Last edited:

blackbeard12

New member
Check this

Code:
# Chuck LeBeau's Chandelier Stop
# Mobius
# V01.03.2014

input HighPeriod = 13;
input LowPeriod = 13;
input AtrPeriod = 13;
input AtrMultiplier = 1.00;
input n   = 5;

def o = open;
def h = high;
def l = low;
def c = close;
def MDI = if MDI[1] <= 0
          then c[1] + ((c - c[1]) /
              (n * (sqr(c / c[1]))))
          else MDI[1] + ((c - MDI[1]) /
              (n * (sqr(c / MDI[1]))));
def Data = Inertia(MDI, n);
def hh = if c > Data
         then Highest(Data, HighPeriod)
         else double.nan;
def ll = if c < Data
         then Lowest(Data, LowPeriod)
         else double.nan;
def ATR = WildersAverage(TrueRange(h, c, l), AtrPeriod);
def AtrScaled = (Atrmultiplier * ATR);
plot H_trigger= if c crosses above hh - AtrScaled
                then l
                else Double.NaN;
     H_trigger.SetPaintingStrategy(PaintingStrategy.Arrow_UP);
     H_trigger.SetLineWeight(4);
     H_Trigger.SetDefaultColor(Color.Green);
plot L_trigger = if l crosses below ll + AtrScaled
                 then h
                 else Double.NaN;
     L_trigger.SetPaintingStrategy(PaintingStrategy.Arrow_Down);
     L_trigger.SetLineWeight(4);
     L_trigger.SetDefaultColor(Color.Red);
# End Code
 

john3

Active member
2019 Donor
Do you mean this one?

Code:
## START STUDY
## Chandelier_Stops
## linus, 2014-07-18, v0.2

#hint: thinkScript adaptation of Chandelier stops.

#hint n: lookback length for highest highs, lowest lows.
input n = 15;

#hint atrLength: Period length of avg. true range.
input atrLength = 5;

#hint atrMult: Multiplier of avg. true range.
input atrMult = 3.0;  

#hint atrType: Moving average type of ATR.
input atrType = AverageType.SIMPLE;

#hint shift: Offsets the data this many bars.
input shift = 1; 

#hint hideOppositeStop: Set to No to see the inactive stop.
input hideOppositeStop = Yes; 

#hint label: Toggles P/L label.
input label = Yes;

#hint bubbles: Toggles P/L bubbles.
input bubbles = Yes;

def atr = MovingAverage(atrType, TrueRange(high, close, low)) * atrMult;

def smax = Lowest(low, n)[shift] + atr[shift];
def smin = Highest(high, n)[shift] - atr[shift];

def dir = compoundValue(1, if close > smax[1] then 1 else if close < smin[1] then -1 else dir[1], 0);

def rUB = compoundValue(1, if dir > 0 then if smax > rUB[1] then smax else rUB[1] else if dir < 0 then if smax < rUB[1] then smax else rUB[1] else rUB[1], high);

def rLB = compoundValue(1, if dir < 0 then if smin < rLB[1] then smin else rLB[1] else if dir > 0 then if smin > rLB[1] then smin else rLB[1] else rLB[1], low);

plot UB = if !hideOppositeStop or dir < 0 then rUB else Double.NaN;
plot LB = if !hideOppositeStop or dir > 0 then rLB else Double.NaN;

UB.SetDefaultColor(Color.MAGENTA);
LB.SetDefaultColor(Color.CYAN);

UB.SetLineWeight(3);
LB.SetLineWeight(3);

def orderDir = dir;

def isOrder = orderDir crosses 0;

def orderCount = compoundValue(1, if isNaN(isOrder) then 0 else if isOrder then orderCount[1] + 1 else orderCount[1], 0);

def noBar = isNaN(open[-1]);

def orderPrice = if isOrder then if noBar then close else open[-1] else orderPrice[1];
def profitLoss = if !isOrder or orderCount == 1 then 0 else if orderDir > 0 then orderPrice[1] - orderPrice else if orderDir < 0 then orderPrice - orderPrice[1] else 0;
def profitLossSum = compoundValue(1, if isNaN(isOrder) then 0 else if isOrder then profitLossSum[1] + profitLoss else profitLossSum[1], 0);

AddLabel(label, orderCount + " orders | P/L " + AsDollars((profitLossSum / tickSize()) * tickValue()), if profitLossSum > 0 then Color.GREEN else if profitLossSum < 0 then Color.RED else Color.GRAY);

AddChartBubble(bubbles and isOrder and orderDir > 0, low, profitLoss, if noBar then Color.LIGHT_GRAY else Color.GREEN, 0);
AddChartBubble(bubbles and isOrder and orderDir < 0, high, profitLoss, if noBar then Color.GRAY else Color.RED, 1);

## END STUDY
 

wtf_dude

Active member
It's a pretty simple one. Basically, buy when the line turns blue. Sell or go short when the line turns magenta. The change over is based on the average true range and is basically the exact same as ATRtrailingstop already in TOS.
 
@john3 It's not accurate. I saw the price go below the band but the the color didn't change. I think I gave it a good shot and came up with one. Somehow these darn volatility trailing stops are hard to find and done right that aren't supertrend, and even that is sometimes done wrong. I found one on tradingview that that looks pretty solid that liked, or atleast the code looks solid I think. I gave it a whirl, can't be worse than what's mostly out there anyway. I then compared it to Wilder's original volatility trailing stop and noticed that the ATR length is much higher on the Chandelier version. I might be wrong but I'll assume this was a mistake because it looks way off when plotted on the chart so I changed it from 22 to a 7 because 22 seems way off. The highest high and lowest low used for the bands can supposedly be changed to just closing price. If you change the ATR length to be the same as the lookback length and the highest and lowest, from high and low, to close and close then it becomes Wilder's volatility stop. Hope yall like it.
Ruby:
#Chandelier Volatility Trailing Stop

#SparkyFlary

#Code taken and re-edited to thinkorswim from pipCharlie, who got it from LazyBear
#https://www.tradingview.com/script/mjBdRGXe-Chandelier-Stop/

input AtrMult = 3.0;
input ATRlength = 7;
input lookbackLength = 22;
input highestHigh = high;
input lowestLow = low;
input AvgType = AverageType.WILDERS;
input PaintBars = no;

def ATR = MovingAverage(AvgType, TrueRange(high, close, low), ATRlength);
def longstop = Highest(highestHigh,lookbackLength)-AtrMult*atr;
def shortstop = Lowest(lowestLow,lookbackLength)+AtrMult*atr;

def shortvs = if isNaN(shortvs[1]) then shortstop else if close>shortvs[1] then shortstop else min(shortstop,shortvs[1]);
def longvs = if isNaN(longvs[1]) then longstop else if close<longvs[1] then longstop else max(longstop,longvs[1]);

def longswitch= if close>=shortvs[1] and close[1]<shortvs[1] then 1 else 0;
def shortswitch = if close<=longvs[1] and close[1]>longvs[1] then 1 else 0;

def direction = if isNaN(direction[1]) then 0 else if direction[1]<=0 and longswitch then 1 else if direction[1]>=0 and shortswitch then -1 else direction[1];
          
def pc = if direction>0 then longvs else shortvs;

plot VolatilityStop = pc;
VolatilityStop.AssignValueColor(if direction < 0 then Color.RED else Color.GREEN);
AssignPriceColor(if PaintBars and direction < 0
                 then Color.RED
                 else if PaintBars and direction > 0
                      then Color.GREEN
                      else Color.CURRENT);
 

E_Sophiea

New member
Hello- I am looking for a script to the 'Chandelier Exit' (CE)- Does anyone have that script for ThinkorSwim? I also want to optimize the atr_length and atr_mult inputs.
I think people have confused a Chadelier Exit with a Chadelier Stop. They are not the same.

In ThinkorSwim, the Chandelier Exit is already coded and called ATRTrailingStop
 

SirSeff

New member
@john3 It's not accurate. I saw the price go below the band but the the color didn't change. I think I gave it a good shot and came up with one. Somehow these darn volatility trailing stops are hard to find and done right that aren't supertrend, and even that is sometimes done wrong. I found one on tradingview that that looks pretty solid that liked, or atleast the code looks solid I think. I gave it a whirl, can't be worse than what's mostly out there anyway. I then compared it to Wilder's original volatility trailing stop and noticed that the ATR length is much higher on the Chandelier version. I might be wrong but I'll assume this was a mistake because it looks way off when plotted on the chart so I changed it from 22 to a 7 because 22 seems way off. The highest high and lowest low used for the bands can supposedly be changed to just closing price. If you change the ATR length to be the same as the lookback length and the highest and lowest, from high and low, to close and close then it becomes Wilder's volatility stop. Hope yall like it.
Ruby:
#Chandelier Volatility Trailing Stop

#SparkyFlary

#Code taken and re-edited to thinkorswim from pipCharlie, who got it from LazyBear
#https://www.tradingview.com/script/mjBdRGXe-Chandelier-Stop/

input AtrMult = 3.0;
input ATRlength = 7;
input lookbackLength = 22;
input highestHigh = high;
input lowestLow = low;
input AvgType = AverageType.WILDERS;
input PaintBars = no;

def ATR = MovingAverage(AvgType, TrueRange(high, close, low), ATRlength);
def longstop = Highest(highestHigh,lookbackLength)-AtrMult*atr;
def shortstop = Lowest(lowestLow,lookbackLength)+AtrMult*atr;

def shortvs = if isNaN(shortvs[1]) then shortstop else if close>shortvs[1] then shortstop else min(shortstop,shortvs[1]);
def longvs = if isNaN(longvs[1]) then longstop else if close<longvs[1] then longstop else max(longstop,longvs[1]);

def longswitch= if close>=shortvs[1] and close[1]<shortvs[1] then 1 else 0;
def shortswitch = if close<=longvs[1] and close[1]>longvs[1] then 1 else 0;

def direction = if isNaN(direction[1]) then 0 else if direction[1]<=0 and longswitch then 1 else if direction[1]>=0 and shortswitch then -1 else direction[1];
        
def pc = if direction>0 then longvs else shortvs;

plot VolatilityStop = pc;
VolatilityStop.AssignValueColor(if direction < 0 then Color.RED else Color.GREEN);
AssignPriceColor(if PaintBars and direction < 0
                 then Color.RED
                 else if PaintBars and direction > 0
                      then Color.GREEN
                      else Color.CURRENT);
Hi, I am at my wit's end..
can someone please help me place "AddOrder(OrderType.BUY_TO_OPEN" when the volatility stop turns red to green and Sell_to_open when it turns green to red?

@SparkyFlary
 

JP782

Active member
Here is a TOS Thinkscript version of "Chandelier Exit" translated from Tradingview.
source link: https://www.facebook.com/CustomThinkscript
http://tos.mx/7C9Ahas
Ruby:
#Chandelier Exit (Upper Study)
#Version 1.0 Released 04/10/22
#Created by CustomThinkscript

input ATRLength = 22;
input ATRMultiplier = 3;
input ShowLabels = yes;
input UseClose = yes;
input HighlightState = yes;
input Alerts = no;

def atr = ATRMultiplier * ATR(ATRLength);

def longStop = (if UseClose then highest(close, ATRLength) else highest(high, ATRLength)) - atr;
def longStopPrev = if IsNan(LongStop[1]) then longStop else LongStop[1];
def LS =if close[1] > longStopPrev then max(longStop, longStopPrev) else longStop;


def shortStop = (if UseClose then lowest(close, ATRLength) else lowest(low, ATRLength)) + atr;
def shortStopPrev = if IsNan(shortStop[1]) then shortStop else shortStop[1];
def SS =if close[1] < shortStopPrev then min(shortStop, shortStopPrev) else shortStop;

def dir = if close > shortStopPrev then 1 else if close < longStopPrev then -1 else dir[1];
def direction = dir;

plot LongStopPlot = if direction == 1 then LS else double.Nan;
longStopPlot.SetDefaultColor(Color.Green);

plot ShortStopPlot = if direction == -1 then SS else Double.Nan;
shortStopPlot.SetDefaultColor(Color.Red);

def midPricePlot = OHLC4;

def buySignal = direction == 1 and direction[1] == -1;
def sellSignal = direction == -1 and direction[1] == 1;

AddChartBubble(buySignal and ShowLabels, low, "Buy Signal", Color.Green, no);
AddChartBubble(sellSignal and ShowLabels, high, "Sell Signal", Color.Red, yes);

AddCloud(if direction == 1 and HighlightState then longStopPlot else Double.Nan, If direction == 1 and HighlightState then midPricePlot else Double.Nan, Color.Green, Color.Green, no);

AddCloud(if direction == -1 and HighlightState then shortStopPlot else Double.Nan, If direction == -1 and HighlightState then midPricePlot else Double.Nan, Color.Red, Color.Red, no);

Alert(direction == 1 and direction[1] == -1 and Alerts, "'Chandelier Exit' Buy!", Alert.ONCE, Sound.Ding);
Alert(direction == -1 and direction[1] == 1 and Alerts, "'Chandelier Exit' Sell!", Alert.ONCE, Sound.Ding);
 

I_Tucci

New member
VIP
Here is a TOS Thinkscript version of "Chandelier Exit" translated from Tradingview.
source link: https://www.facebook.com/CustomThinkscript
http://tos.mx/7C9Ahas
Ruby:
#Chandelier Exit (Upper Study)
#Version 1.0 Released 04/10/22
#Created by CustomThinkscript

input ATRLength = 22;
input ATRMultiplier = 3;
input ShowLabels = yes;
input UseClose = yes;
input HighlightState = yes;
input Alerts = no;

def atr = ATRMultiplier * ATR(ATRLength);

def longStop = (if UseClose then highest(close, ATRLength) else highest(high, ATRLength)) - atr;
def longStopPrev = if IsNan(LongStop[1]) then longStop else LongStop[1];
def LS =if close[1] > longStopPrev then max(longStop, longStopPrev) else longStop;


def shortStop = (if UseClose then lowest(close, ATRLength) else lowest(low, ATRLength)) + atr;
def shortStopPrev = if IsNan(shortStop[1]) then shortStop else shortStop[1];
def SS =if close[1] < shortStopPrev then min(shortStop, shortStopPrev) else shortStop;

def dir = if close > shortStopPrev then 1 else if close < longStopPrev then -1 else dir[1];
def direction = dir;

plot LongStopPlot = if direction == 1 then LS else double.Nan;
longStopPlot.SetDefaultColor(Color.Green);

plot ShortStopPlot = if direction == -1 then SS else Double.Nan;
shortStopPlot.SetDefaultColor(Color.Red);

def midPricePlot = OHLC4;

def buySignal = direction == 1 and direction[1] == -1;
def sellSignal = direction == -1 and direction[1] == 1;

AddChartBubble(buySignal and ShowLabels, low, "Buy Signal", Color.Green, no);
AddChartBubble(sellSignal and ShowLabels, high, "Sell Signal", Color.Red, yes);

AddCloud(if direction == 1 and HighlightState then longStopPlot else Double.Nan, If direction == 1 and HighlightState then midPricePlot else Double.Nan, Color.Green, Color.Green, no);

AddCloud(if direction == -1 and HighlightState then shortStopPlot else Double.Nan, If direction == -1 and HighlightState then midPricePlot else Double.Nan, Color.Red, Color.Red, no);

Alert(direction == 1 and direction[1] == -1 and Alerts, "'Chandelier Exit' Buy!", Alert.ONCE, Sound.Ding);
Alert(direction == -1 and direction[1] == 1 and Alerts, "'Chandelier Exit' Sell!", Alert.ONCE, Sound.Ding);

Just awesome stuff! Thank you!
 

Nick

New member
Thanks for the Chandelier Exit script. In addition to Buy and Sell stops, it provides a view of volatility, trend, and coupled with other indicators, it can assist with trade entries. Thanks again.
 

Kitchasap

New member
VIP
Here is a TOS Thinkscript version of "Chandelier Exit" translated from Tradingview.
source link: https://www.facebook.com/CustomThinkscript
http://tos.mx/7C9Ahas
Ruby:
#Chandelier Exit (Upper Study)
#Version 1.0 Released 04/10/22
#Created by CustomThinkscript

input ATRLength = 22;
input ATRMultiplier = 3;
input ShowLabels = yes;
input UseClose = yes;
input HighlightState = yes;
input Alerts = no;

def atr = ATRMultiplier * ATR(ATRLength);

def longStop = (if UseClose then highest(close, ATRLength) else highest(high, ATRLength)) - atr;
def longStopPrev = if IsNan(LongStop[1]) then longStop else LongStop[1];
def LS =if close[1] > longStopPrev then max(longStop, longStopPrev) else longStop;


def shortStop = (if UseClose then lowest(close, ATRLength) else lowest(low, ATRLength)) + atr;
def shortStopPrev = if IsNan(shortStop[1]) then shortStop else shortStop[1];
def SS =if close[1] < shortStopPrev then min(shortStop, shortStopPrev) else shortStop;

def dir = if close > shortStopPrev then 1 else if close < longStopPrev then -1 else dir[1];
def direction = dir;

plot LongStopPlot = if direction == 1 then LS else double.Nan;
longStopPlot.SetDefaultColor(Color.Green);

plot ShortStopPlot = if direction == -1 then SS else Double.Nan;
shortStopPlot.SetDefaultColor(Color.Red);

def midPricePlot = OHLC4;

def buySignal = direction == 1 and direction[1] == -1;
def sellSignal = direction == -1 and direction[1] == 1;

AddChartBubble(buySignal and ShowLabels, low, "Buy Signal", Color.Green, no);
AddChartBubble(sellSignal and ShowLabels, high, "Sell Signal", Color.Red, yes);

AddCloud(if direction == 1 and HighlightState then longStopPlot else Double.Nan, If direction == 1 and HighlightState then midPricePlot else Double.Nan, Color.Green, Color.Green, no);

AddCloud(if direction == -1 and HighlightState then shortStopPlot else Double.Nan, If direction == -1 and HighlightState then midPricePlot else Double.Nan, Color.Red, Color.Red, no);

Alert(direction == 1 and direction[1] == -1 and Alerts, "'Chandelier Exit' Buy!", Alert.ONCE, Sound.Ding);
Alert(direction == -1 and direction[1] == 1 and Alerts, "'Chandelier Exit' Sell!", Alert.ONCE, Sound.Ding);
Thanks it worked well on ES today
 

Lando7

New member
Here is a TOS Thinkscript version of "Chandelier Exit" translated from Tradingview.
source link: https://www.facebook.com/CustomThinkscript
http://tos.mx/7C9Ahas
Ruby:
#Chandelier Exit (Upper Study)
#Version 1.0 Released 04/10/22
#Created by CustomThinkscript

input ATRLength = 22;
input ATRMultiplier = 3;
input ShowLabels = yes;
input UseClose = yes;
input HighlightState = yes;
input Alerts = no;

def atr = ATRMultiplier * ATR(ATRLength);

def longStop = (if UseClose then highest(close, ATRLength) else highest(high, ATRLength)) - atr;
def longStopPrev = if IsNan(LongStop[1]) then longStop else LongStop[1];
def LS =if close[1] > longStopPrev then max(longStop, longStopPrev) else longStop;


def shortStop = (if UseClose then lowest(close, ATRLength) else lowest(low, ATRLength)) + atr;
def shortStopPrev = if IsNan(shortStop[1]) then shortStop else shortStop[1];
def SS =if close[1] < shortStopPrev then min(shortStop, shortStopPrev) else shortStop;

def dir = if close > shortStopPrev then 1 else if close < longStopPrev then -1 else dir[1];
def direction = dir;

plot LongStopPlot = if direction == 1 then LS else double.Nan;
longStopPlot.SetDefaultColor(Color.Green);

plot ShortStopPlot = if direction == -1 then SS else Double.Nan;
shortStopPlot.SetDefaultColor(Color.Red);

def midPricePlot = OHLC4;

def buySignal = direction == 1 and direction[1] == -1;
def sellSignal = direction == -1 and direction[1] == 1;

AddChartBubble(buySignal and ShowLabels, low, "Buy Signal", Color.Green, no);
AddChartBubble(sellSignal and ShowLabels, high, "Sell Signal", Color.Red, yes);

AddCloud(if direction == 1 and HighlightState then longStopPlot else Double.Nan, If direction == 1 and HighlightState then midPricePlot else Double.Nan, Color.Green, Color.Green, no);

AddCloud(if direction == -1 and HighlightState then shortStopPlot else Double.Nan, If direction == -1 and HighlightState then midPricePlot else Double.Nan, Color.Red, Color.Red, no);

Alert(direction == 1 and direction[1] == -1 and Alerts, "'Chandelier Exit' Buy!", Alert.ONCE, Sound.Ding);
Alert(direction == -1 and direction[1] == 1 and Alerts, "'Chandelier Exit' Sell!", Alert.ONCE, Sound.Ding);
Thank you so much!!!
 

JP782

Active member
I used it for 10 mins for Intraday scalping? But please do retest for yourself in this volatile market.
Yes you have to use other indicators to verify direction & entry... the claim by many "pros" to 'just use price action and volume' is bs. Ive literally watched them lose money on trades (if I had the bank to take )I WOULD NOT take, only to be proven right when it tanks and theyre like oh well, thats the name of the game: NO IT IS NOT
 
Here is a TOS Thinkscript version of "Chandelier Exit" translated from Tradingview.
source link: https://www.facebook.com/CustomThinkscript
http://tos.mx/7C9Ahas
Ruby:
#Chandelier Exit (Upper Study)
#Version 1.0 Released 04/10/22
#Created by CustomThinkscript

input ATRLength = 22;
input ATRMultiplier = 3;
input ShowLabels = yes;
input UseClose = yes;
input HighlightState = yes;
input Alerts = no;

def atr = ATRMultiplier * ATR(ATRLength);

def longStop = (if UseClose then highest(close, ATRLength) else highest(high, ATRLength)) - atr;
def longStopPrev = if IsNan(LongStop[1]) then longStop else LongStop[1];
def LS =if close[1] > longStopPrev then max(longStop, longStopPrev) else longStop;


def shortStop = (if UseClose then lowest(close, ATRLength) else lowest(low, ATRLength)) + atr;
def shortStopPrev = if IsNan(shortStop[1]) then shortStop else shortStop[1];
def SS =if close[1] < shortStopPrev then min(shortStop, shortStopPrev) else shortStop;

def dir = if close > shortStopPrev then 1 else if close < longStopPrev then -1 else dir[1];
def direction = dir;

plot LongStopPlot = if direction == 1 then LS else double.Nan;
longStopPlot.SetDefaultColor(Color.Green);

plot ShortStopPlot = if direction == -1 then SS else Double.Nan;
shortStopPlot.SetDefaultColor(Color.Red);

def midPricePlot = OHLC4;

def buySignal = direction == 1 and direction[1] == -1;
def sellSignal = direction == -1 and direction[1] == 1;

AddChartBubble(buySignal and ShowLabels, low, "Buy Signal", Color.Green, no);
AddChartBubble(sellSignal and ShowLabels, high, "Sell Signal", Color.Red, yes);

AddCloud(if direction == 1 and HighlightState then longStopPlot else Double.Nan, If direction == 1 and HighlightState then midPricePlot else Double.Nan, Color.Green, Color.Green, no);

AddCloud(if direction == -1 and HighlightState then shortStopPlot else Double.Nan, If direction == -1 and HighlightState then midPricePlot else Double.Nan, Color.Red, Color.Red, no);

Alert(direction == 1 and direction[1] == -1 and Alerts, "'Chandelier Exit' Buy!", Alert.ONCE, Sound.Ding);
Alert(direction == -1 and direction[1] == 1 and Alerts, "'Chandelier Exit' Sell!", Alert.ONCE, Sound.Ding);
Hey JP, thanks, however, I tested it against TV version and it's not coming out exact. at least in 1m timeframe.
Also, you'll want to use decimal for atrmult to be more precise.
 

Similar threads

Top