本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("AE_MTaskRSI v3", shorttitle="AE_MTaskRSI v3", overlay=false)
// RSI SETTINGS
group_rsi = "RSI"
rsiSource = input.source(close, "Source", group=group_rsi)
rsiLength = input.int(14, "RSI Length", minval=1, group=group_rsi)
rsiColor = input.color(color.aqua, "RSI Color", group=group_rsi)
rsiWidth = input.int(2, "RSI Width", minval=1, maxval=5, group=group_rsi)
// BOLLINGER BANDS
groupBB = "RSI Bollinger Bands"
showBB = input.bool(false, "Show BB", group=groupBB)
bbLength = input.int(20, "BB Length", minval=1, group=groupBB)
bbMult = input.float(2.0, "BB StdDev", minval=0.1, group=groupBB)
// HTF RSI SETTINGS
group_htf = "HTF RSI"
showHTF = input.bool(true, "Show HTF RSI", group=group_htf)
htfTF = input.timeframe("60", "HTF Timeframe", group=group_htf)
htfLength = input.int(14, "HTF RSI Length", minval=1, group=group_htf)
htfColor = input.color(color.orange, "HTF RSI Color", group=group_htf)
htfWidth = input.int(2, "HTF RSI Width", minval=1, maxval=5, group=group_htf)
// HTF BUY / SELL LABELS
group_htf_signals = "HTF Buy / Sell Labels"
showHTFSignals = input.bool(true, "Show HTF B / S", group=group_htf_signals)
hideHTFSignalHistory = input.bool(false, "Hide HTF B / S History", group=group_htf_signals)
showLastHTFSignals = input.int(20, "Show Last X HTF B / S", minval=1, group=group_htf_signals)
htfBuyLabelColor = input.color(color.lime, "HT + Label Color", group=group_htf_signals)
htfSellLabelColor = input.color(color.red, "HT - Label Color", group=group_htf_signals)
htfLabelTextColor = input.color(color.white, "HT Label Text Color", group=group_htf_signals)
htfLabelSizeInput = input.string(
"Small",
"HT Label Size",
options=["Tiny","Small","Normal","Large","Huge"],
group=group_htf_signals)
// REGRESSION CHANNEL VARS
var line upperRegLine = na
var line midRegLine = na
var line lowerRegLine = na
// REGRESSION CHANNEL
group_channel = "Regression Channel"
showRegressionChannel = input.bool(
true,
"Show Regression Channel",
group = group_channel)
regressionLength = input.int(
100,
"Regression Length",
minval = 10,
group = group_channel)
regressionDeviation = input.float(
2.0,
"Channel Deviation",
minval = 0.1,
step = 0.1,
group = group_channel)
showMidChannel = input.bool(
true,
"Show Mid Channel",
group = group_channel)
channelWidth = input.int(
1,
"Channel Width",
minval = 1,
maxval = 5,
group = group_channel)
// EMA SETTINGS
group_ema = "RSI EMAs"
showFastEMA = input.bool(true, "Show Fast EMA", group=group_ema)
fastLen = input.int(9, "Fast EMA Length", minval=1, group=group_ema)
fastColor = input.color(color.lime, "Fast EMA Color", group=group_ema)
showSlowEMA = input.bool(true, "Show Slow EMA", group=group_ema)
slowLen = input.int(21, "Slow EMA Length", minval=1, group=group_ema)
slowColor = input.color(color.red, "Slow EMA Color", group=group_ema)
// RSI LEVELS
group_levels = "RSI Levels"
upperLevel = input.float(70.0, "Upper Level", group=group_levels)
upperLevel2 = input.float(90.0, "Upper Level", group=group_levels)
midLevel = input.float(50.0, "Middle Level", group=group_levels)
lowerLevel = input.float(30.0, "Lower Level", group=group_levels)
lowerLevel2 = input.float(10.0, "Lower Level", group=group_levels)
levelColor = input.color(color.gray, "Level Color", group=group_levels)
levelWidth = input.int(1, "Level Width", minval=1, maxval=5, group=group_levels)
showMidUpperLower = input.bool(true, "Show MidUpper / MidLower", group=group_levels)
midUpperLevel = input.float(55.0, "MidUpper Level", group=group_levels)
midLowerLevel = input.float(45.0, "MidLower Level", group=group_levels)
midUpperLowerColor = input.color(color.yellow, "MidUpper / MidLower Color", group=group_levels)
midUpperLowerWidth = input.int(1, "MidUpper / MidLower Width", minval=1, maxval=5, group=group_levels)
// STRUCTURE SETTINGS
group_structure = "Structure"
showStructure = input.bool(true, "Show HH/LH/HL/LL", group=group_structure)
pivotLeft = input.int(5, "Pivot Left", minval=1, group=group_structure)
pivotRight = input.int(5, "Pivot Right", minval=1, group=group_structure)
labelSizeInput = input.string(
"Small",
"Label Size",
options=["Tiny","Small","Normal"],
group=group_structure)
labelTransparency = input.int(
0,
"Label Transparency",
minval=0,
maxval=100,
group=group_structure)
labelOffset = input.int(
0,
"Label Offset",
minval=-20,
maxval=20,
group=group_structure)
highLabelOffset = input.float(
4.0,
"High Label Vertical Offset",
step=0.5,
group=group_structure)
lowLabelOffset = input.float(
8.0,
"Low Label Vertical Offset",
step=0.5,
group=group_structure)
// DIVERGENCE
group_div = "Divergence"
divLabelOffset = input.float(
8.0,
"Divergence Label Offset",
minval=0,
step=0.5,
group=group_div)
showDivergence = input.bool(
true,
"Show Divergence",
group=group_div)
showDivLines = input.bool(
true,
"Show Divergence Lines",
group=group_div)
showDivLabels = input.bool(
true,
"Show BD/SD Labels",
group=group_div)
bullDivColor = input.color(
color.lime,
"BD Color",
group=group_div)
bearDivColor = input.color(
color.red,
"SD Color",
group=group_div)
divLineWidth = input.int(
1,
"Line Width",
minval=1,
maxval=5,
group=group_div)
// CALCULATIONS
rsi = ta.rsi(rsiSource, rsiLength)
htfRSI = request.security(
syminfo.tickerid,
htfTF,
ta.rsi(rsiSource, htfLength))
fastEMA = ta.ema(rsi, fastLen)
slowEMA = ta.ema(rsi, slowLen)
htfBuySignal = ta.crossunder(htfRSI, rsi)
htfSellSignal = ta.crossover(htfRSI, rsi)
// RSI BOLLINGER BANDS
bbBasis = ta.sma(rsi, bbLength)
bbDev = bbMult * ta.stdev(rsi, bbLength)
bbUpper = bbBasis + bbDev
bbLower = bbBasis - bbDev
showBBBasis = input.bool(false, "Show BB Basis", group=groupBB)
// STRUCTURE ENGINE
rsiPH = ta.pivothigh(rsi, pivotLeft, pivotRight)
rsiPL = ta.pivotlow(rsi, pivotLeft, pivotRight)
var float prevHigh = na
var float prevLow = na
hhFound = false
lhFound = false
hlFound = false
llFound = false
if not na(rsiPH)
hhFound := not na(prevHigh) and rsiPH > prevHigh
lhFound := not na(prevHigh) and rsiPH < prevHigh
prevHigh := rsiPH
if not na(rsiPL)
hlFound := not na(prevLow) and rsiPL > prevLow
llFound := not na(prevLow) and rsiPL < prevLow
prevLow := rsiPL
labelSize =
labelSizeInput == "Tiny" ? size.tiny :
labelSizeInput == "Small" ? size.small :
size.normal
htfLabelSize =
htfLabelSizeInput == "Tiny" ? size.tiny :
htfLabelSizeInput == "Small" ? size.small :
htfLabelSizeInput == "Normal" ? size.normal :
htfLabelSizeInput == "Large" ? size.large :
size.huge
// DIVERGENCE ENGINE
var float prevPriceLow = na
var float prevRsiLow = na
var float prevPriceHigh = na
var float prevRsiHigh = na
var int prevLowBar = na
var int prevHighBar = na
var float bullPrice1 = na
var float bullPrice2 = na
var float bearPrice1 = na
var float bearPrice2 = na
var int bullBar1 = na
var int bullBar2 = na
var int bearBar1 = na
var int bearBar2 = na
bullDiv = false
bearDiv = false
if not na(rsiPL)
currentPriceLow = low[pivotRight]
currentRsiLow = rsiPL
bullDiv :=
not na(prevPriceLow) and
not na(prevRsiLow) and
currentPriceLow < prevPriceLow and
currentRsiLow > prevRsiLow
if bullDiv
bullPrice1 := prevRsiLow
bullPrice2 := currentRsiLow
bullBar1 := prevLowBar
bullBar2 := bar_index - pivotRight
prevPriceLow := currentPriceLow
prevRsiLow := currentRsiLow
prevLowBar := bar_index - pivotRight
if not na(rsiPH)
currentPriceHigh = high[pivotRight]
currentRsiHigh = rsiPH
bearDiv :=
not na(prevPriceHigh) and
not na(prevRsiHigh) and
currentPriceHigh > prevPriceHigh and
currentRsiHigh < prevRsiHigh
if bearDiv
bearPrice1 := prevRsiHigh
bearPrice2 := currentRsiHigh
bearBar1 := prevHighBar
bearBar2 := bar_index - pivotRight
prevPriceHigh := currentPriceHigh
prevRsiHigh := currentRsiHigh
prevHighBar := bar_index - pivotRight
// LEVELS
hline(70, "Upper", color=levelColor, linestyle=hline.style_solid)
hline(50, "Middle", color=levelColor, linestyle=hline.style_dotted)
hline(30, "Lower", color=levelColor, linestyle=hline.style_solid)
hline(90, "ExUpper", color=levelColor, linestyle=hline.style_solid)
hline(10, "ExLower", color=levelColor, linestyle=hline.style_solid)
hline(midUpperLevel, "MidUpper", color=showMidUpperLower ? midUpperLowerColor : na, linestyle=hline.style_solid, linewidth=midUpperLowerWidth)
hline(midLowerLevel, "MidLower", color=showMidUpperLower ? midUpperLowerColor : na, linestyle=hline.style_solid, linewidth=midUpperLowerWidth)
// HTF BUY / SELL LABELS
var label[] htfSignalLabels = array.new_label()
addHTFSignalLabel(signalText, signalY, signalStyle, signalColor) =>
if showHTFSignals
if hideHTFSignalHistory
while array.size(htfSignalLabels) > 0
label.delete(array.shift(htfSignalLabels))
newLabel = label.new(
bar_index,
signalY,
signalText,
style=signalStyle,
textcolor=htfLabelTextColor,
color=signalColor,
size=htfLabelSize)
array.push(htfSignalLabels, newLabel)
maxLabels = hideHTFSignalHistory ? 1 : showLastHTFSignals
while array.size(htfSignalLabels) > maxLabels
label.delete(array.shift(htfSignalLabels))
if htfBuySignal
addHTFSignalLabel("HT +", rsi - 4.0, label.style_label_up, htfBuyLabelColor)
if htfSellSignal
addHTFSignalLabel("HT -", rsi + 4.0, label.style_label_down, htfSellLabelColor)
// STRUCTURE LABELS
if showStructure and hhFound
label.new(
bar_index - pivotRight + labelOffset,
rsiPH + highLabelOffset,
"HH",
style=label.style_none,
textcolor=color.new(color.gray, labelTransparency),
size=labelSize)
if showStructure and lhFound
label.new(
bar_index - pivotRight + labelOffset,
rsiPH + highLabelOffset,
"LH",
style=label.style_none,
textcolor=color.new(color.gray, labelTransparency),
size=labelSize)
if showStructure and hlFound
label.new(
bar_index - pivotRight + labelOffset,
rsiPL - lowLabelOffset,
"HL",
style=label.style_none,
textcolor=color.new(color.blue, labelTransparency),
size=labelSize)
if showStructure and llFound
label.new(
bar_index - pivotRight + labelOffset,
rsiPL - lowLabelOffset,
"LL",
style=label.style_none,
textcolor=color.new(color.green, labelTransparency),
size=labelSize)
// DIVERGENCE LABELS
if showDivergence and showDivLabels and bullDiv
label.new(
bar_index - pivotRight,
rsiPL - lowLabelOffset - divLabelOffset,
"BD",
style=label.style_none,
textcolor=bullDivColor,
size=size.small)
if showDivergence and showDivLabels and bearDiv
label.new(
bar_index - pivotRight,
rsiPH + highLabelOffset + divLabelOffset,
"SD",
style=label.style_none,
textcolor=bearDivColor,
size=size.small)
// DIVERGENCE LINES
if showDivergence and showDivLines and bullDiv
line.new(
bullBar1,
bullPrice1,
bullBar2,
bullPrice2,
color=bullDivColor,
width=divLineWidth)
if showDivergence and showDivLines and bearDiv
line.new(
bearBar1,
bearPrice1,
bearBar2,
bearPrice2,
color=bearDivColor,
width=divLineWidth)
// REGRESSION CHANNEL
periodMinusOne = regressionLength - 1
rsdcr2(PeriodMinusOne, Deviations) =>
period = PeriodMinusOne + 1
devDenominator = PeriodMinusOne
Ex = 0.0
Ex2 = 0.0
Exy = 0.0
Ey = 0.0
for i = 0 to PeriodMinusOne
rsiI = nz(rsi[i])
Ex += i
Ex2 += i * i
Exy += rsiI * i
Ey += rsiI
ExEx = Ex * Ex
slope =
Ex2 == ExEx ?
0.0 :
(period * Exy - Ex * Ey) /
(period * Ex2 - ExEx)
linearRegression =
(Ey - slope * Ex) /
period
intercept =
linearRegression +
bar_index * slope
deviation = 0.0
for i = 0 to PeriodMinusOne
deviation += math.pow(
nz(rsi[i]) -
(intercept - bar_index[i] * slope),
2.0)
deviation :=
regressionDeviation *
math.sqrt(deviation / devDenominator)
[linearRegression, slope, deviation]
[linReg, regSlope, regDeviation] =
rsdcr2(periodMinusOne, regressionDeviation)
endPointBar =
bar_index - regressionLength + 1
endPointY =
linReg + regSlope * periodMinusOne
// PLOTS
plot(rsi,
title="RSI",
color=rsiColor,
linewidth=rsiWidth)
plot(showHTF ? htfRSI : na,
title="HTF RSI",
color=htfColor,
linewidth=htfWidth)
plot(showFastEMA ? fastEMA : na,
title="Fast EMA",
color=fastColor,
linewidth=2)
plot(showSlowEMA ? slowEMA : na,
title="Slow EMA",
color=slowColor,
linewidth=2)
// BB PLOTS
plot(showBB and showBBBasis ? bbBasis : na,
"BB Basis",
color=color.gray)
pBB1 = plot(showBB ? bbUpper : na, "BB Upper", color=color.gray)
pBB2 = plot(showBB ? bbLower : na, "BB Lower", color=color.gray)
// REGRESSION CHANNEL DRAWING
if showRegressionChannel and not na(regSlope)
line.delete(upperRegLine)
line.delete(midRegLine)
line.delete(lowerRegLine)
startBar = bar_index - regressionLength + 1
endBar = bar_index
startMid = endPointY
endMid = linReg
startUpper = startMid + regDeviation
endUpper = endMid + regDeviation
startLower = startMid - regDeviation
endLower = endMid - regDeviation
upperRegLine := line.new(
startBar,
startUpper,
endBar,
endUpper,
extend=extend.right,
color=color.fuchsia,
width=channelWidth)
if showMidChannel
midRegLine := line.new(
startBar,
startMid,
endBar,
endMid,
extend=extend.right,
color=color.fuchsia,
width=1)
lowerRegLine := line.new(
startBar,
startLower,
endBar,
endLower,
extend=extend.right,
color=color.fuchsia,
width=channelWidth)