本页源码来自 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/
// © TrendX_
//@version=5
//
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// ████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░
// ████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░
// ████████████████████████████████████████░░░░░░░░░░░░░░░░░░
// ████████████████████████████████████████░░░░░░░░░░░░░░ ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ████████████████████████████████████████░░░░░░░░░░ ┃ ┃
// ████████████████████████████████████████░░░░░░ ┃ ████████╗██████╗ ███████╗███╗ ██╗██████╗ ██╗ ██╗ ┃
// ████████████████████████████████████████░░ ┃ ╚══██╔══╝██╔══██╗██╔════╝████╗██║██╔══██╗ ██╗██╔╝ ┃
// ████████████████████████████████████████ ┃ ██║ ██████╔╝███████╗███████║██║ ██║ ███╔╝ ┃
// ████████████████████████████████████████░░ ┃ ██║ ██╔══██╗██╔════╝██╔████║██║ ██║ ██╔██╗ ┃
// ████████████████████████████████████████░░░░░░ ┃ ██║ ██║ ██║███████╗██║ ███║██████╔╝██╔╝ ██╗ ┃
// ████████████████████████████████████████░░░░░░░░░░ ┃ ╚═╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═════╝ ╚═╝ ╚═╝ ┃
// ████████████████████████████████████████░░░░░░░░░░░░░░ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
// ████████████████████████████████████████░░░░░░░░░░░░░░░░░░
// ████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░
// ████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░
// ████████████████████████████████████████
indicator("RSI Divergence + Sweep + Signal + Alerts Toolkit [TrendX_]", "RSI Toolkit [TrendX]", false, max_labels_count = 500, max_lines_count = 500)
// ╔══════════════════════════════════════════════════════════════════════════════════════════╗
// ║ ║
// ║ INPUT ║
// ║ ║
// ╚══════════════════════════════════════════════════════════════════════════════════════════╝
// Grouping {
space = ""
groupSetting = "RSI Setting"
groupMA = "▶ Moving Average"
groupDiv = "▶ Divergence"
groupSweep = "▶ Sweep"
//_____________________________________________________________________________________}
// Initial Setting {
rsiSrc = input.source(hlc3 , "Source" , inline = "1", group = groupSetting)
rsiLen = input.int(14 , "Length" , inline = "2", group = groupSetting)
bullCss = input.color(color.green, "" , inline = "1", group = groupSetting)
bearCss = input.color(color.red , "" , inline = "1", group = groupSetting)
showMA = input.bool(true , "Show MA" , inline = "3", group = groupSetting)
showDiv = input.bool(true , "Show Divergence", inline = "4", group = groupSetting)
showSweep = input.bool(true , "Show RSI Sweep" , inline = "5", group = groupSetting)
showOverlays = input.bool(true , "Overlays" , inline = "6", group = groupSetting)
//_____________________________________________________________________________________}
// Initial Setting {
matype = input.string("SMA" , "Type" , inline = "1", group = groupMA, options = ["SMA", "EMA", "RMA", "VWMA"])
maLen = input.int(14 , "Length" , inline = "2", group = groupMA)
//_____________________________________________________________________________________}
// Initial Setting {
divtype = input.string("Both", "Type" , inline = "1", group = groupDiv, options = ["Regular Divergence", "Hidden Divergence", "Both"])
lookbackLeft = input.int(14 , "Pivot" , inline = "2", group = groupDiv)
lookbackRight = input.int(1 , "" , inline = "2", group = groupDiv, tooltip = "Set the Pivot length for left and right lookback to detect divergences")
oblevel = input.int(70 , "OB" , inline = "3", group = groupDiv)
oslevel = input.int(30 , "OS" , inline = "3", group = groupDiv)
useobos = input.bool(false , "" , inline = "3", group = groupDiv, tooltip = "Available only Divegences above OB and below OS levels")
//_____________________________________________________________________________________}
// Initial Setting {
srcSweep = input.source(close , "RSI Sweep" , inline = "1", group = groupSweep, tooltip = "")
showSweep_all = input.bool(false , "" , inline = "1", group = groupSweep, tooltip = "Show all potential RSI Sweeps, including unfinished RSI Sweeps")
Momentum_lvl = input.int(50 , "Momentum Threshold" , inline = "2", group = groupSweep, tooltip = "Set the Zone for RSI to Retest")
//_____________________________________________________________________________________}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ╔══════════════════════════════════════════════════════════════════════════════════════════╗
// ║ ║
// ║ FUNCTION ║
// ║ ║
// ╚══════════════════════════════════════════════════════════════════════════════════════════╝
//Functions {
ma_Func(src, len, MAtype) =>
switch MAtype
"SMA" => ta.sma(src, len)
"EMA" => ta.ema(src, len)
"RMA" => ta.rma(src, len)
"VWMA" => ta.vwma(src, len)
div_Func(src, topLimit, botLimit, useLimits) =>
pH = not na(ta.pivothigh(src, lookbackLeft, lookbackRight)) and (useLimits ? src[2] >= topLimit : true) ? src[2] : na
pL = not na(ta.pivotlow(src, lookbackLeft, lookbackRight)) and (useLimits ? src[2] <= botLimit : true) ? src[2] : na
pre_rpH = ta.valuewhen(pH, src[2], 0)[2]
pre_cpH = ta.valuewhen(pH, high[2], 0)[2]
pre_rpL = ta.valuewhen(pL, src[2], 0)[2]
pre_cpL = ta.valuewhen(pL, low[2], 0)[2]
bearDiv = pH and high[2] > pre_cpH and src[2] < pre_rpH
bullDiv = pL and low[2] < pre_cpL and src[2] > pre_rpL
bearDivHidden = pH and high[2] < pre_cpH and src[2] > pre_rpH
bullDivHidden = pL and low[2] > pre_cpL and src[2] < pre_rpL
[pH, pL, bearDiv, bullDiv, bearDivHidden, bullDivHidden]
lbl_Func(y, txt, styleCond, tip, overlays) =>
label.new(bar_index,
y,
txt,
xloc.bar_index,
yloc.price,
styleCond == "bull" ? color.green : color.red,
styleCond == "bull" ? label.style_label_up : label.style_label_down,
color.white,
size.tiny,
tooltip = tip,
force_overlay = overlays)
//_____________________________________________________________________________________}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ╔══════════════════════════════════════════════════════════════════════════════════════════╗
// ║ ║
// ║ CALCULATION ║
// ║ ║
// ╚══════════════════════════════════════════════════════════════════════════════════════════╝
// RSI {
rsi = ta.rsi(rsiSrc, rsiLen)
rsima = ma_Func(rsi, maLen, matype)
//_____________________________________________________________________________________}
// Divergence {
[bearDiv_line, bullDiv_line, bearDiv, bullDiv, bearDiv_Hidden, bullDiv_Hidden] = div_Func(rsi, oblevel, oslevel, useobos)
//_____________________________________________________________________________________}
// RSI {
var float x_bull_val = 0
var int x_bull_bar = 0
var bool x_bull = false
var bool x_bull1 = false
var line x_bull_line = na
var label x_bull_label = na
var label x_bull_label_rsi = na
var float x_bear_val = 0
var int x_bear_bar = 0
var bool x_bear = false
var bool x_bear1 = false
var line x_bear_line = na
var label x_bear_label = na
var label x_bear_label_rsi = na
if ta.crossover(rsi, Momentum_lvl)
x_bull_val := srcSweep
x_bull_bar := bar_index
x_bull1 := true
if ta.crossunder(low, x_bull_val) and close > x_bull_val and x_bull1
x_bull := true
x_bull1 := false
else if ta.crossunder(close, x_bull_val)
x_bull := false
x_bull1 := false
else
x_bull := false
if ta.crossunder(rsi, Momentum_lvl)
x_bear_val := srcSweep
x_bear_bar := bar_index
x_bear1 := true
if ta.crossover(high, x_bear_val) and close < x_bear_val and x_bear1
x_bear := true
x_bear1 := false
else if ta.crossover(close, x_bear_val)
x_bear := false
x_bear1 := false
else
x_bear := false
//_____________________________________________________________________________________}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ╔══════════════════════════════════════════════════════════════════════════════════════════╗
// ║ ║
// ║ DISPLAY ║
// ║ ║
// ╚══════════════════════════════════════════════════════════════════════════════════════════╝
// RSI {
rsiPlot = plot(rsi, "RSI", color.rgb(126, 87, 194))
rsimaPlot = plot(showMA ? rsima : na, "RSIMA", na, editable = false)
fill(rsiPlot, rsimaPlot, color=color.rgb(126, 87, 194, 80), title="RSI MA Fill", editable = false)
//_____________________________________________________________________________________}
// Divergence {
_size = size.tiny
_txtcss = color.white
divlineColor = chart.fg_color
hiddenDivlineColor = chart.fg_color
bearDivColor = (divtype == "Both" ? bearDiv or bearDiv_Hidden : divtype == "Regular Divergence" ? bearDiv : bearDiv_Hidden) ? divlineColor : na
bullDivColor = (divtype == "Both" ? bullDiv or bullDiv_Hidden : divtype == "Regular Divergence" ? bullDiv : bullDiv_Hidden) ? divlineColor : na
beardivPlot = plot(bearDiv_line ? rsi : na, color = bearDivColor, editable = false)
bulldivPlot = plot(bullDiv_line ? rsi : na, color = bullDivColor, editable = false)
midline = hline(Momentum_lvl, "RSI Middle Band", color=chart.fg_color, editable = false)
rsiUpperBand = hline(oblevel , "RSI Over", color=#787B86, editable = false)
rsiLowerBand = hline(oslevel , "RSI Lower Band", color=#787B86, editable = false)
midLinePlot = plot(Momentum_lvl , color = na, editable = false, display = display.none)
rsiUpperPlot = plot(oblevel , "RSI Over", color=#787B86, editable = false, display = display.none)
rsiLowerPlot = plot(oslevel , "RSI Lower Band", color=#787B86, editable = false, display = display.none)
fill(rsiPlot , rsiUpperPlot , 100 , oblevel, color.new(bullCss, 25), color.new(bullCss, 100), "OB Gradient Fill")
fill(rsiPlot , rsiLowerPlot , oslevel , 0 , color.new(bearCss, 100), color.new(bearCss, 25), "OS Gradient Fill")
R_tip = "Regular \nDivergence"
H_tip = "Hidden \nDivergence"
if showDiv
if bearDiv_line and bearDiv and (divtype == "Regular Divergence" or divtype == "Both")
lbl_Func(rsi, "R", "bear", R_tip, false)
if showOverlays
lbl_Func(high, "R", "bear", R_tip, true)
if bullDiv_line and bullDiv and (divtype == "Regular Divergence" or divtype == "Both")
lbl_Func(rsi, "R", "bull", R_tip, false)
if showOverlays
lbl_Func(low, "R", "bull", R_tip, true)
if bearDiv_line and bearDiv_Hidden and (divtype == "Hidden Divergence" or divtype == "Both")
lbl_Func(rsi, "H", "bear", H_tip, false)
if showOverlays
lbl_Func(high, "H", "bear", H_tip, true)
if bullDiv_line and bullDiv_Hidden and (divtype == "Hidden Divergence" or divtype == "Both")
lbl_Func(rsi, "H", "bull", H_tip, false)
if showOverlays
lbl_Func(low, "H", "bull", H_tip, true)
//_____________________________________________________________________________________}
// Sweep {
if showSweep
if showSweep_all
if x_bull1
x_bull_line := line.new(x_bull_bar, x_bull_val, bar_index, x_bull_val, xloc.bar_index, extend.none, chart.fg_color, line.style_dotted, 1, true)
if x_bull
x_bull_line := line.new(x_bull_bar, x_bull_val, bar_index, x_bull_val, xloc.bar_index, extend.none, chart.fg_color, line.style_solid, 1, true)
x_bull_label := label.new(bar_index, x_bull_val, "x", xloc.bar_index, yloc.belowbar, na, label.style_label_up, chart.fg_color, size.small, tooltip = "RSI Sweep", force_overlay = true)
x_bull_label_rsi := label.new(bar_index, rsi+1, "x", xloc.bar_index, yloc.price, na, label.style_label_up, bullCss, size.normal, tooltip = "RSI Sweep")
if showSweep
if showSweep_all
if x_bear1
x_bear_line := line.new(x_bear_bar, x_bear_val, bar_index, x_bear_val, xloc.bar_index, extend.none, chart.fg_color, line.style_dotted, 1, true)
if x_bear
x_bear_line := line.new(x_bear_bar, x_bear_val, bar_index, x_bear_val, xloc.bar_index, extend.none, chart.fg_color, line.style_solid, 1, true)
x_bear_label := label.new(bar_index, x_bear_val, "x", xloc.bar_index, yloc.abovebar, na, label.style_label_down, chart.fg_color, size.small, tooltip = "RSI Sweep", force_overlay = true)
x_bear_label_rsi := label.new(bar_index, rsi-1, "x", xloc.bar_index, yloc.price, na, label.style_label_down, bearCss, size.normal, tooltip = "RSI Sweep")
//_____________________________________________________________________________________}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ╔══════════════════════════════════════════════════════════════════════════════════════════╗
// ║ ║
// ║ ALERTS ║
// ║ ║
// ╚══════════════════════════════════════════════════════════════════════════════════════════╝
// MA {
alertcondition(ta.crossover(rsi, rsima) , "RSI crossover Moving Average of RSI" , "")
alertcondition(ta.crossunder(rsi, rsima), "RSI crossunder Moving Average of RSI", "")
//_____________________________________________________________________________________}
// Divergence {
alertcondition(bullDiv , "RSI Bullish Divergence" , "")
alertcondition(bearDiv , "RSI Bearish Divergence" , "")
alertcondition(bullDiv_Hidden, "RSI Bullish Divergence (Hidden)", "")
alertcondition(bearDiv_Hidden, "RSI Bearish Divergence (Hidden)", "")
//_____________________________________________________________________________________}
// Retest {
alertcondition(x_bull, "RSI Bullish Sweep", "")
alertcondition(x_bear, "RSI Bearish Sweep", "")
//_____________________________________________________________________________________}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////