Defu_RSI
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Defuwa
//https://cn.tradingview.com/u/Defuwa/#published-scripts
//@version=5
indicator(title='Defu_RSI', overlay=false)
//var table logo = table.new(position.top_right, 1, 1)
//table.cell(logo, 0, 0, 
//         " 害怕踏空只会让你一无所有\n
//         历史会重演，相信你的眼睛\n",
//         text_size = size.auto,text_color = color.new(color.teal,60))

//==============================定义颜色==============================
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)
l        = input(120, title='长周期！') //0轴上方
//==============================自编公式==============================
rsif(scr, len) => ta.sma(math.max(ta.change(scr), 0), len) / ta.sma(math.abs(ta.change(scr)), len) * 100 //rsi
wpr(len)  => (ta.highest(high, len) - close) / (ta.highest(high, len) - ta.lowest(low, len)) * 100 //wpr威廉指标
countf(sr, len) => //计算len周期内符合条件的k线数量
    out = 0
    src = sr
    for i = 1 to len by 1
        out := src[len - i] ? out + 1 : out
    out
rmaf(src, n1, m1) => //m1/n1加权移动平均线
    alpha = m1 / n1
    sum   = 0.0
    sum   := na(sum[1]) ? ta.sma(src, n1) : alpha * src + (1 - alpha) * nz(sum[1])
    sum

//==============================水平强弱分界线==============================
l00        = plot(0, title='hline0', color=color.new(#9598a1, 0), style=plot.style_stepline)
l20        = plot(20, title='hline20', color=lv0, style=plot.style_stepline)
l50        = plot(50, title='hline50', color=color.new(#787b86, 30), style=plot.style_stepline)
l80        = plot(80, title='hline80', color=fen0, style=plot.style_stepline)
l100       = plot(100, title='hline100', display=display.none)
//ld50        = plot(-50, title='hline-50', color=color.new(#787b86, 30), style=plot.style_stepline)

//==============================RSI超买超卖区==============================
RSSwitch = input(defval=true, title='RSSwitch')
len1    = input.int(defval=8, title='RS周期', minval=1)
maim     = rsif(close, len1)
//maid     = plot(RSSwitch and ta.crossunder(rsif(close, 6), 81) ? 100 : na, title='超卖', style=plot.style_circles, color=fen0, linewidth=2, show_last=200)
fill(l00, l20, title='买入区', color=maim < 19 ? color.new(lv1, 80) : touming)
fill(l80, l100, title='卖出区', color=maim > 81 ? color.new(fen0, 80) : touming)
//==============================到顶柱线=============================
DDSwitch  = input(true, title='到顶Switch！')
//daod1     = wpr(9) - 70
daod2     = ta.sma(ta.sma(ta.stoch(close, high, low, 9), 3), 3) - (ta.sma(wpr(9) - 70, 9) + 100)
dding     = daod2  > 40 ? daod2  - 40 : na
ddclr     = ta.change(dding) > 0 ? color.new(hong2, 0) : color.new(hong1, 20)
plot(DDSwitch ? dding+50 : na, title='到顶柱线', color=ddclr, style=plot.style_columns,histbase=50)
daodinal   = dding > 0
//daodinal   = (daodin[1] > 0 and daodin == 0 )or (daodin[2]>daodin[1] and daodin[1]>daodin) 
bgcolor(DDSwitch and daodinal ?  color.new(hong1, 93) : na, title='到顶')
alertcondition(daodinal,title="顶顶顶！", message="到顶了！")

//==============================CCI ema绿线==============================
emCCwitch = input(defval=true, title='emCCwitch')
//cc1       = (hlc3 - ta.ema(hlc3, ltLB)) / (0.015 * ta.stdev(hlc3, ltLB))
ltLB      = input.int(10, title='ltLB',minval=5, maxval = 21)
cci       = (hlc3 - ta.ema(hlc3, ltLB)) / (0.015 * ta.ema(math.abs(hlc3 - ta.ema(hlc3, ltLB)), ltLB))
tci       = ta.ema(cci, 21) 
plot(emCCwitch ? tci+ 50 :na, title='emCC', color= tci > 55 ? color.new(hong1,0) : tci < -55 ? color.new(lv2,0): color.new(lan1,0), linewidth=1)
//==============================趋势XX==============================
TrendSwitch = input(true, title='trendSwitch！')
trenda      = 5 * ta.sma(ta.stoch(close, high, low, 34), 5) - 3 * ta.sma(ta.sma(ta.stoch(close, high, low, 34), 5), 3) - ta.sma(ta.sma(ta.sma(ta.stoch(close, high, low, 34), 5), 3), 2)
zou         = rmaf(rmaf(ta.stoch(close, high, low, 21), 13, 8), 13, 8) - rmaf(wpr(21) - 10, 21, 8)
plotshape(TrendSwitch and ta.crossover(trenda, 3) ? -15 : na, title='趋势底',style= shape.triangleup, location=location.absolute, color=color.new(lv2, 0))
plotshape(TrendSwitch and ta.crossunder(trenda, 108) ? 100: na, title='趋势顶',style= shape.triangledown, location=location.absolute, color=color.new(zi1, 0))
plot(TrendSwitch ? trenda  : na, title='趋势', color=color.new(cheng2, 30) ,linewidth=1)
putdll     = plot(TrendSwitch ? trenda : na,title='主力超载', color=zou > 85 ? color.new(fen2, 20) : touming, linewidth=2)

//=============================日内波动====================================
//=====================长上影线===================================
DaySwitch = input(true, title='日内波动Switch！')

hltrhigh      = (high -math.max(open,close))/(math.min(open ,close) -low)
hltrhighl     =  hltrhigh >1.6 and hltrhigh < 30? -hltrhigh:  hltrhigh >1.6 and hltrhigh > 30 ? -30 : na
hltrguolv     = (high -math.max(open,close)) > math.abs(open - close)*0.5

plot(DaySwitch  and hltrguolv and rsif(close,6) >60 ? hltrhighl: na, title='针探', color=color.new(hong0,50),style=plot.style_columns)
alertcondition(hltrhigh>1.6,title="锤子针！",message="到顶了！")

//=====================中转点======================
hltrmid      = (high - low)/math.abs(open - close)
hltrmidl     = hltrmid >1.6 and hltrmid < 30? -hltrmid:  hltrmid >1.6 and hltrmid > 30 ? -30 : na

plot(DaySwitch  and rsif(close,6) >60 ? hltrmidl : na, title='中转点', color=color.new(cheng1,65),linewidth=1,style=plot.style_columns)
alertcondition(hltrmid>1.6,title="锤子针！",message="转折了！")

