# Keltner_toptobottombars_0
# KeltnerChannels
# TD Ameritrade IP Company, Inc. (c) 2007-2022
declare weak_volume_dependency;
input displace = 0;
input factor = 1.5;
input length = 20;
input price = close;
input averageType = AverageType.SIMPLE;
input trueRangeAverageType = AverageType.SIMPLE;
def shift = factor * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
def average = MovingAverage(averageType, price, length);
plot Avg = average[-displace];
Avg.SetDefaultColor(GetColor(1));
plot Upper_Band = average[-displace] + shift[-displace];
Upper_Band.SetDefaultColor(GetColor(8));
plot Lower_Band = average[-displace] - shift[-displace];
Lower_Band.SetDefaultColor(GetColor(5));
#=----------------------------
def bn = barnumber();
def na = double.nan;
# https://usethinkscript.com/threads/how-to-find-last-occurrence-of-touching-a-keltner-channel-top-to-bottom.11974/
#How to Find Last Occurrence of Touching a Keltner Channel Top to Bottom?
# Thread starterPengu Start dateWednesday at 1:52 PM Tagsunanswered
#I have experience coding however, still learning my way through ThinkScript. I am trying to get a idea of how I could run an indicator for a certain time threshold to find the average of how many candles it takes to go from the top to bottom of a keltner channel.
#Example: I could put an input to specify 3 years and compute the average amount of candles where a ticker went from the top to bottom keltner channel.
#I’m not looking for a full solution but just some advice on where to start. Mostly an idea of how to find where the top and bottom touched.
#keep a var to tell what was the prev crossed level , upper or lower
#def xupper1 = high crosses Upper_Band;
#def xlower1 = low crosses Lower_Band;
def xupper2 = high > Upper_Band and low < Upper_Band;
def xlower2 = high > Lower_Band and low < lower_band;
def xupper = xupper2;
def xlower = xlower2;
#prev crossing
def klevel = if bn == 1 then 0
else if xupper then 1
else if xlower then -1
else klevel[1];
#drop --- upper to lower ---------------
#keep bn of last cross of upper
def drop_up_bn = if bn == 1 then 0 else if xupper then bn else drop_up_bn[1];
def drop_up_chg = if drop_up_bn != drop_up_bn[1] then 1 else 0;
#true when a drop period ends
def drop_end = if bn == 1 then 0 else (xlower and klevel[1] == 1);
#keep bn of 1st cross of lower after an upper cross
def drop_low_bn = if bn == 1 then 0 else if drop_end then bn else drop_low_bn[1];
#bars during a drop
def drop_bars = if drop_end then
(drop_low_bn - drop_up_bn + 1)
else drop_bars[1];
input test1 = no;
addchartbubble(test1, lower_band*0.993,
bn + "\n" +
xupper + "\n" +
xlower + "\n" +
drop_up_bn + " up\n" +
drop_end + " end\n" +
drop_low_bn + "\n" +
drop_bars
, (if drop_up_chg then color.yellow else if drop_end then color.magenta else color.gray), no);
#bars during all drops
def drop_bars_ttl = if bn == 1 then 0
else if drop_end then drop_bars_ttl[1] + drop_bars else drop_bars_ttl[1];
def drop_cnt = if bn == 1 then 0
else if drop_end then drop_cnt[1] + 1
else drop_cnt[1];
def drop_bars_avg = if drop_cnt > 0 then round(drop_bars_ttl / drop_cnt, 1) else 0;
addlabel(1, "total drop bars " + drop_bars_ttl, color.yellow);
addlabel(1, "qty of drops " + drop_cnt, color.yellow);
addlabel(1, "average bars per drop " + drop_bars_avg, color.yellow);
input test2 = no;
addchartbubble(test2, lower_band*0.993,
drop_bars_ttl + "\n" +
drop_cnt + "\n" +
drop_bars_avg
, color.yellow, no);
#