OPEN-SOURCE SCRIPT

macd背离

164
//version=6
indicator(title = 'MACD可视化', format = format.price, timeframe = '')

fast_length = input(12, title = 'MACD快线长度')
slow_length = input(26, title = 'MACD慢线长度')
signal_length = input(9, title = 'MACD信号线长度')

col_macd = input.color(#2962FF, 'MACD', group = 'Color Settings', inline = 'MACD')
col_signal = input.color(#FF6D00, '信号线', group = 'Color Settings', inline = 'Signal')
col_grow_above = input.color(#26A69A, '增长绿柱', group = 'Histogram', inline = 'Above')
col_fall_above = input.color(#B2DFDB, '减弱绿柱', group = 'Histogram', inline = 'Above')
col_grow_below = input.color(#FFCDD2, '减弱红柱', group = 'Histogram', inline = 'Below')
col_fall_below = input.color(#FF5252, '增长红柱', group = 'Histogram', inline = 'Below')
// Calculating
fast_ma = ta.ema(close, fast_length) //快线
slow_ma = ta.ema(close, slow_length) //慢线
macd = fast_ma - slow_ma //macd线
signal = ta.ema(macd, signal_length) //信号线

hist = macd - signal //能量柱

plot(hist, title = '直方图', style = plot.style_columns, color = color.new(hist >= 0 ? hist[1] < hist ? col_grow_above : col_fall_above : hist[1] < hist ? col_grow_below : col_fall_below, 0))
plot(macd, title = 'MACD', color = color.new(col_macd, 0))
plot(signal, title = '信号线', color = color.new(col_signal, 0))

rangeUpper1 = input(title = '金死叉最大范围', defval = 60)
rangeLower1 = input(title = '金死叉最小范围', defval = 5)
crossGold = ta.crossover(macd, signal) ? true : false
crossDead = ta.crossunder(macd, signal) ? true : false

_inRange1(cond1) =>
bars1 = ta.barssince(cond1)
rangeLower1 <= bars1 and bars1 <= rangeUpper1

crossJudgeGold = (_inRange1(crossGold) or _inRange1(crossDead)) and crossGold ? macd : na
crossJudgeDead = (_inRange1(crossDead) or _inRange1(crossGold)) and crossDead ? macd : na

plotshape(crossJudgeGold, title = '金叉标识', style = shape.circle, location = location.absolute, color = color.new(color.green, 0), size = size.tiny)
plotshape(crossJudgeDead, title = '死叉标识', style = shape.circle, location = location.absolute, color = color.new(color.red, 0), size = size.tiny)


lbR = input(title = '右范围', defval = 5)
lbL = input(title = '左范围', defval = 5)
rangeUpper = input(title = '最大范围', defval = 60)
rangeLower = input(title = '最小范围', defval = 5)
plotBull = input(title = '底背离标识', defval = true)
plotBear = input(title = '顶背离标识', defval = true)

bearColor = color.red
bullColor = color.green
hiddenBullColor = color.new(color.green, 80)
hiddenBearColor = color.new(color.red, 80)
textColor = color.white
noneColor = color.new(color.white, 100)
osc = macd

plFound = na(ta.pivotlow(osc, lbL, lbR)) ? false : true
phFound = na(ta.pivothigh(osc, lbL, lbR)) ? false : true
_inRange(cond) =>
bars = ta.barssince(cond == true)
rangeLower <= bars and bars <= rangeUpper

//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low

oscHL = osc[lbR] > ta.valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

// Price: Lower Low

priceLL = low[lbR] < ta.valuewhen(plFound, low[lbR], 1)
bullCond = plotBull and priceLL and oscHL and plFound

plot(plFound ? osc[lbR] : na, offset = -lbR, title = '底背离线', linewidth = 2, color = bullCond ? bullColor : noneColor)

plotshape(bullCond ? osc[lbR] : na, offset = -lbR, title = '底背离标识', text = ' 底 ', style = shape.labelup, location = location.absolute, color = color.new(bullColor, 0), textcolor = color.new(textColor, 0))


oscLL = osc[lbR] < ta.valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

oscLH = osc[lbR] < ta.valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])



priceHH = high[lbR] > ta.valuewhen(phFound, high[lbR], 1)

bearCond = plotBear and priceHH and oscLH and phFound

plot(phFound ? osc[lbR] : na, offset = -lbR, title = '顶背离线', linewidth = 2, color = bearCond ? bearColor : noneColor)

plotshape(bearCond ? osc[lbR] : na, offset = -lbR, title = '顶背离标识', text = ' 顶 ', style = shape.labeldown, location = location.absolute, color = color.new(bearColor, 0), textcolor = color.new(textColor, 0))

Penafian

Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.