// This Pine Script® code is licensed under Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// © PineForge contributors 2026
//
// PF probe 31 — Bollinger bands wrapped around RSI itself
// Purpose: parity test for ta.sma + ta.stdev applied directly to ta.rsi
// (TA-on-TA chain) with band-cross + bar-direction confirm entries.
//
//@version=6
strategy("RSI with Bollinger Bands", overlay=false, initial_capital=1000000, process_orders_on_close=false, pyramiding=1, commission_type=strategy.commission.percent, commission_value=0, slippage=0, default_qty_type=strategy.fixed, default_qty_value=1)
// === Inputs ===
rsiLen = input.int(14, "RSI Length", minval=1)
bbLen = input.int(50, "BB Length on RSI", minval=5)
bbMult = input.float(2.0, "BB Mult", step=0.1)
// === RSI ===
rsiVal = ta.rsi(close, rsiLen)
// === Bollinger Bands on RSI itself ===
rsiBBMid = ta.sma(rsiVal, bbLen)
rsiDev = ta.stdev(rsiVal, bbLen) * bbMult
rsiBBUp = rsiBBMid + rsiDev
rsiBBDn = rsiBBMid - rsiDev
// === Signals: RSI crosses its own BB ===
longCond = rsiVal < rsiBBDn and rsiVal > rsiVal[1]
shortCond = rsiVal > rsiBBUp and rsiVal < rsiVal[1]
if longCond
strategy.entry("Long", strategy.long)
if shortCond
strategy.entry("Short", strategy.short)
// === Exit at RSI midline ===
if strategy.position_size > 0 and rsiVal > rsiBBMid
strategy.close("Long")
if strategy.position_size < 0 and rsiVal < rsiBBMid
strategy.close("Short")
plot(rsiVal, "RSI", color=color.blue, linewidth=2)
plot(rsiBBUp, "RSI BB Upper", color=color.red)
plot(rsiBBMid, "RSI BB Mid", color=color.gray)
plot(rsiBBDn, "RSI BB Lower", color=color.green)