Strategy · Pine Script v5 · On the chart

BB-Keltner Squeeze with KDJ strategy

by wynnforthework

This strategy overlays Bollinger Bands and Keltner Channels on the price chart alongside an internal KDJ oscillator.

View on GitHub

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code112
Settings10inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago7 Oct 2025

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

112 lines of Pine Script v5 from 策略/开源分享/BB-Keltner Squeeze with KDJ strategy.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

BB-Keltner Squeeze with KDJ strategy.txt
//@version=5
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Modified by Juan C for FVBO Strategy, some source code from Trader-Elisa
strategy(title='BB-Keltner Squeeze with KDJ strategy', overlay=true, initial_capital=1000)
length = input.int(20, minval=1, title='Length')
src = input(close, title='Source')
lowest = input(5, title='How many candles should the stop loss be placed ahead? Lowest price.')
RRR = input(1.5, title='盈亏比')
// 2 Standard Deviation Bollinger Bands
B2mult = input.float(2.0, minval=0.001, maxval=50, title='BB Standard Deviation')
B2basis = ta.sma(src, length)
B2dev = B2mult * ta.stdev(src, length)
B2upper = B2basis + B2dev
B2lower = B2basis - B2dev
// plot(B2basis, color=color.new(#9c27b0, 0), style=plot.style_cross, linewidth=2)
p1 = plot(B2upper, color=color.new(#673ab7, 0), linewidth=2, title='Band 2SD upper', style=plot.style_line)
p2 = plot(B2lower, color=color.new(#673ab7, 0), linewidth=2, title='Band 2SD lower', style=plot.style_line)


// KDJ指标
ilong = input(25, title='period')
isig = input(3, title='signal')

bcwsma(s, l, m) =>
    _s = s
    _l = l
    _m = m
    var float _bcwsma = na
    _bcwsma := (_m * _s + (_l - _m) * nz(_bcwsma[1])) / _l
    _bcwsma

c = close
h = ta.highest(high, ilong)
l = ta.lowest(low, ilong)
RSV = 100 * ((c - l) / (h - l))
pK = bcwsma(RSV, isig, 1)
pD = bcwsma(pK, isig, 1)
pJ = 3 * pK - 2 * pD


// Keltner channels
useTrueRange = input(true)

Kmult = input(1.5, title='Keltner Range')
Kma = ta.ema(src, length)
Krange = useTrueRange ? ta.tr : high - low
Krangema = ta.ema(Krange, length)
Kupper = Kma + Krangema * Kmult
Klower = Kma - Krangema * Kmult

plot(Kma, title='Keltner Basis', color=color.new(#2196f3, 0), style=plot.style_line, linewidth=1)
// p5 = plot(Kupper, title='Keltner upper', color=color.new(#2196f3, 0), linewidth=1, style=plot.style_line)
// p6 = plot(Klower, title='Keltner lower', color=color.new(#2196f3, 0), linewidth=1, style=plot.style_line)
// fill(p5, p6, color=color.new(#2196f3, 90), title='Keltner Channel Fill')

// Highlight Crossover (Squeeze) //

bb_condition = input.string(defval='Both', title='Bollinger Band Crossover Condition', options=['Both', 'At Least One'])

// 第一个条件 符合挤压条件
where1 = B2upper <= Kupper and B2lower >= Klower 

//第二个条件 挤压范围内 某一根K线收盘价收在布林通道下线 并且kdj 的J线小于20
where2 = close < B2lower and pJ < 20 and where1

//第三个条件 在前两个条件满足后,再找出KDJ大于20 且close收涨 就是关键K线
var w2 = 0 
if where2
    w2 := 1 
// 开仓时的止损和止盈价位,并存储在变量中
stop_loss = ta.lowest(lowest) //5根K线内最低价
take_profit = ((close - stop_loss) * RRR) + close //试了下固定盈亏比,效果不好
var stop_loss_initial = 0.0
var take_profit_initial = 0.0
var num = 0
where3 = (w2 == 1 and pJ > 20 and close > close[1])
//满足条件开多单
if where3
    // 在开仓时存储止损和止盈价位 bar_index
    stop_loss_initial := stop_loss
    take_profit_initial := take_profit
    num := 1
    strategy.entry('long',strategy.long)
    w2 := 0
    // 下单时的止损和止盈不变
    strategy.exit("止损", "long",stop=stop_loss_initial)
// 如果kdj 的J线大于80,再跌下80 进行平仓 其实就是死叉
if(ta.crossunder(pJ,80))
    strategy.close("long", qty_percent = 100, comment = "KDJ跌破80止盈")


cross_over_color = if bb_condition == 'Both'
    cross_over_color = B2upper <= Kupper and B2lower >= Klower ? color.aqua : na
    cross_over_color
else
    cross_over_color = B2upper <= Kupper or B2lower >= Klower ? color.aqua : na
    cross_over_color

bgcolor(color=cross_over_color, title='挤压区间', transp=90)

// // 绘制止损和止盈价位
// plot(stop_loss_initial, "Stop Loss", color.red)
// plot(take_profit_initial, "Take Profit", color.green)
// 在每个周期结束时,检查订单状态并更新止损和止盈价位
if strategy.position_size == 0
    stop_loss_initial := stop_loss
    take_profit_initial := take_profit
    num := 0

// Alert Option (to trigger you need to actually set a Tradingview alert) //
alert = B2upper <= Kupper and B2lower >= Klower ? true : false
alertcondition(alert, title='BB-Kelt Squeeze', message='BB Kelt Squeeze')

Overview

BB-Keltner Squeeze with KDJ strategy identifies a volatility squeeze when Bollinger Bands contract inside the Keltner Channels. Long entries trigger after price closes below the lower band with an oversold KDJ J-line below 20, followed by an up bar with the J-line crossing above 20. Positions exit via a stop loss placed at recent lowest lows or when the J-line crosses under 80. Main settings configure channel lengths, standard deviations, and KDJ periods.

Details

Published by

wynnforthework on GitHub

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Volatility & bands, Oscillators & momentum, Mean reversion

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Lengthint20length
Sourcesourceclosesrc
How many candles should the stop loss be placed ahead? Lowest price.int5lowest
盈亏比float1.5RRR
BB Standard Deviationfloat2.0B2mult
periodint25ilong
signalint3isig
useTrueRangebooltrueuseTrueRange
Keltner Rangefloat1.5Kmult
Bollinger Band Crossover ConditionstringBothbb_condition
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

330 stars

Alert BB + RSI [Alorse] is a multi-symbol mean-reversion strategy designed for cryptocurrency trading. It evaluates Bollinger Bands and the Relative Strength Index (RSI) across up to 20 user-defined ticker symbols simultaneously. The strategy enters long posit

Strategy v4 Oscillators & momentumVolatility & bands
Updated 2 months ago · None stated GitHub repository of Alert BB + RSI [Alorse]
106 stars

A multi-timeframe Dollar Cost Averaging (DCA) trading strategy that combines signals from three RSI indicators, three CCI indicators, and Bollinger Bands. It places up to five weighted scale-in safety orders to lower the average entry price. The script offers

Strategy v5 Oscillators & momentumVolatility & bands
Updated 2 years ago · GPL-3.0 GitHub repository of 3RSI 3CCI BB 5orders DCA Strategy+
104 stars

This strategy combines Bollinger Bands with a short RSI to fade moves that leave the bands and then return. A long requires the prior bar below the lower band and oversold, then a close back inside the band while RSI is between 30 and 50. Shorts mirror that at

Strategy v5 Oscillators & momentumVolatility & bands
Updated 3 years ago · MIT GitHub repository of Bollinger Bands and RSI Strategy

Greeffer RSI Strategy

by SammyEnigma
18 stars

Greeffer RSI Strategy is a trading strategy that generates buy and sell entries based on Bollinger Bands and RSI levels. It places stop orders at upper and lower Bollinger Bands on band crossovers, and adds positions when RSI reaches extreme levels. Users can

Strategy v4 Oscillators & momentumVolatility & bands
Updated 6 years ago · None stated GitHub repository of Greeffer RSI Strategy
13 stars

This strategy overlays the price chart to trade DAX futures during European market hours. It calculates exponential and simple moving averages, RSI, MACD, Bollinger Bands, ATR, and realized volatility to simulate VIX levels. Trades execute when session time fi

Strategy v5 Sessions & timeOscillators & momentum
Updated 8 months ago · Custom licence GitHub repository of ClaudeHedge - DAX Session Strategy
7 stars

This strategy plots a simple moving average basis and upper and lower Bollinger bands on the price chart, with a fill between the bands. It also calculates RSI. It opens one long trade when RSI is above 25, price is at or below the lower band, and no position

Strategy v5 Volatility & bandsOscillators & momentum
Updated 4 years ago · GPL-3.0 GitHub repository of Bollinger Bands Strategy

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers