Keltner Channel Questions

Pengu

New member
Hello,

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.

Thanks for any help!
 
Hello,

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.

Thanks for any help!

this calculates the average drop , across the whole chart.
i didn't add an option for start and stop times.

there are 3 labels,
total drop bars on a chart,
quantity of drops,
average bars per drop.

Code:
# 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?

#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);

#
 
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

this calculates the average drop , across the whole chart.
i didn't add an option for start and stop times.

there are 3 labels,
total bars during a drop, quantity of drops, average bars per drop.

Code:
# 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);

#
This is awesome, thank you so much!
 
Hello, does anyone know how to make keltner channel with a 3.0 factor appear only for the last 5 bars on any time frame?

This has an input to limit the plot and optional clouds and lineweight inputs.

[Note to Scarlett. The link I provided on your Moxie Indicator question did not work properly and has been corrected.]

Screenshot-2023-03-22-005622.png

Code:
#KeltnerChannel_Limited_Plot

input plot_length = 5;
input lineweight  = 3;
input showclouds  = yes;

declare weak_volume_dependency;

input displace = 0;
input factor = 3.0;
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);

def dataCount = CompoundValue(1, if !IsNaN(average) then dataCount[1] + 1 else dataCount[1], 0);

plot Avg = if HighestAll(dataCount) - dataCount <= plot_length - 1 then average[-displace] else Double.NaN;
Avg.SetDefaultColor(GetColor(1));
Avg.SetLineWeight(lineweight);

plot Upper_Band =  if HighestAll(dataCount) - dataCount <= plot_length - 1 then average[-displace] + shift[-displace] else Double.NaN;
Upper_Band.SetDefaultColor(GetColor(8));
Upper_Band.SetLineWeight(lineweight);

plot Lower_Band =  if HighestAll(dataCount) - dataCount <= plot_length - 1 then average[-displace] - shift[-displace] else Double.NaN;
Lower_Band.SetDefaultColor(GetColor(5));
Lower_Band.SetLineWeight(lineweight);

AddCloud(if showclouds then Upper_Band else Double.NaN, Avg, Color.CYAN, Color.CYAN);
AddCloud(if showclouds then Avg else Double.NaN, Lower_Band, Color.MAGENTA, Color.MAGENTA);
 
Last edited:
Screenshot 2023-07-18 at 11.48.41 PM.png

Price is in the upper Keltner Channel
Price bar has the lowest low in the last 5 bars
Price bar closed in the top 25% of its range

Would like to create the opposite as well for when price is below in the lower Keltner Channel.
 
Price is in the upper Keltner Channel
Price bar has the lowest low in the last 5 bars
Price bar closed in the top 25% of its range

Would like to create the opposite as well for when price is below in the lower Keltner Channel.

i think this will do what you asked.
i copied the keltner code and left the defaults.
has inputs for quantity of bars and the percent of bar height.


Code:
# keltner_upper_dropping_00

#https://usethinkscript.com/threads/help-creating-keltner-channel-triggers.16114/
#Help creating Keltner Channel triggers

#Price is in the upper Keltner Channel
#Price bar has the lowest low in the last 5 bars
#Price bar closed in the top 25% of its range

#Would like to create the opposite as well for when price is below in the lower Keltner Channel.

def na = double.nan;
def bn = barnumber();

input high_low_bars = 5;
input bar_range_per = 25.0;

def ht = high - low;

#--------------------------------
# KeltnerChannels
# TD Ameritrade IP Company, Inc. (c) 2007-2023
#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);

def Avg = average[-displace];
def Upper_Band = average[-displace] + shift[-displace];
def Lower_Band = average[-displace] - shift[-displace];

input show_keltner = yes;
plot zAvg = if show_keltner then avg else na;
zAvg.SetDefaultColor(GetColor(1));
plot zUpper_Band = if show_keltner then Upper_Band else na;
zUpper_Band.SetDefaultColor(GetColor(8));
plot zLower_Band = if show_keltner then Lower_Band else na;
zLower_Band.SetDefaultColor(GetColor(5));

#---------------------------
#upper
# rule 1  Price is in the upper Keltner Channel
def upr1 = (close <= Upper_Band and close >= avg);

# rule 2  Price bar has the lowest low in the last 5 bars
def lo = lowest(low, high_low_bars);
def upr2 = low == lo;

# rule 3  Price bar closed in the top 25% of its range
def topperlvl = high - (ht * bar_range_per/100);
def upr3 = (close >= topperlvl);

def upper = upr1 and upr2 and upr3;

#---------------------------
#lower
# rule 1  Price is in the lower Keltner Channel
def dwnr1 = (close >= lower_Band and close <= avg);

# rule 2  Price bar has the highest high in the last 5 bars
def hi = highest(high, high_low_bars);
def dwnr2 = high == hi;

# rule 3  Price bar closed in the bottom 25% of its range
def botperlvl = low + (ht * bar_range_per/100);
def dwnr3 = (close <= botperlvl);

def lower = dwnr1 and dwnr2 and dwnr3;

#---------------------------

plot zup = upper;
zup.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
zup.SetDefaultColor(color.cyan);
zup.SetLineWeight(2);

plot zdwn = lower;
zdwn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
zdwn.SetDefaultColor(color.cyan);
zdwn.SetLineWeight(2);
#
 
Help me code a simple Keltner Buy / Sell Strategy
Using the following parameters:

Keltner Channels, 20pd, 2.25 ATR, Exponential

Buy - Price closes below lower KC band, enter long on the open of the following bar. Sell after 1 day
Sell - Same as buys but just in reverse.
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
324 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top