风险提示:虚拟货币不具有法定货币等同的法律地位,参与虚拟货币投资交易存在法律风险,继续浏览代表你同意以上所有声明,否则请立即关闭本站!

MACD背离版+金死叉指标

//@version=4
study(title="macd背离", format=format.price, resolution="")

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

col_macd = input(#2962FF, "MACD", input.color, group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "信号线", input.color, group="Color Settings", inline="Signal")
col_grow_above = input(#26A69A, "增长绿柱", input.color, group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "减弱绿柱", input.color, group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "减弱红柱", input.color, group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "增长红柱", input.color, group="Histogram", inline="Below")
// Calculating
fast_ma = ema(close, fast_length)//快线
slow_ma = ema(close, slow_length)//慢线
macd = fast_ma - slow_ma//macd线
signal = 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 = crossover(macd,signal)?true:false
crossDead = crossunder(macd,signal)?true:false

_inRange1(cond1) =>
    bars1 = 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.green,size = size.tiny)
plotshape(crossJudgeDead,title = "死叉标识",style = shape.circle,location = location.absolute,color = color.red,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(pivotlow(osc, lbL, lbR)) ? false : true
phFound = na(pivothigh(osc, lbL, lbR)) ? false : true
_inRange(cond) =>
    bars = barssince(cond == true)
    rangeLower <= bars and bars <= rangeUpper

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

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

// Price: Lower Low

priceLL = low[lbR] < 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=bullColor,
     textcolor=textColor
     )


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

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



priceHH = high[lbR] > 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=bearColor,
     textcolor=textColor
     )

背离指标.png

发表评论