SWITCH STATS: RSI/MTF UPDATED CODE

mail@pastecode.io avatar
unknown
plain_text
a month ago
16 kB
1
Indexable
Never
//@version=5
///------------------------------------------------------------


indicator(title='Switch Stats Updated RSI/MTF', shorttitle='SWITCH STATS')

IndicatorSwitch ='Relative Money Flow Index'

// MONEY FLOW INDEx

useCurrentResMFI = input(true, title='Current Timeframe')
resCustomsMFI = input.timeframe(title='Timeframe', defval="D")
useCurrentpriceMFI = true
show_mfi = 1
baseindicatorswitch = input.string(title = "Base Indicator",defval="RSI",options = ["RSI","MFI","STOCH"])
if baseindicatorswitch == "RSI" 
    show_mfi :=1
if baseindicatorswitch == "MFI" 
    show_mfi :=2
if baseindicatorswitch == "STOCH" 
    show_mfi :=3

srcMFI = close
base_lenMFI = input(title = "length",defval=14) 
len2MFI = 23
th1MFI = 70
th2MFI = 30
show_rsiMFI = false//input(false, title='(Relative money flow index)  Show RSI/MFI/Stoch')
show_rsi2MFI = input(false,"Show RSI")//input(true, title='(Relative money flow index) Show trend')
show_Histo = input(true,"Show Histogram")


show_bgMFI = false
// color_bars = input(false,"Color Candles")
color_baseMFI = 7
show_linesMFI = input(false,"Trendlines")  //input(false,"(Relative money flow index) Show columns trendlines")
show_crossMFI = false //input(false,"(Relative money flow index) Show trendline crosses")
tllen_lMFI = 20
tllen_hMFI = 20

resMFI = useCurrentResMFI ? timeframe.period : resCustomsMFI

//timeframe conversion
base_resMFI = timeframe.period == '1' ? 1 : timeframe.period == '3' ? 3 : timeframe.period == '5' ? 5 : timeframe.period == '15' ? 15 : timeframe.period == '30' ? 30 : timeframe.period == '45' ? 45 : timeframe.period == '60' ? 60 : timeframe.period == '120' ? 120 : timeframe.period == '180' ? 180 : timeframe.period == '240' ? 240 : timeframe.period == 'D' ? 1440 : timeframe.period == 'W' ? 10080 : timeframe.period == 'M' ? 302400 : na
custom_resMFI = resCustomsMFI == '1' ? 1 : resCustomsMFI == '3' ? 3 : resCustomsMFI == '5' ? 5 : resCustomsMFI == '15' ? 15 : resCustomsMFI == '30' ? 30 : resCustomsMFI == '45' ? 45 : resCustomsMFI == '60' ? 60 : resCustomsMFI == '120' ? 120 : resCustomsMFI == '180' ? 180  : resCustomsMFI == '240' ? 240 : resCustomsMFI == "D" ? 1440 : resCustomsMFI == "W" ? 10080  : resCustomsMFI == 'M' ? 302400 : na
res_factorMFI = useCurrentResMFI ? 1 : base_resMFI > custom_resMFI ? 1 : math.round(custom_resMFI / base_resMFI)
lenMFI = useCurrentResMFI ? base_lenMFI : useCurrentpriceMFI ? base_lenMFI * res_factorMFI : base_lenMFI

//MFI calcutation
TypPrice = hlc3
upper_sMFI = math.sum(volume * (ta.change(TypPrice) <= 0 ? 0 : TypPrice), lenMFI)
lower_sMFI = math.sum(volume * (ta.change(TypPrice) >= 0 ? 0 : TypPrice), lenMFI)
mfMFI = 100.0 - 100.0 / (1.0 + upper_sMFI / lower_sMFI)

//stoch calculation
stMFIMFI = ta.stoch(close, high, low, lenMFI)

//RSI/MFI/Stoch selection
rsi_1MFI = ta.rsi(srcMFI, lenMFI)
rsi_baseMFI = show_mfi == 1 ? rsi_1MFI : show_mfi == 2 ? mfMFI : stMFIMFI

//RSI output MTF
rsiMTF = request.security(syminfo.tickerid, useCurrentpriceMFI ? timeframe.period : resMFI, rsi_baseMFI)
rsi_MTF = request.security(syminfo.tickerid, resMFI, rsi_baseMFI)
smooth_baseMFI = ta.ema(rsi_baseMFI, lenMFI)
ema_1MFI = ta.ema(rsiMTF, lenMFI)
security_1MFI = request.security(syminfo.tickerid, resMFI, smooth_baseMFI)
smoothMFI = useCurrentpriceMFI ? ema_1MFI : security_1MFI

