本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AlgoAlpha
//@version=5
indicator("Supertrended RSI [AlgoAlpha]", overlay = false, timeframe = "")
// Input parameters
relativeStrengthIndexLength = input.int(14, title="RSI Length", group="RSI Settings")
smoothingLength = input.int(21, title="RSI Smoothing Length", group="RSI Settings")
rsiInputSource = input.source(close, title="RSI Source", group="RSI Settings")
isSmoothed = input.bool(false, "Smooth RSI?", group="RSI Settings")
movingAverageLength = input.int(14, title="MA Length", group="MA Settings", display = display.data_window)
movingAverageType = input.string("HMA", title="MA Type", options=["SMA", "HMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings", display = display.data_window)
showMovingAverage = input.bool(true, "Show RSI MA?", group="MA Settings")
trendFactor = input.float(0.8, title="Factor", group="Super Trend Settings")
averageTrueRangeLength = input.int(10, title="ATR Length", group="Super Trend Settings")
colorUp = input.color(#00ffbb, title="Up Color")
colorDown = input.color(#ff1100, title="Down Color")
// MA Selection
calculateMA(source, length, type) =>
switch type
"SMA" => ta.sma(source, length)
"HMA" => ta.hma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)
// Function to calculate ATR
calculateATR(source, atrLength) =>
highestHigh = ta.highest(source, atrLength)
lowestLow = ta.lowest(source, atrLength)
trueRange = na(highestHigh[1]) ? highestHigh - lowestLow : math.max(highestHigh - lowestLow, math.abs(highestHigh - source[1]), math.abs(lowestLow - source[1]))
ta.rma(trueRange, atrLength)
// Function to calculate Supertrend
calculateSupertrend(factor, atrPeriod, source) =>
priceSource = source
atr = calculateATR(source, atrPeriod)
upperBand = priceSource + factor * atr
lowerBand = priceSource - factor * atr
prevLowerBand = nz(lowerBand[1])
prevUpperBand = nz(upperBand[1])
lowerBand := lowerBand > prevLowerBand or source[1] < prevLowerBand ? lowerBand : prevLowerBand
upperBand := upperBand < prevUpperBand or source[1] > prevUpperBand ? upperBand : prevUpperBand
int trendDirection = na
float supertrendValue = na
prevSupertrend = supertrendValue[1]
if na(atr[1])
trendDirection := 1
else if prevSupertrend == prevUpperBand
trendDirection := source > upperBand ? -1 : 1
else
trendDirection := source < lowerBand ? 1 : -1
supertrendValue := trendDirection == -1 ? lowerBand : upperBand
[supertrendValue, trendDirection]
// Calculating RSI
rsiValue = isSmoothed ? ta.hma(ta.rsi(rsiInputSource, relativeStrengthIndexLength), smoothingLength) : ta.rsi(rsiInputSource, relativeStrengthIndexLength)
rsiMovingAverage = calculateMA(rsiValue, movingAverageLength, movingAverageType)
// Calculating Supertrend based on RSI values
[rsiSupertrend, trendDirection] = calculateSupertrend(trendFactor, averageTrueRangeLength, rsiValue)
// Plotting
supertrendPlot = plot(rsiSupertrend, color = trendDirection == -1 ? colorUp : colorDown, title="Supertrend")
rsiPlot = plot(rsiValue, color = color.new(color.gray, 100), title="RSI")
overboughtLine = plot(70, color = color.from_gradient(rsiValue, 50, 70, color.new(color.gray, 70), color.new(colorDown, 0)), title="Overbought Line", linewidth = 3)
oversoldLine = plot(30, color = color.from_gradient(rsiValue, 30, 50, color.new(colorUp, 0), color.new(color.gray, 70)), title="Oversold Line", linewidth = 3)
plot(rsiMovingAverage, color = color.gray, title = "RSI MA", display = showMovingAverage ? display.all : display.none, linewidth = 1)
plot(50, color = color.gray, title="Mid Line")
// Filling
fill(overboughtLine, oversoldLine, color = color.new(color.gray, 90), title="Overbought/Oversold Fill")
fill(supertrendPlot, rsiPlot, color = trendDirection == 1 ? color.new(colorDown, 90) : color.new(colorUp, 90), title="Trend Fill")
fill(supertrendPlot, rsiPlot, rsiValue, rsiSupertrend, color.new(chart.bg_color, 1000), trendDirection == 1 ? colorDown : colorUp)
// Char plotting for crossover and crossunder
plotshape(ta.crossover(rsiSupertrend, rsiValue) and rsiSupertrend > 70 ? 85 : na, style=shape.triangledown, location=location.absolute, color=colorDown, size=size.tiny, title="Crossover Down")
plotshape(ta.crossunder(rsiSupertrend, rsiValue) and rsiSupertrend < 30 ? 15 : na, style=shape.triangleup, location=location.absolute, color=colorUp, size=size.tiny, title="Crossunder Up")
//Alerts
alertcondition(ta.crossover(rsiValue, rsiSupertrend), "RSI Bullish Supertrend")
alertcondition(ta.crossunder(rsiValue, rsiSupertrend), "RSI Bearish Supertrend")
alertcondition(ta.crossover(rsiSupertrend, rsiValue) and rsiSupertrend > 70, "RSI Bullish Reversal")
alertcondition(ta.crossunder(rsiSupertrend, rsiValue) and rsiSupertrend < 30, "RSI Bearish Reversal")