Pine语言顶/底背离画图函数

Author: ianzeng123, Date: 2023-02-22 13:04:19
Tags:

顶背离:K线图上的价格走势一峰比一峰高,价格一直在向上涨,而MACD指标图形上的由红柱构成的图形的走势是一峰比一峰低,即当价格的高点比前一次的高点高、而MACD指标的高点比指标的前一次高点低,这叫顶背离现象。这是一个有“多”转“空”的参考信号。 底背离:K线图上的价格走势一峰比一峰低,价格一直在向下跌,而MACD指标图形上的由绿构成的图形的走势是一峰比一峰高,即当价格的低点比前一次的低点低、而MACD指标的低点比指标的前一次低点高,这叫底背离现象。这是一个有“空”转“多”的参考信号。

MACD指标

12日EMA的计算:EMA12 = 前一日EMA12 * 11/13 + 今日收盘 * 2/13 26日EMA的计算:EMA26 = 前一日EMA26 * 25/27 + 今日收盘 * 2/27 差离值(DIF)的计算: DIF = EMA12 - EMA26 9日DEA = 前一日DEA * 8/10 + 今日DIF * 2/10BAR=(DIF-DEA)*2 MACD=(DIF-DEA)*2

fastline = ta.ema(close,12)  
slowline = ta.ema(close,26)
diff = fastline - slowline 

dea = ta.ema(diff,9) 
macd = 2*(diff - dea)

顶背离:

快线下穿慢线: MACD 从正转为负(下穿0线) 价格逐渐上涨:今日价格大于昨日

top_diver = ta.crossunder(macd,0) and close[1] < close
plotchar(top_diver, char='顶背离', location = location.abovebar, size = size.normal, overlay=true)

底背离:

快线上穿慢线: MACD 从负转为正(上穿0线) 价格逐渐上跌:今日价格小于昨日

bot_diver = ta.crossover(macd,0) and close[1] > close
plotchar(bot_diver, char='底背离', location = location.belowbar, size = size.normal, overlay=true)

fastline = ta.ema(close,12)  
slowline = ta.ema(close,26)
diff = fastline - slowline 

dea = ta.ema(diff,9) 
macd = 2*(diff - dea) 



plot(diff, title='diff', style=plot.style_line,overlay=false)
plot(dea, title='dea', style=plot.style_line,overlay=false)
plot(macd, title='delta', color=macd >= 0 ? color.red : color.green,style=plot.style_histogram,histbase=0,overlay=false)

bot_diver = ta.crossover(macd,0) and close[1] > close

plotchar(bot_diver, char='底背离', color = color.red, location = location.belowbar, size = size.normal, overlay=true)

top_diver = ta.crossunder(macd,0) and close[1] < close

plotchar(top_diver, char='顶背离', color = color.green, location = location.abovebar, size = size.normal,overlay=true)




更多内容