Keltner Channel Strategy - Need help Coding

Lone Wolf

New member
Plus
Hi,

Does anybody know if I can scan using TOS for stocks where the body of the candle is COMPLETELY above or below the upper/lower Keltner Channel (using length 20, and 2.25 ATR)?

I see there are criteria for Bollinger bands there, but I am looking to scan for stocks where the body is completely outside the Keltner channel.

Let me know.

Thanks!
 
This should be easy for you all but I am having a hard to doing this. I want to show the column to show a 1 if both of the last 2 bars have closed above the Keltner Channel. Of course the same with below but that's just the opposite. Here is what I have. The issue is that it is still sometimes showing a 1 when clearly the last 2 bars are not above the band.

Code:
close from 2 bars ago is greater than KeltnerChannels("factor" = 2.0)."Upper_Band" and close from 1 bars ago is greater than KeltnerChannels("factor" = 2.0)."Upper_Band"
you are comparing close from a previous bar to the current bar of keltner price.
i would set 2 variables, to close and keltner. then reference them with offsets ( [1] and [2] ) in another formula.
 
you are comparing close from a previous bar to the current bar of keltner price.
i would set 2 variables, to close and keltner. then reference them with offsets ( [1] and [2] ) in another formula.
I thought two bars ago and one bars ago were not the current bar? I thought my code was basically saying if one bar ago and two bars ago are both above KC upper band then show 1 and if not 0. It seems to work in most cases but not all.
 
I thought two bars ago and one bars ago were not the current bar?
correct
the 1 bar ago and 2 bars ago , only apply to close, not to keltner.
do you want to compare the value of keltner on the current bar, to previous bars of close ?
i would think you would want to compare close and keltner from the same bar.

here is an equivalent code.
see how keltner is read from the current bar, not a previous one.
is this what you intended?

Code:
def cls1 = close from 1 bars ago;
def cls2 = close from 2 bars ago;
def k = KeltnerChannels("factor" = 2.0)."Upper_Band";
def z = (cls2 > k and cls1 > k);

i don't use words. i would write the formulas this way.
Code:
def cls0 = close;
def cls1 = cls0[1];
def cls2 = cls0[2];
 
Thanks. Just made the change and same issue. I don't know how to include a screenshot to show but USD/CHF is showing a 1 and it's clearly not above the upper Keltner Channel band. Everything lines up, 15min timeframe on code and chart, factor of 2 lines up, I don't know what is going on. I've seen others include screenshots, maybe if someone can help me figure out how to include a screenshot it may make it easier.
 
Thanks. Just made the change and same issue. I don't know how to include a screenshot to show but USD/CHF is showing a 1 and it's clearly not above the upper Keltner Channel band. Everything lines up, 15min timeframe on code and chart, factor of 2 lines up, I don't know what is going on. I've seen others include screenshots, maybe if someone can help me figure out how to include a screenshot it may make it easier.
reread post 4
i think you are missing what i am saying.

do you want to compare the value of keltner from the current bar, OR the same offset used on close?

i would think you would want to compare a keltner value from the same bar as close, a previous bar.

Code:
# this version adds an offset to the keltner variable, so the data comes from the same bar as the close data.
def cls = close;
def k = KeltnerChannels("factor" = 2.0)."Upper_Band";
plot z = (cls[2] > k[2] and cls[1] > k[1]);


post images
thinkscript heading
questions
5th post at top, in tan area.
https://usethinkscript.com/threads/how-to-insert-image-in-a-post-thread.277/
 
Thank you. Nothing is above the KC band right now so I'll wait to see if this works. But essentially what I am trying to do is check 2 bars ago and compare the close of that bar to the KC band at that time as well as look at last bar's close and compare that to the KC upper band at that same bar. In other words, is the close of the bar above the KC band at each of those two points in time.
 
Oh one more question....right now it just shows 0 or 1, how can I have it not show numbers and just colors? green if it would have been a 1 or and red if it were to say 0
 
