本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
indicator(title='Divergence Macd+RSI Fast[RSU]', max_bars_back=2000, overlay=true)
//-----------
//rsi function
//-----------
//A small amount of smoothing to reduce false divergence hints
rsi = ta.ema(ta.rsi(close, 14), 2)
findRSITopBl(len) =>
top = ta.pivothigh(rsi, len, len)
topc = 0
topc := top ? len : nz(topc[1]) + 1
newtop = ta.pivothigh(rsi, len, 0)
topBL_z = newtop[1] and not newtop and high[1] > high[topc] and rsi[1] < rsi[topc] and rsi[topc] > 60
topBL_h = newtop[1] and not newtop and high[1] < high[topc] and rsi[1] > rsi[topc] and rsi[topc] > 60
BLTopc = 0
if topBL_z or topBL_h
BLTopc := topc
BLTopc
findRSIBotBl(len) =>
bot = ta.pivotlow(rsi, len, len)
botc = 0
botc := bot ? len : nz(botc[1]) + 1
newbot = ta.pivotlow(rsi, len, 0)
BotBL_z = newbot[1] and not newbot and low[1] < low[botc] and rsi[1] > rsi[botc]
BotBL_h = newbot[1] and not newbot and low[1] > low[botc] and rsi[1] < rsi[botc]
BLBotc = 0
if BotBL_z or BotBL_h
BLBotc := botc
BLBotc
t5result = findRSITopBl(5)
t10result = findRSITopBl(10)
t20result = findRSITopBl(20)
b5result = findRSIBotBl(5)
b10result = findRSIBotBl(10)
b20result = findRSIBotBl(20)
//K not end information
string notend = ''
if timenow < time_close
notend := '\nk not end'
//
var rsitoplabels = array.new_label()
var rsibotlabels = array.new_label()
var macdtoplabels = array.new_label()
var macdbotlabels = array.new_label()
if t5result
array.push(rsitoplabels,label.new(x=bar_index, y=high, yloc=yloc.abovebar, text='r' + notend, textcolor=#ffffff, color=color.new(color.red, 40), size=size.tiny, style=label.style_label_down))
if t10result
array.push(rsitoplabels,label.new(x=bar_index, y=high, yloc=yloc.abovebar, text='r' + notend, textcolor=#ffffff, color=color.new(color.red, 40), size=size.tiny, style=label.style_label_down))
if t20result
array.push(rsitoplabels,label.new(x=bar_index, y=high, yloc=yloc.abovebar, text='r' + notend, textcolor=#ffffff, color=color.new(color.red, 40), size=size.tiny, style=label.style_label_down))
if b5result
array.push(rsibotlabels,label.new(x=bar_index, y=low, yloc=yloc.belowbar, text='r' + notend, textcolor=#ffffff, color=color.new(color.green, 40), size=size.tiny, style=label.style_label_up))
if b10result
array.push(rsibotlabels,label.new(x=bar_index, y=low, yloc=yloc.belowbar, text='r' + notend, textcolor=#ffffff, color=color.new(color.green, 40), size=size.tiny, style=label.style_label_up))
if b20result
array.push(rsibotlabels,label.new(x=bar_index, y=low, yloc=yloc.belowbar, text='r' + notend, textcolor=#ffffff, color=color.new(color.green, 40), size=size.tiny, style=label.style_label_up))
//recolor previous error divergence labels
if array.size(rsitoplabels)>=1
for i=0 to array.size(rsitoplabels)-1
templabel=array.get(rsitoplabels,i)
x1=label.get_x(templabel)
if bar_index-x1-1<4 and rsi>rsi[bar_index-x1+1]
label.set_color(templabel,color.new(color.gray,90))
label.set_textcolor(templabel,color.gray)
if array.size(rsibotlabels)>=1
for i=0 to array.size(rsibotlabels)-1
templabel=array.get(rsibotlabels,i)
x1=label.get_x(templabel)
if bar_index-x1-1<4 and rsi<rsi[bar_index-x1+1]
label.set_color(templabel,color.new(color.gray,90))
label.set_textcolor(templabel,color.gray)
//-----------
//macd function
//-----------
diff = ta.ema(close, 12) - ta.ema(close, 26)
dea = ta.ema(diff, 9)
macd = (diff - dea) * 2
findMACDTopBl(len)=>
src=diff
top= ta.pivothigh(src,len, len)
topc = 0
topc := top ? len : nz(topc[1]) + 1
newtop=ta.pivothigh(src,len,0)
topBL_z=newtop[1] and not newtop and high[1]>high[topc] and src[1]<src[topc] and diff[topc]>dea[topc] and src[topc]>0
topBL_h=newtop[1] and not newtop and high[1]<high[topc] and src[1]>src[topc] and diff[topc]>dea[topc] and src[topc]>0
BLTopc=0
if topBL_z or topBL_h
BLTopc:=topc
BLTopc
findMACDBotBl(len)=>
src=diff
bot= ta.pivotlow(src,len, len)
botc = 0
botc := bot ? len : nz(botc[1]) + 1
newbot=ta.pivotlow(src,len,0)
BotBL_z=newbot[1] and not newbot and low[1]<low[botc] and src[1]>src[botc] and diff[botc]<dea[botc] and src[botc]<0
BotBL_h=newbot[1] and not newbot and low[1]>low[botc] and src[1]<src[botc] and diff[botc]<dea[botc] and src[botc]<0
BLBotc=0
if BotBL_z or BotBL_h
BLBotc:=botc
BLBotc
//5,20,40,60
mt5result = findMACDTopBl(5)
mt10result = findMACDTopBl(10)
mt20result = findMACDTopBl(20)
mt40result = findMACDTopBl(40)
mt60result = findMACDTopBl(60)
mb5result = findMACDBotBl(5)
mb10result = findMACDBotBl(10)
mb20result = findMACDBotBl(20)
mb40result = findMACDBotBl(40)
mb60result = findMACDBotBl(60)
if mt5result
array.push(macdtoplabels,label.new(x=bar_index, y=high, yloc=yloc.abovebar, text='m' + notend, textcolor=#ffffff, color=color.new(color.red, 40), size=size.tiny, style=label.style_label_down))
if mt10result
array.push(macdtoplabels,label.new(x=bar_index, y=high, yloc=yloc.abovebar, text='m' + notend, textcolor=#ffffff, color=color.new(color.red, 40), size=size.tiny, style=label.style_label_down))
if mt20result
array.push(macdtoplabels,label.new(x=bar_index, y=high, yloc=yloc.abovebar, text='m' + notend, textcolor=#ffffff, color=color.new(color.red, 40), size=size.tiny, style=label.style_label_down))
if mt40result
array.push(macdtoplabels,label.new(x=bar_index, y=high, yloc=yloc.abovebar, text='m' + notend, textcolor=#ffffff, color=color.new(color.red, 40), size=size.tiny, style=label.style_label_down))
if mt60result
array.push(macdtoplabels,label.new(x=bar_index, y=high, yloc=yloc.abovebar, text='m' + notend, textcolor=#ffffff, color=color.new(color.red, 40), size=size.tiny, style=label.style_label_down))
if mb5result
array.push(macdbotlabels,label.new(x=bar_index, y=low, yloc=yloc.belowbar, text='m' + notend, textcolor=#ffffff, color=color.new(color.green, 40), size=size.tiny, style=label.style_label_up))
if mb10result
array.push(macdbotlabels,label.new(x=bar_index, y=low, yloc=yloc.belowbar, text='m' + notend, textcolor=#ffffff, color=color.new(color.green, 40), size=size.tiny, style=label.style_label_up))
if mb20result
array.push(macdbotlabels,label.new(x=bar_index, y=low, yloc=yloc.belowbar, text='m' + notend, textcolor=#ffffff, color=color.new(color.green, 40), size=size.tiny, style=label.style_label_up))
if mb40result
array.push(macdbotlabels,label.new(x=bar_index, y=low, yloc=yloc.belowbar, text='m' + notend, textcolor=#ffffff, color=color.new(color.green, 40), size=size.tiny, style=label.style_label_up))
if mb60result
array.push(macdbotlabels,label.new(x=bar_index, y=low, yloc=yloc.belowbar, text='m' + notend, textcolor=#ffffff, color=color.new(color.green, 40), size=size.tiny, style=label.style_label_up))
//recolor previous error divergence labels
if array.size(macdtoplabels)>=1
for i=0 to array.size(macdtoplabels)-1
templabel=array.get(macdtoplabels,i)
x1=label.get_x(templabel)
if bar_index-x1-1<4 and diff>diff[bar_index-x1]
label.set_color(templabel,color.new(color.gray,90))
label.set_textcolor(templabel,color.gray)
if array.size(macdbotlabels)>=1
for i=0 to array.size(macdbotlabels)-1
templabel=array.get(macdbotlabels,i)
x1=label.get_x(templabel)
if bar_index-x1-1<4 and diff<diff[bar_index-x1]
label.set_color(templabel,color.new(color.gray,90))
label.set_textcolor(templabel,color.gray)
//-----------
//ALert
//-----------
alertname = syminfo.ticker
periodDesc = ''
if timeframe.period == '30'
periodDesc := 'in 30Min '
periodDesc
else if timeframe.period == '60'
periodDesc := 'in 1Hour '
periodDesc
else if timeframe.period == '120'
periodDesc := 'in 2Hour '
periodDesc
else if timeframe.period == '240'
periodDesc := 'in 4Hour '
periodDesc
else if timeframe.period == 'D'
periodDesc := 'in Day '
periodDesc
else if timeframe.period == 'W'
periodDesc := 'in Week '
periodDesc
else
periodDesc := 'in ' + timeframe.period + 'period '
periodDesc
alertname += periodDesc
topbl = t5result or t10result or t20result or mt5result or mt20result or mt40result or mt60result
botbl = b5result or b10result or b20result or mb5result or mb20result or mb40result or mb60result
if topbl
alert(alertname + ' TOP Divergence!', alert.freq_once_per_bar_close)
if botbl
alert(alertname + ' Bottom Divergence!', alert.freq_once_per_bar_close)