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

//@version=4
study(title="A_HMS_RSI_COMPOSIT", overlay=false)

////////////////////////////////////////////////Stochastica///////////////////////////////////
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// smoothK = input(14, minval=1), smoothD = input(3, minval=1)
// k = sma(stoch(close, high, low, smoothK), 3)
// d = sma(k, smoothD)
// plot(k-80, color=#880e4f ,linewidth=2 , title = "StochK line" )
// plot(d-80, color=#44cccc ,linewidth=2 , title = "StochD line" )
// h0 = hline(0)
// h1 = hline(-60)
// h2 = hline(-30)
// fill(h0, h1, color=color.gray, transp=95)

// stochKema = ema(k , 12)
// stochKema2 = ema(k , 21)
// stochDema = ema(d , 21)
// plot(stochKema-80, color=color.orange ,linewidth=2 , transp= 10 ,  title = "StochKEma line" )
// plot(stochDema-80 , color=#4400cc , transp= 90 , linewidth=1 , title = "StochDEma line")

////////////////////////////////////////////////Stochastica///////////////////////////////////
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$



////////////////////////////////////////////////////////Comporsi////////////////////////////////
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 
len = input(14, title="Length")
source= input(hl2 , type = input.source )
mom = input(4, title="Momentum",minval=0)
mom20 = input(33 , title="Momentum20",minval=0)
ob = input(70,title="Overbought")
os = input(30,title="Oversold")
c = close
//histo
docol = input(true,title="Change Color?")
dohist = input(true ,title="Show Hist?")

//rsi
showrsi = input(true , title = "Show RSI?")
showrsi2 = input(false , title = "Show RSI2?")
showrsiemas = input(false , title = "Show RSI EMAs?")

//composit
showcomposite = input(true , title = "Show Composite?")
showcomposite2 = input(false , title = "Show Composite2?")
showcomposignal = input(false , title = "Show Copmposite Signal Line?")

//rmi
smaLength = input(9 , title = "SMA Length")
sigone = input(9,title="Signal One Length")
sigtwo = input(33,title="Signal Two Length")
showrmi = input(false , title = "Show RMI?")
showrmi33 = input(true , title = "Show RMI33?")
showemasignal = input(false,title="Show EMA Signal Line?")



//calc
up = ema(max(c - c[mom],0),len)
dn = ema(max(c[mom] - c,0),len)
rmi = dn == 0 ? 0 : 100 - 100 / (1 + up / dn)
up20 = ema(max(c - c[mom20],0),len)
dn20 = ema(max(c[mom20] - c,0),len)
rmi20 = dn == 0 ? 0 : 100 - 100 / (1 + up20 / dn20)
rmiema = ema(rmi , smaLength)
signalone = ema(rmi,sigone)
signaltwo = ema(rmi,sigtwo)

//Calculate Histogram
histogram = dohist ? (rmi - rmiema)+50 : na
histup = true ? histogram >=50 : na
histdown = true ? histogram <50 : na
//Plot histogram
plot(histup ? histogram :na , title = "RMIs Histogram UP" , color = color.rgb(68, 168 , 100 , 85)  ,histbase=50,style=plot.style_histogram ,linewidth=4 )
plot(histdown ? histogram: na , title = "RMIs Histogram DOWN" , color = color.rgb(166 , 95 , 168 , 85)  ,histbase=50,style=plot.style_histogram ,linewidth=4 )
//Second hist
// histogram2 = dohist ? ((rmi - 70 ) - (rmiema - 70)) -70 : na
// histup2 = true ? histogram >=-20 : na
// histdown2 = true ? histogram <-20 : na
// plot(histup2 ? histogram :na , title = "RMIs Histogram UP" , color = color.rgb(0, 68 , 0 , 80)  ,histbase=50,style=plot.style_histogram ,linewidth=4 )
// plot(histdown2 ? histogram: na , title = "RMIs Histogram DOWN" , color = color.rgb(106 , 1 , 168 , 80)  ,histbase=50,style=plot.style_histogram ,linewidth=4 )





//plots
//hline(ob)
//hline(os)
//plot( dohist?(rmi-rmisma)+50:na , color=color.rgb(0, 68, 89 , 58) ,histbase=50,style=plot.style_histogram ,linewidth=3 , title = "RMI's Histogram")
//plot( histogram ?  color.rgb(0,68,0,50)  : color.rgb(255,0,0,50)  ,histbase=50,style=plot.style_histogram ,linewidth=3 , title = "RMI's Histogram")
plot( showemasignal?signalone:na,color=color.orange , title = "RMI's EMA ONE line")
plot( showemasignal?signaltwo:na,color=color.green , title = "RMI's EMA TWO line")
col = docol ? rmi > rmi[1] ? color.rgb(148, 13, 194 , 85) : color.rgb(241, 92, 236 , 85) : #0094FF

//plot rmi
plot(showrmi ? rmi : na , color=col,linewidth=2 , title = "RMI line")
plot(showrmi33 ? rmi20/2 + 25 : na , color=col ,linewidth=2   , title = "RMI33 line")


/////composite
rsi_length=input(14, title="RSI Length")
rsi_mom_length=input(9, title="RSI Momentum Length")
rsi_ma_length=input(3, title="RSI MA Length")
ma_length=input(3, title="SMA Length")
fastLength=input(13)
slowLength=input(33)


r=rsi(source , rsi_length)
rsidelta = mom(r, rsi_mom_length)
rsisma = sma(rsi(source, rsi_ma_length), ma_length)
composit=rsidelta+rsisma