DiffMFI = rsiMTF - smoothMFI

//alternative version MA definition
//smooth2MFI=ema(ema(DiffMFI,res_factorMFI),len2MFI)

//2nd version MA TEMA definition
sema1MFI = ta.ema(DiffMFI, res_factorMFI)
sema2MFI = ta.ema(sema1MFI, len2MFI)
sema3MFI = ta.ema(sema2MFI, len2MFI)
smooth2MFI = 3 * (sema1MFI - sema2MFI) + sema3MFI

smooth_upMFI = smoothMFI >= smoothMFI[1] ? true : false
smooth2_uppMFI = smooth2MFI >= smooth2MFI[2] ? true : false

//ADXMFI and DI trend calculation
TrueRangeMFI = math.max(math.max(high - low, math.abs(high - nz(close[1]))), math.abs(low - nz(close[1])))
DirectionalMovementPluseMFI = high - nz(high[1]) > nz(low[1]) - low ? math.max(high - nz(high[1]), 0) : 0
DirectionalMovementMinuseMFI = nz(low[1]) - low > high - nz(high[1]) ? math.max(nz(low[1]) - low, 0) : 0

calc_DIPlus() =>
    base_DIPlus = float(na)
    SmoothedTrueRange = float(na)
    SmoothedTrueRange := nz(SmoothedTrueRange[1]) - nz(SmoothedTrueRange[1]) / lenMFI + TrueRangeMFI
    SmoothedDirectionalMovementPlus = float(na)
    SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - nz(SmoothedDirectionalMovementPlus[1]) / lenMFI + DirectionalMovementPluseMFI
    base_DIPlus := SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
    base_DIPlus
calc_DIMinus() =>
    base_DIMinus = float(na)
    SmoothedTrueRange = float(na)
    SmoothedTrueRange := nz(SmoothedTrueRange[1]) - nz(SmoothedTrueRange[1]) / lenMFI + TrueRangeMFI
    SmoothedDirectionalMovementMinus = float(na)
    SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - nz(SmoothedDirectionalMovementMinus[1]) / lenMFI + DirectionalMovementMinuseMFI
    base_DIMinus := SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
    base_DIMinus

//ADXMFI output MTF
HTFMFI = base_resMFI > custom_resMFI ? true : false
DIPlusMFI = request.security(syminfo.tickerid, useCurrentpriceMFI ? timeframe.period : resMFI, calc_DIPlus())
DIPlusSMFI = request.security(syminfo.tickerid, resMFI, calc_DIPlus())
DIMinus = request.security(syminfo.tickerid, useCurrentpriceMFI ? timeframe.period : resMFI, calc_DIMinus())
DIMinusSSMFI = request.security(syminfo.tickerid, resMFI, calc_DIMinus())

DIdiff = HTFMFI ? DIPlusMFI - DIMinus : DIPlusSMFI - DIMinusSSMFI
DIcolor = DIdiff < 0 ? color.red : DIdiff > 0 ? color.green : color.yellow
DXMFI = HTFMFI ? math.abs(DIPlusMFI - DIMinus) / (DIPlusMFI + DIMinus) * 100 : math.abs(DIPlusSMFI - DIMinusSSMFI) / (DIPlusSMFI + DIMinusSSMFI) * 100
ADXMFI = ta.sma(DXMFI, lenMFI)

momentprod = useCurrentpriceMFI and HTFMFI ? DIdiff * ADXMFI * res_factorMFI / 2 : DIdiff * ADXMFI
smooth_ADX = ta.ema(momentprod, lenMFI)
strong_trend = DIdiff < 0 and smooth_ADX <= smooth_ADX[1] or DIdiff > 0 and smooth_ADX >= smooth_ADX[1] ? true : false
//color definition

// color_121 = color.new(#089981,0)
// color_131 = color.new(#f23645,0)

