Need help on syntax code for keltner channel watchlist

Slickss

New member
Hello all ,no matter how many times I watch syntax code setup I cannot figure it out . I have been backtesting option setups with keltner channel TTM squeeze setup ,,I need help so with code to load into watchlist column , I would like a green indicator when daily close price is above ATR 3 (I think that’s the 87 ema) ,,corresponding red indicator when close price on daily is outside minus ATR 3 ,,,,,,my backtesting indicates once close is above ATR 3 the reversion towards mean ema 21 a put contract to be bought for 2 to 7 days ,,corresponding outside minus ATR 3 call to be bought fo4 same 2 to 7 days ,,,,thanks in advance
 

Slippage

Active member
Here's code that does what you want. I made it so the value displayed in the column is a percentage of where price is relative to the Keltner channels so the column is sortable in a way that makes sense. 100 is at the 3rd ATR above. -100 is at the 3rd ATR below.

Be careful with this strategy, especially when going short. It's very common for stocks to pop higher than the 3rd ATR and gap up even more the next day. Depending on when you get in and your position size it could lead to a margin call, getting you out at a really bad time.

Some symbols will have a value of NaN (not a number) because they don't have enough periods of data for the averages used by Keltner to be calculated.

Above/Below Keltner watchlist column
Ruby:
input factor = 3;
input price = close;

def upper = reference KeltnerChannels(factor=factor).Upper_Band;
def lower = reference KeltnerChannels(factor=factor).Lower_Band;
def mid = reference KeltnerChannels(factor=factor).Avg;

plot p =
  if price > mid then Round((price - mid) / (upper - mid) * 100, 1)
  else if price < mid then Round((mid - price) / (mid - lower) * -100, 1)
  else 0
;

AssignBackgroundColor(if price > upper then Color.DARK_GREEN else if price < lower then Color.DARK_RED else Color.BLACK);
 
Last edited:

Slickss

New member
Here's code that does what you want. I made it so the value displayed in the column is a percentage of where price is relative to the Keltner channels so the column is sortable in a way that makes sense. 100 is at the 3rd ATR above. -100 is at the 3rd ATR below.

Be careful with this strategy, especially when going short. It's very common for stocks to pop higher than the 3rd ATR and gap up even more the next day. Depending on when you get in and your position size it could lead to a margin call, getting you out at a really bad time.

Some symbols will have a value of NaN (not a number) because they don't have enough periods of data for the averages used by Keltner to be calculated.

Above/Below Keltner watchlist column
Ruby:
input factor = 3;
input price = close;

def upper = reference KeltnerChannels(factor=factor).Upper_Band;
def lower = reference KeltnerChannels(factor=factor).Lower_Band;
def mid = reference KeltnerChannels(factor=factor).Avg;

plot p =
  if price > upper then Round((price - mid) / (upper - mid) * 100, 1)
  else if price > mid then Round((upper - price) / (upper - mid) * 100, 1)
  else if price < lower then Round((mid - price) / (mid - lower) * -100, 1)
  else if price < mid then Round((price - lower) / (mid - lower) * -100, 1)
  else 0
;

AssignBackgroundColor(if price > upper then Color.DARK_GREEN else if price < lower then Color.DARK_RED else Color.BLACK);
Thankyou for the help and the code ,loaded into my watchlist and its awesome ..using as a visual indicator in S&P 100 and theotrade options to trade . Will continue on backtesting ...plus I already use simpler trading keltner TTM squeeze chart ,,,thanks again
 

Slippage

Active member
Thankyou for the help and the code

It's no problem. Sometimes I write this stuff just for an interesting code challenge and to help someone out. Sometimes it's those things but also something I think I might use. This one I might use at some point but probably not as an entry signal.
 

Similar threads

Top