compositline = (composit / 2 + 25)
greenline = (sma(composit , fastLength) / 2 + 25)
redline = (sma(composit , slowLength) / 2 + 25)
rsiemasignalone = ema(r , 12)
rsiemasignaltwo = ema(r , 66)

//Plot Composit
plot(showcomposite ? compositline : na , color=color.rgb(254, 0, 1 , 80 ), linewidth=2 , title = "Composite Line" )
//Second composit
plot(showcomposite2 ? compositline - 70 : na , color=color.rgb(254, 0, 1), linewidth=2 , title = "Composite Line2 " )
plot(showcomposignal ? greenline -70  : na , color=color.green , transp = 68 , title = "Green Line")
plot(showcomposignal ? redline -70  : na , color=color.red , transp = 68 , title = "Red Line")

//Plot RSI
plot(showrsi ?  r : na , color=color.rgb(10, 17, 161) , linewidth=2 , title = "RSI line" )
//Second RSI
plot(showrsi2 ?  r - 70 : na , color=color.rgb(10 , 0, 0 , 90) ,linewidth=2 , title = "RSI line2" )
plot(showrsiemas ?  rsiemasignalone : na , color=color.rgb(255, 100, 0) ,linewidth=2 , title = "RSI EMA ONE" )
plot(showrsiemas ?  rsiemasignaltwo : na , color=color.green ,linewidth=1  , title = "RSI EMA TWO" )


//LINES
// rmizeroline =  hline(0, color=color.rgb(68,0,0,68) , title = "RMI 0 Level" )
//rsiN50 = hline(-50, color=color.rgb(68 , 0 , 0 ,68) , title = "RSI -50 Level" )
//rsiN40 = hline(-40, color=color.rgb(68 , 0 , 0 ,68) , title = "RSI -40 Level" )
//rsiN30 = hline(-30, color=color.rgb(68 , 0 , 0 ,68) , title = "RSI -30 Level" )
rsi0 = hline(0, color=color.rgb(68 , 0 , 0 ,86 ) , title = "RSI 0 Level" )
//rsi10 = hline(-50, color=color.rgb(241,246,242,68) , title = "RSI -50 Level" )
//rsi20 = hline(-10, color=color.rgb(241,246,242,68) , title = "RSI -10 Level" )
//rsi30 = hline(-30, color=color.rgb(241,246,242,68) , title = "RSI -30 Level" )
rsi33 = hline(33.3, color=color.rgb(0, 68, 0 , 86) , title = "RSI 33.3 Level" )
rsiMiddleUpLevel = hline(50,color=color.rgb(70 , 70 ,200 ,68) , title = "RSI50 Middle Up Level")
rsiMiddleDownLevel = hline(40, color=color.rgb(70 , 70 ,200 ,86) , title = "RSI42 Middle Down Level")
rsi60 = hline(60, color=color.rgb(0 , 0 , 0 ,86) , title = "RSI 60 Level" )
rsi67 = hline(66.67, color=color.rgb(0, 68, 0 , 86) , title = "RSI 66.67 Level" )
// rsi70 = hline(70, color=color.rgb(241,246,242,68) , title = "RSI 70 Level" )
rsi80 = hline(80, color=color.rgb(68 , 0 , 0 ,86), title = "RSI 80 Level" )
rsi85 = hline(85, color=color.rgb(0, 68, 0 , 86) , title = "RSI 85 Level" )
rmihanderedline =  hline(100, color=color.rgb(0, 68, 0 , 86) , title = "RMI 100 Level" )


// fill(rsi85 , rsi80 , color=color.rgb(222, 219, 227 , 80) , title = "RSI MIDDLE Level")
fill(rsi60 , rsi67 , color=color.rgb(103, 151, 34 , 85) , title = "RSI MIDDLE Level")
fill(rsiMiddleDownLevel , rsiMiddleUpLevel, color=color.aqua , transp = 85 , title = "RSI MIDDLE Level")
fill(rsi33 , rsiMiddleDownLevel , color=color.rgb(255, 87, 80 , 85) , title = "RSI MIDDLE Level")
// fill(rsi20 , rsi15 , color=color.rgb(222, 219, 227 , 80) , title = "RSI MIDDLE Level")
////////////////////////////////////////////////////////Comporsi////////////////////////////////
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$



///////////////////////////////////////////////////////////Lets Lets Lets Go Go Go////////////////////////////////////////////
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//  if you took a trade for every bars close for the duration of the length
//  how would those trades fair atm?
//  values closer to (100 | -100) are better for its (bull | bear), closer to 0 will be closer to break eaven.
//  this indicator is optimal for trending markets.

// length = input(100)
// avg_length = input(25)

// f_compound_risk(_source, _length)=>
//     _total = 0.0
//     _absolute = 0.0
//     for _i = 1 to _length
//         _container0 = _total
//         _container1 = _absolute
//         _total := _container0 + (_source[0] - _source[_i])
//         _absolute := _container1 + abs(_source[0] - _source[_i])
//     if _total > 0
//         _container = _total
//         _total := _total / _absolute
//     else
//         _container = _total
//         _total := 0 - (abs(_container) / _absolute)
//     _total * 100

// cr = f_compound_risk(close, length)
// ma = ema(cr, avg_length)

// plot(series=(cr/3)-25, color=#880e4f ,linewidth=2 , title="A1")
// plot(series=(ma/3)-25, color=#4400cc ,linewidth=2 , title = "A2")
// hline(10)
// hline(-25)
// hline(-60)
///////////////////////////////////////////////////////////Lets Lets Lets Go Go Go////////////////////////////////////////////
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$