Bollinger Bands %b & RSI & Stochastic Smoothed Indicator & Alert

Zamboniman · study · 95 行 · 点赞 2,427 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

study(shorttitle="BB %b - RSI - Stoch", title="Bollinger Bands %b & RSI & Stochastic Smoothed Indicator & Alert", overlay=false)
 
//bb %b setup
bbrperiod = input(20, minval=1)
mult = input(2.0, minval=0.001, maxval=50)
 
//rsi setup
rsiperiod = input(14, minval=1)
rsiob = input(70, minval=0, maxval=100)
rsios = input(30, minval=0, maxval=100)
 
//Stochastic Smoothed setup
usestochastic = input(true,type=bool)
stochperiod = input(14, minval=1)
stochob = input(86, minval=0, maxval=100)
stochsmoothing = input(5, minval=1)
src = input(close, title="Source")
 
//Draw Arrows?
drawbuyarrow = input(true,type=bool)
drawsellarrow = input(true,type=bool)
 
//calculate bbr %b
basis = sma(src, bbrperiod)
dev = mult * stdev(src, bbrperiod)
upper = basis + dev
lower = basis - dev
 
bbr = (src - lower)/(upper - lower)
 
//normalize the plotting of the BB %b indicator line to fit RSI at 70 OB / 30 OS
a = (rsiob - rsios)
b = rsiob - a
normbbr = (a*bbr+b)
 
 
plot(normbbr, title = 'BB %b', color=blue, linewidth=2)
 
//calculate rsi
rsi = rsi(close, rsiperiod)
 
plot(rsi, title = 'RSI', color=red, linewidth=2)
 
 
color(purple, 30)
band1 = hline(rsiob, linestyle=dotted, linewidth=1, color=purple)
band2 = hline(rsios, linestyle=dotted, linewidth=1, color=purple)
fill(band1, band2)
 
 
//Calculate Stochastic smoothed
high = highest(close, stochperiod)
low=lowest(close,stochperiod)
stoch = (close - low) / (high - low)
stochsmoothed = 100 * sma(stoch, stochsmoothing)
 
//normalize Stochastic smoothed
c = ((rsiob + 10) - (rsios - 10)) / 100
d = (rsiob + 10) - c * 100
normstoch = (c * stochsmoothed + d)
 
plot(normstoch, title = 'normstoch', color=orange, linewidth=2)
 
 
//Buy or Sell indicators
 
 
//CheckConditionBuy = interval <= 60 and rsi < rsios and close < open and close < lower and rsi[1] < rsios and close[1] < open[1] and close[1] < lower[1]
CheckConditionBuy = interval <= 60 and rsi < rsios and close < open and close < lower and rsi[1] < rsios and close[1] < open[1]
CheckConditionSell = usestochastic ? interval <= 60 and close > open and close > upper and stochsmoothed >= stochob : interval <= 60 and rsi > rsiob and close > open and close > upper and rsi[1] > rsiob and close[1] > open[1] and close[1] > upper[1]
indicator = close > upper and CheckConditionSell ? -1 : rsi < rsios and (close < lower) and CheckConditionBuy ? 1 : 0
 
//Color in buy/sell indicator column
 
fillcolumnbuy = (drawbuyarrow and indicator == 1)
fillcolumnsell = (drawsellarrow and indicator == -1)
bgcolor(fillcolumnbuy ? aqua : na, transp=50)
bgcolor(fillcolumnsell ? aqua : na, transp=50)
 
uparrow = indicator == 1 and drawbuyarrow
downarrow = indicator == -1 and drawsellarrow
 
 
plotchar(uparrow, char = '▲', title = 'BuyArrow', location = location.top, color = green, text = 'BUY', textcolor = green)
plotchar(downarrow, char = '▼', title = 'SellArrow', location = location.bottom, color = red, text = 'SELL', textcolor = red)
 
 
// combination alert for either crossing
alertcondition(indicator != 0, title = "Crossing BB %b - RSI", message = "BB %b + RSI crossed")
 
// alert for buy
alertcondition(indicator == 1, title = "BB & RSI below threshhold - buy", message = "Below BB %b + RSI -- Buy")
 
// alert for sell
alertcondition(indicator == -1, title = "BB & RSI above threshhold - sell", message = "Above BB %b + RSI -- Sell")

← 返回列表