Defu_Divergence
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © defuwa
//@version=5
// CSML is price divergence. c=close,s=short,m=mid,l=long.
// 通过当前价与短器、中期、长期EMA之间的关系，判断市场乖离程度。
indicator(title='Defu_Divergence')
//=============================定义颜色=========================================
yellow   = color.yellow
red      = color.red
black    = color.black
hvi      = #787b86
hei      = #071513
hong0    = #e57373
hong1    = #ef5350
hong2    = #d32f2f
hong3    = #b71c1c
cheng1   = #ffa726
cheng2   = #f57c00
cheng3   = #e65100
lv0      = #a5d6a7
lv1      = #66bb6a
lv2      = #388e3c
lv3      = #00796b
lan0     = #52c3d0
lan1     = #90bff9
lan2     = #3179f5
lan3     = #0c3299
zi0      = #ce93d8
zi1      = #ba68c8
zi2      = #7b1fa2
zi3      = #4a148c
fen0     = #f48fb1
fen1     = #f06292
fen2     = #ec407a
fen3     = #c2185b
touming  = color.new(color.blue, 100)

//================================自编公式================================
wpr(len)  => (ta.highest(high, len) - close) / (ta.highest(high, len) - ta.lowest(low, len)) * 100
rsif(scr, len) =>ta.sma(math.max(ta.change(scr), 0), len) / ta.sma(math.abs(ta.change(scr)), len) * 100
rmaf(src, n1, m1) => //m1/n1加权移动平均线
    alpha = m1 / n1
    sum   = 0.0
    sman1 = ta.sma(src, n1)
    sum   := na(sum[1]) ? sman1 : alpha * src + (1 - alpha) * nz(sum[1])
    sum
//================================说明=================
//var table logo = table.new(position.bottom_left, 1, 1)
//table.cell(logo, 0, 0," 背景红绿蓝rsiOBV\n灰大量蓝红柱MFI\n",text_size = size.auto, text_color = color.new(color.teal,70))

EMASwitch  = input(defval=true, title='EMA123Switch')
s          = input(defval=21, title='短期 MA')
m          = input(defval=55, title='中期 MA')
l          = input(defval=120, title='长期 MA')
sr         = input(defval=close, title='当前价')
BBSwitch   = input(defval=true, title='布林带Switch')
volSwitch  = input(defval=true, title='巨大成交量Highlight large volume')

//================================成交量大量背景显示==============================
vema       = input(defval=14, title='大量EMA周期')
vol        = input.float(defval=2.25, title='大量因数Highlight large volume', minval=1, step=0.01)
largevol   = volume > vol * ta.ema(volume, vema)
bgcolor(volSwitch and largevol ? color.new(color.gray, 85) : na, title='大量large volume')

//================================市场成本乖离程度 price divergence ===============
//M1         = ta.ema(close, s)
//M2         = ta.ema(close, m)
//M3         = ta.ema(close, l)
EM1        = (sr / ta.ema(close, s) - 1) * 100
EM2        = (ta.ema(close, s) / ta.ema(close, m) - 1) * 100
EM3        = (ta.ema(close, m) / ta.ema(close, l) - 1) * 100
eEM1       = ta.ema(ta.sma(EM1, 3), 3)


//================================布林带================================
[_, upperL, lowerL] = ta.bb(EM1, s, 1.5)  // 1.5 = 下轨Lower Standard Deviation
[_, upperH, lowerH] = ta.bb(EM1, s, 2.25)   // 2.25 = 上轨Higher Standard Deviation
bu1        = plot(BBSwitch ? upperL : na, 'BB超买Lower Line-Overbought', display=display.none)
bu2        = plot(BBSwitch ? upperH : na, 'BB超买Upper Line-Overbought', display=display.none)
fill(bu1, bu2, color=EM1 > upperL ? color.new(#e57373, 65) : color.new(#e57373, 85), title='超买Overbought Background')
bl1        = plot(BBSwitch ? lowerL : na, 'BB超卖Upper Line-Oversold', display=display.none)
bl2        = plot(BBSwitch ? lowerH : na, 'BB超卖Lower Line-Oversold', display=display.none)
fill(bl1, bl2, color=EM1 < lowerL ? color.new(#66bb6a, 60) : color.new(#66bb6a, 85), title='超卖Oversold Background')
//设置警报，EM1> or <7.
alertcondition(ta.crossunder(EM1, upperL), title='出货提醒', message='乖离很大，要出货吗？')
alertcondition(ta.crossover(EM1, lowerH), title='抄底提醒', message='乖离很大，要入场吗？')

//================================资金流量指标 MFI================================
MFISwitch  = input(true, title='MFI Switch')
length     = input.int(defval=14, title='周期length', minval=1)
mf         = ta.mfi(hlc3, length)
mftclr     = mf > 89 ? color.new(cheng2, 30) : mf > 85 ? color.new(cheng2, 45) : mf > 81 ? color.new(cheng2, 60) : touming
mflclr     = mf < 13 ? color.new(lan1, 30) : mf < 16 ? color.new(lan1, 45) : mf < 19 ? color.new(lan1, 60) : touming
mfclr      = mf > 81 ? mftclr : mflclr
mfil       = plot(MFISwitch ? eEM1 : na, title='MFI', color=mfclr, style=plot.style_columns, linewidth=2, show_last=200)
//mfil       = plot(MFISwitch ? mf : na, title='MFI', color=mfclr, style=plot.style_columns, linewidth=2, show_last=200)

//================================EM123交叉================================
crossed2   = ta.cross(EM2, EM3)
plot(crossed2 ? EM3 : na, title='55120交叉', style=plot.style_circles, linewidth=2, color=color.new(color.red, 0))

//================================乖离率制图================================
B0         = plot(EMASwitch ?   0 : na, title='0轴', color=color.new(#4db6ac,0), linewidth=1)
B3         = plot(EMASwitch ? EM3 : na, title='EM3', color=color.new(lan1, 0), linewidth=2)
B2         = plot(EMASwitch ? EM2 : na, title='EM2', color=color.new(fen2,0), linewidth=1)
B1         = plot(EMASwitch ? EM1 : na, title='EM1', color=color.new(#5d606b, 0), linewidth=1)
