Script Help - Keltner Bands

Hi, I need some help scripting this. I’d like to identify when price has CLOSED BELOW a Keltner’s upper plot and CLOSED ABOVE it’s average plot (middle line). I thought I might insert something like this into my other scripts but - doesn’t seem to be doing it:

// Price Closing Above 21 EMA Average Keltner Band
c1 = KC((Day(PERIODAMOUNT=1), BARS=21) >= 0, CALC=Close, STYLE=Exponential);

// Price Closing Below 21 EMA Upper Keltner Band
c2 = KC((Day(PERIODAMOUNT=1), BARS=21) <= 1, CALC=Close, STYLE=Exponential);

c1 and c2

Hi Mark,

By default the KC() function uses the upper line value in the output when nothing else is specified, so you need to select the centre line in one of the variables, along with the required CLOSE() condition.

When creating a new script it’s best to start from scratch and use the pop-up boxes to build the formula as syntax from other scripts may not be relevant for what you are trying to achieve.

This should do what you need:

// Price Closing Above 21 EMA Average Keltner Band;
C1 = CLOSE() > KC(DEFAULT=CentreLine, BARS=21, STYLE=Exponential); 
// Price Closing Below 21 EMA Upper Keltner Band;
C2 = CLOSE() < KC(BARS=21, STYLE=Exponential);
C1 and C2

When used in a Show Bar tool the arrows highlight when both conditions have been met:

Capture

Hi Darren,

Is there any way to require both conditions to be met without having to use “Show Bar”? Reason I’m asking is that I’m trying to run a scan based on this script and open the results in a watch list - AND - to only have symbols appear in that watch list that have met BOTH conditions. I thought the last line (C1 and C2) would accomplish that but it doesn’t and using Show Bar doesn’t really help since I still end up with a watch list that contain a lot of results that don’t meet both conditions, and then I have to manually weed out all the incorrect results. How can this script be adjusted to eliminate those results?

Hi Mark,

The Show Bar was used just to prove that the script is doing what you want it to do (it’s good practice to do this before running any tests and scans). So using the formula in a scan should return correct results.

However, if you are getting incorrect scan results please let support know with examples and we will investigate further.