color_1 = color.new(#089981, 0)
color_2 = color.new(#089981, 50)

color_131 = color.new(#f23645,0)
color_132 = color.new(#f23645,50)



emacolor = DiffMFI >= 0 ? smooth2_uppMFI ? color_1 : color_2 : smooth2_uppMFI ? color_132 : color_131
smooth2color = smooth2MFI >= smooth2MFI[2] ? color.green : color.red
color_3 = color.new(color.red, 75)
color_4 = color.new(color.lime, 85)
color_5 = color.new(color.green, 90)
color_6 = color.new(color.maroon, 85)
bcol = rsi_MTF < th2MFI ? color_3 : rsi_MTF > th1MFI ? color_4 : rsiMTF > 50 ? color_5 : color_6
color_7 = color.new(color.maroon, 85)
color_8 = color.new(color.maroon, 20)
color_9 = color.new(color.lime, 85)
color_10 = color.new(color.green, 90)
color_11 = color.new(color.green, 30)
rsicol = bcol == color.new(color.red, 75) ? color.red : bcol == color_7 ? color_8 : bcol == color_9 ? color.lime : bcol == color_10 ? color_11 : na
plot( IndicatorSwitch == 'Relative Money Flow Index' and show_Histo ? DiffMFI :na, title='RSI - histo', style=plot.style_columns, color=emacolor, linewidth=2, transp=0)
plot( IndicatorSwitch == 'Relative Money Flow Index' and show_rsiMFI ? smooth2MFI :na, title='RSI - ma', color=smooth2color, linewidth=3, transp=20,editable=false)
plot (IndicatorSwitch == 'Relative Money Flow Index' and show_rsi2MFI ? rsiMTF - 50 : na, linewidth=2, color=rsicol, transp=0,editable=false)
bgcolor(title='RSI overbought/oversold', color=show_bgMFI ? bcol : na, transp=90,editable=false)

//trendline pivots
last_high1 = ta.pivothigh(DiffMFI, tllen_hMFI, tllen_hMFI)
last_low1 = ta.pivotlow(DiffMFI, tllen_lMFI, tllen_lMFI)
color_high = ta.highest(DiffMFI, 2 * tllen_hMFI) < ta.highest(DiffMFI, 5 * tllen_hMFI) ? color.red : na
color_low = ta.lowest(DiffMFI, 2 * tllen_lMFI) > ta.lowest(DiffMFI, 5 * tllen_lMFI) ? color.teal : na
_barshigh = ta.barssince(DiffMFI >= last_high1)
_barslow = ta.barssince(DiffMFI <= last_low1)

//last high-low point
lh1 = ta.highest(DiffMFI, tllen_hMFI)
lh2 = ta.highest(DiffMFI, 6 * tllen_hMFI)
lh3 = ta.highest(DiffMFI, 10 * tllen_hMFI)
//indication of possible reversal or H/S pattern
high_falling = lh1 < lh2 and lh2 < lh3 ? true : false

last_high1_period = ta.barssince(DiffMFI >= lh1)
last_high2_period = ta.barssince(DiffMFI >= lh2)

ll1 = ta.lowest(DiffMFI, tllen_lMFI)
ll2 = ta.lowest(DiffMFI, 5 * tllen_lMFI)
ll3 = ta.lowest(DiffMFI, 12 * tllen_lMFI)
//indication of possible reversal or inv. H/S pattern
low_rising = ll1 > ll2 and ll2 > ll3 ? true : false

//trendline plot
plot(IndicatorSwitch == 'Relative Money Flow Index' and show_linesMFI ? last_high1 : na, title='trendline high', color=color_high, offset=-1 * tllen_hMFI, transp=0,editable=false)
plot(IndicatorSwitch == 'Relative Money Flow Index' and  show_linesMFI ? last_low1 : na, title='trendline low', color=color_low, offset=-1 * tllen_lMFI, transp=0,editable=false)

///ext high trendlines
_show_last = 10000
Ph1y = fixnan(last_high1)
Ph2y = float(na)
Ph2y := ta.change(Ph1y) != 0 ? Ph1y[1] : Ph2y[1]
Ph1x = 0
Ph2x = int(na)
change_1 = ta.change(Ph1y[1])
barssince_1 = ta.barssince(change_1 != 0)
Ph2x := ta.change(Ph1y) != 0 ? barssince_1 : Ph2x[1]
Ph2xbis = ta.barssince(ta.change(Ph1y) != 0)
// point slope
mh = float(na)
mh := ta.change(Ph1y) != 0 ? (Ph1y - Ph2y) / Ph2x : mh[1]
// plot range
ext_high_tline = Ph1y + mh * Ph2xbis
ext_high_tline_RT = Ph1y + mh * (Ph2xbis + tllen_hMFI)
color_ext_tl_high = mh <= 0 ? color.red : na
// plot(IndicatorSwitch == 'Relative Money Flow Index' and show_linesMFI ? mh <= 0 and ext_high_tline_RT[tllen_hMFI] != ext_high_tline ? ext_high_tline : na : na, title='ext trendline high', color=color_ext_tl_high, linewidth=1, style=plot.style_linebr, offset=-tllen_hMFI, show_last=_show_last, transp=40,editable=false)
// plot( IndicatorSwitch == 'Relative Money Flow Index' and show_linesMFI ? mh <= 0 ? ext_high_tline_RT : na : na, title='ext trendline high RT', color=color_ext_tl_high, linewidth=1, style=plot.style_circles, show_last=_show_last, transp=40,editable=false)

//ext low trendlines
Pl1y = fixnan(last_low1)
Pl2y = float(na)
Pl2y := ta.change(Pl1y) != 0 ? Pl1y[1] : Pl2y[1]
Pl1x = 0
Pl2x = int(na)
change_2 = ta.change(Pl1y[1])
barssince_2 = ta.barssince(change_2 != 0)
Pl2x := ta.change(Pl1y) != 0 ? barssince_2 : Pl2x[1]
Pl2xbis = ta.barssince(ta.change(Pl1y) != 0)
// point slope
ml = float(na)
ml := ta.change(Pl1y) != 0 ? (Pl1y - Pl2y) / Pl2x : ml[1]
// plot range
ext_low_tline = Pl1y + ml * Pl2xbis
ext_low_tline_RT = Pl1y + ml * (Pl2xbis + tllen_lMFI)
color_ext_tl_low = ml >= 0 ? color.lime : na
// plot(IndicatorSwitch == 'Relative Money Flow Index' and  show_linesMFI ? ml >= 0 and ext_low_tline_RT[tllen_lMFI] != ext_low_tline ? ext_low_tline : na : na, title='ext trendline low', color=color_ext_tl_low, linewidth=1, style=plot.style_linebr, offset=-tllen_lMFI, show_last=_show_last, transp=40,editable=false)
// plot(IndicatorSwitch == 'Relative Money Flow Index' and show_linesMFI ? ml >= 0 ? ext_low_tline_RT : na : na, title='ext trendline low RT', color=color_ext_tl_low, linewidth=1, style=plot.style_circles, show_last=_show_last, transp=40,editable=false)

//detect trendline breaks
avg_Diff = ta.ema(DiffMFI, res_factorMFI)
crossover_1 = ta.crossover(avg_Diff, ext_high_tline_RT)
break_tl_high = mh <= 0 ? crossover_1 : false
crossunder_1 = ta.crossunder(avg_Diff, ext_low_tline_RT)
break_tl_low = ml >= 0 ? crossunder_1 : false
cross_color = break_tl_high ? color.lime : break_tl_low ? color.fuchsia : color.yellow
above_tl_high = avg_Diff >= ext_high_tline ? true : false
below_tl_low = avg_Diff <= ext_low_tline ? true : false

//mark trendline crosses
YPosition = show_crossMFI and break_tl_high ? ext_high_tline_RT : break_tl_low ? ext_low_tline_RT : na
plotshape(IndicatorSwitch == 'Relative Money Flow Index' and show_crossMFI and break_tl_high ? YPosition : na, style=shape.triangleup, location=location.absolute, size=size.tiny, color=cross_color, transp=0,editable=false)
plotshape(IndicatorSwitch == 'Relative Money Flow Index' and show_crossMFI and break_tl_low ? YPosition : na, style=shape.triangledown, location=location.absolute, size=size.tiny, color=cross_color, transp=0,editable=false)

//final coloring
ema_2 = ta.ema(DiffMFI, res_factorMFI)
trend_color = strong_trend ? DIcolor : smooth2MFI >= 0 or ema_2 >= 0 ? high_falling ? color.red : color.green : low_rising ? color.green : color.red
ema_3 = ta.ema(DiffMFI, res_factorMFI)
trendline_color = DiffMFI < ext_low_tline_RT ? DiffMFI > 0 ? smooth2color : color.red : DiffMFI > ext_high_tline_RT ? DiffMFI < 0 ? smooth2color : color.green : ema_3 >= 0 ? color.green : color.red
ema_4 = ta.ema(DiffMFI, res_factorMFI)
ema_5 = ta.ema(rsiMTF, res_factorMFI)
ema_6 = ta.ema(DiffMFI, res_factorMFI)
ema_7 = ta.ema(DiffMFI, res_factorMFI)
barcolor_ref = color_baseMFI == 1 ? smooth2color : color_baseMFI == 2 ? ema_4 >= 0 ? color.green : color.red : color_baseMFI == 3 ? (useCurrentpriceMFI ? ema_5 : rsiMTF) > 50 ? color.green : color.red : color_baseMFI == 4 ? smooth2MFI >= 0 or ema_6 >= 0 ? color.green : low_rising ? smooth2color : color.red : color_baseMFI == 5 ? smooth2MFI <= 0 or ema_7 <= 0 ? color.red : high_falling ? smooth2color : color.green : color_baseMFI == 6 ? trend_color : color_baseMFI == 7 ? trendline_color : na
color_12 = color.new(color.lime, 30)
color_13 = color.new(color.red, 20)
barc = barcolor_ref == color.green ? color_12 : barcolor_ref == color.red ? color_13 : color.yellow
// barcolor(color_bars ? barc : na,editable = false)
baseline_col = strong_trend ? DIcolor : na
// plot(IndicatorSwitch == 'Relative Money Flow Index' ?  0 :na, color=baseline_col, linewidth=2, transp=0)

//bar coloring
colorchange = barc == barc[1] ? false : true


// uppertopHline = hline( useCurrentResMFI and baseindicatorswitch == "RSI"  ? 20 :na,color=color.new(color.lime, 30), editable = false,linestyle=hline.style_solid)
// upperbottomHline = hline(useCurrentResMFI and baseindicatorswitch == "RSI"  ? 19 :na,color=color.new(color.lime, 30),editable = false)

// lowertopHline = hline(useCurrentResMFI and baseindicatorswitch == "RSI"  ? -19 :na,color=color.new(color.red, 30),editable = false)
// lowerbottomHline = hline(useCurrentResMFI and baseindicatorswitch == "RSI"  ? -20 :na,color=color.new(color.red, 30),editable = false,linestyle=hline.style_solid)

// MiddleHline = hline( useCurrentResMFI and baseindicatorswitch == "RSI"  ? 0 :na,color=color.new(color.white, 30), editable = false)

// fill(uppertopHline,upperbottomHline,color=color.new(color.lime,90),editable=false)
// fill(lowertopHline,lowerbottomHline,color=color.new(color.red,90),editable=false)


//option for long and short signals or just "trend change" signal you can use if you only have a basic account with one alert max.

//possible long/short-conditions.

SignalBuy_Alert = ta.crossover(DiffMFI,0) or DiffMFI == 0  
SignalSell_Alert = ta.crossunder(DiffMFI,0) or DiffMFI == 0  

longcondition = colorchange and barc == color.new(color.lime, 30)
shortcondition = colorchange and barc == color.new(color.red, 20)
longcondition_tl_high = DiffMFI > ext_high_tline and DiffMFI[1] <= ext_high_tline ? true : false
shortcondition_tl_low = DiffMFI < ext_low_tline and DiffMFI[1] >= ext_low_tline ? true : false
alertcondition(longcondition, 'Momentum Change Long', 'Momentum Change')
alertcondition(shortcondition, 'Momentum Change Short', 'Momentum Change')
alertcondition(shortcondition or longcondition, 'Momentum Change Long/Short', 'Momentum Change Both')

alertcondition(SignalBuy_Alert, "Trend Change Long",'Trend has changed')
alertcondition(SignalSell_Alert, "Trend Change Short",'Trend has changed')
alertcondition(SignalSell_Alert or SignalBuy_Alert , "Trend Change Short/Long",'Trend has changed')



// ===================== 
// ==== Backtesting ====
// =====================
startLongTrade = SignalBuy_Alert
startShortTrade = SignalSell_Alert
endLongTrade = SignalSell_Alert
endShortTrade =  SignalBuy_Alert



// The following can be used to stream signals to a backtest adapter
backTestStream = switch 
    startLongTrade => 1
    endLongTrade => 2
    startShortTrade => -1
    endShortTrade => -2
plot(backTestStream, "Backtest Stream", display=display.none,editable=false)
Leave a Comment