cdxy.me
Cyber Security / Data Science / Trading

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © cdxy

//@version=4
study("cdxy breakout",overlay=true)

// statics
backlen = input(7, 'sliding window len')

body = abs(close-open)
upshadow = open>close?(high-open):(high-close)
downshadow = open>close?(close-low):(open-low)
exp = high-low

lowest_price = lowest(low, backlen)
highest_price = highest(high, backlen)

low_breakout_signal = (lowest_price - low[0] == 0)
plotshape(low_breakout_signal,style=shape.triangleup,color=color.green,location=location.belowbar)

high_breakout_signal = (highest_price - high[0] == 0)
plotshape(high_breakout_signal,style=shape.triangledown,color=color.red,location=location.abovebar)


backward = high_breakout_signal?abs(close-high):low_breakout_signal?abs(high-low):na


// 单根反转
plotchar(high_breakout_signal and abs(close-high)/exp > 0.5,text="F",char="",color=color.red,location=location.abovebar)
plotchar(low_breakout_signal and abs(close-low)/exp > 0.5,text="F",char="",color=color.green,location=location.belowbar)


//@version=3
study("MACD divergent")

// MACD
fastLength = input(12)
slowLength = input(26)
MACDLength = input(9)

fastEma = ema(close, fastLength)
slowEma = ema(close, slowLength)

MACD = fastEma - slowEma
eMACD = ema(MACD, MACDLength)
deltaMACD = MACD - eMACD

isDeltaUp = deltaMACD - deltaMACD[1] > 0
deltaColor = deltaMACD > 0 ? (isDeltaUp ? aqua : blue) : (isDeltaUp ? maroon : red)

//char = close

// 快线的斜率>0 ? lime : red
plot(MACD, color=MACD > MACD[1] ? lime : red, linewidth=1)
plot(eMACD, color=fuchsia, linewidth=1)
plot(deltaMACD, color=deltaColor, style=histogram, linewidth=3)
gold_cross = crossover(MACD, eMACD)
death_cross = crossunder(MACD, eMACD)
plot(gold_cross ? MACD : na, color=green, style=circles, linewidth=2)
plot(death_cross ? MACD : na, color=red, style=circles, linewidth=2)


backleninput = input(90, '背离计算长度(divergent len)')
backlen = backleninput

lowest_MACD = lowest(MACD, backlen)
// lowest_MACD1 = lowest(MACD, 30)
lowest_price = lowest(close, backlen)
reverse_long_signal = (lowest_price - close == 0) and (lowest_MACD < MACD * 1.1)// and (lowest_MACD1 < MACD * 1.1)
// 多背离
plotshape(reverse_long_signal ? MACD : na, location=location.absolute, color=green, style=shape.labelup, size=size.small)

highest_MACD = highest(MACD, backlen)
// highest_MACD1 = highest(MACD, 30)
highest_price = highest(close, backlen)
// 空背离
reverse_short_signal = (highest_price - close == 0) and (highest_MACD > MACD * 1.1)// and (highest_MACD1 > MACD * 1.1)
plotshape(reverse_short_signal ? MACD : na, location=location.absolute, color=red, style=shape.labeldown, size=size.small)