OPEN-SOURCE SCRIPT

davidqqq

27
//version=5
indicator('CD', overlay=false, max_bars_back=500)

// 输入参数
S = input(12, title='Short EMA Period')
P = input(26, title='Long EMA Period')
M = input(9, title='Signal Line Period')

// 计算DIFF, DEA和MACD值
fastEMA = ta.ema(close, S)
slowEMA = ta.ema(close, P)
DIFF = fastEMA - slowEMA
DEA = ta.ema(DIFF, M)
MACD = (DIFF - DEA) * 2

// 计算N1和MM1
N1 = ta.barssince(ta.crossunder(MACD, 0))
MM1 = ta.barssince(ta.crossover(MACD, 0))

// 确保长度参数大于0
N1_safe = na(N1) ? 1 : math.max(N1 + 1, 1)
MM1_safe = na(MM1) ? 1 : math.max(MM1 + 1, 1)

// 计算CC和DIFL系列值
CC1 = ta.lowest(close, N1_safe)
CC2 = nz(CC1[MM1_safe], CC1)
CC3 = nz(CC2[MM1_safe], CC2)
DIFL1 = ta.lowest(DIFF, N1_safe)
DIFL2 = nz(DIFL1[MM1_safe], DIFL1)
DIFL3 = nz(DIFL2[MM1_safe], DIFL2)

// 计算CH和DIFH系列值
CH1 = ta.highest(close, MM1_safe)
CH2 = nz(CH1[N1_safe], CH1)
CH3 = nz(CH2[N1_safe], CH2)
DIFH1 = ta.highest(DIFF, MM1_safe)
DIFH2 = nz(DIFH1[N1_safe], DIFH1)
DIFH3 = nz(DIFH2[N1_safe], DIFH2)

// 判断买入条件
AAA = CC1 < CC2 and DIFL1 > DIFL2 and MACD[1] < 0 and DIFF < 0
BBB = CC1 < CC3 and DIFL1 < DIFL2 and DIFL1 > DIFL3 and MACD[1] < 0 and DIFF < 0
CCC = (AAA or BBB) and DIFF < 0
LLL = not CCC[1] and CCC
XXX = AAA[1] and DIFL1 <= DIFL2 and DIFF < DEA or BBB[1] and DIFL1 <= DIFL3 and DIFF < DEA
JJJ = CCC[1] and math.abs(DIFF[1]) >= math.abs(DIFF) * 1.01
BLBL = JJJ[1] and CCC and math.abs(DIFF[1]) * 1.01 <= math.abs(DIFF)
DXDX = not JJJ[1] and JJJ
DJGXX = (close < CC2 or close < CC1) and (JJJ[MM1_safe] or JJJ[MM1]) and not LLL[1] and math.sum(JJJ ? 1 : 0, 24) >= 1
DJXX = not(math.sum(DJGXX[1] ? 1 : 0, 2) >= 1) and DJGXX
DXX = (XXX or DJXX) and not CCC

// 判断卖出条件
ZJDBL = CH1 > CH2 and DIFH1 < DIFH2 and MACD[1] > 0 and DIFF > 0
GXDBL = CH1 > CH3 and DIFH1 > DIFH2 and DIFH1 < DIFH3 and MACD[1] > 0 and DIFF > 0
DBBL = (ZJDBL or GXDBL) and DIFF > 0
DBL = not DBBL[1] and DBBL and DIFF > DEA
DBLXS = ZJDBL[1] and DIFH1 >= DIFH2 and DIFF > DEA or GXDBL[1] and DIFH1 >= DIFH3 and DIFF > DEA
DBJG = DBBL[1] and DIFF[1] >= DIFF * 1.01
DBJGXC = not DBJG[1] and DBJG
DBJGBL = DBJG[1] and DBBL and DIFF[1] * 1.01 <= DIFF
ZZZZZ = (close > CH2 or close > CH1) and (DBJG[N1_safe] or DBJG[N1]) and not DBL[1] and math.sum(DBJG ? 1 : 0, 23) >= 1
YYYYY = not(math.sum(ZZZZZ[1] ? 1 : 0, 2) >= 1) and ZZZZZ
WWWWW = (DBLXS or YYYYY) and not DBBL

// plot买入和卖出信号
if DXDX
label.new(bar_index, low, text='抄底', style=label.style_label_up, color=color.red, textcolor=color.white, size=size.small)
if DBJGXC
label.new(bar_index, high, text='卖出', style=label.style_label_down, color=color.green, textcolor=color.white, size=size.small)

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.