Oh one more question....right now it just shows 0 or 1, how can I have it not show numbers and just colors? green if it would have been a 1 or and red if it were to say 0
column study, with just a colored cell

Code:
# column study
def cls = close; 
def k = KeltnerChannels("factor" = 2.0)."Upper_Band"; 
def z = (cls[2] > k[2] and cls[1] > k[1]);

# if you want to display a value
#addlabel(1, ",z= " + z, color.yellow);

assignbackgroundcolor( if z == 1 then color.green else color.red) );
 
@Lone Wolf Here you go:

Code:
# KeltnerChannels
# TD Ameritrade IP Company, Inc. (c) 2007-2020
# Modified by BenTen at UseThinkScript.com
# Added paintbars

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

AssignPriceColor(if close > Upper_Band then color.green else if close < Lower_Band then color.red else color.white);
Hey ben, Is there a way I can turn this to a label where the label will only display Upper Range : (Value) Lower Range: (Value)
 
Hey ben, Is there a way I can turn this to a label where the label will only display Upper Range : (Value) Lower Range: (Value)
This script is for the label only.
Ruby:
# KeltnerChannels
# TD Ameritrade IP Company, Inc. (c) 2007-2020
# Modified by BenTen at UseThinkScript.com
# Added paintbars

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];
AddLabel(yes,
"Keltner Upper: " +round(Upper_Band,2) +" | "+
"Keltner Lower: " +round(Lower_Band,2) , color.blue);
ibv81Ru.png
 
Last edited:
Hello,

I have not used my TOS platform in a few years and forgot most of its functionality. Any input on how to create this scanner would be greatly appreciated.

I am looking to create a scan to look for stocks with the following conditions.

1. The stock made a new all-time high within the last 30 - 90 days ( the days I can adjust but just looking here for a starting point. This may be more or less depending on what I see).
2. The stock is currently trading below its new all-time high (pulling back) but not too far off.

A picture is worth a thousand words so I will just show you approximately how to quantify a setup like this (see image below). I am basically trying to find pullback-type setups. Stock has recently made a new high and currently pulling back to about the 50% ish level from the most recent upswing.

The picture below is like textbook what I'm looking for, but really just looking for a scan to get to set ups that more or less look something like this. From there I will discreditably choose if I like the set up, but just looking for a start.
Screenshot 2023-05-30 at 6.47.31 PM.png


Thanks in advance!
 

Attachments

  • Screenshot 2023-06-17 at 11.13.11 PM.png
    Screenshot 2023-06-17 at 11.13.11 PM.png
    598.7 KB · Views: 63
Rules for buy entries
  1. Price closes above the upper Keltner channel
  2. Following the price close above the upper channel, buy when price crosses back below the upper channel, and the closing bar is equal to or below the value of the prior periods moving average value.
  3. Exit at the most recent pivot high.
Keltner Channel settings: 2.25, 20pd, exponential.

Also looking to test exits of 1,5,10, and 20 days after entry.


Sell signals would just be the opposite.

See the picture below.
Screenshot 2023-06-21 at 11.48.41 PM.png

1. Price closes above the upper channel
2. Following that, entering when price closes at or below the previous day's moving average value.
3. Exit at the most recent swing high that set up the trade. Somewhere around the #3 area on the chart.

Thanks!
 
Last edited:
Rules for buy entries
  1. price closes about the upper keltner channel
  2. Following the close of the upper channel, buy when price crosses back below the upper channel, and is equal to or below the value of the prior periods moving average value.
  3. Exit 10 days after entry
Also looking to test exits of 1,5,10, and 20 days after entry.

Sell signals would just be the opposite.

Thanks!

can you edit your post and rewrite your rules to clarify a few things.

what does closes about mean?
...price closes about the


what does this mean? upper channel is a line. there is no close.
...Following the close of the upper channel,


... buy when price crosses back below the upper channel,


not sure on this, what is prior period?
and is equal to or below the value of the prior periods moving average value.
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
335 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