Trend Indicator with RSI and Fibbonacci Band 0.702 crossings
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © blublub

//@version=4
//strategy("Trend Indicator with RSI crossing", shorttitle="TIR", overlay=true, format=format.price, precision=2, margin_long = 120, margin_short = 120 )
study("Trend Indicator with RSI and Fibbonacci Band 0.702 crossings", shorttitle="TIRF", overlay=true, format=format.price, precision=2, resolution="")


//EMA variables and Plotting
shortest = ema(close, 20)
short = ema(close, 50)
longer = ema(close, 100)
longest = ema(close, 200)

plot(shortest, title='20 EMA', color=color.red, linewidth=2)
plot(short, title='50 EMA', color=color.orange)
plot(longer, title='100 EMA', color=color.aqua)
plot(longest, title='200 EMA', color=color.rgb(8, 137, 243),  linewidth=2)


// Trend Indicator
period=input(5,"CCI period")
coeff=input(2,"ATR Multiplier")
AP=input(1,"ATR Period")
ATR=sma(tr,AP)
src=input(hlc3, "Main Input Data/ Symbol") // hlc3 is used to get the average value instead of the candle close value
cci_value=input(20, "CCI Value threshold for Neutral Trend") // for the white dots, Neutral trend calculation
cci_top=input(165, "CCI Top/ Overbought") // variable for the CCI top or bottom signal
cci_bottom=input(-165, "CCI Bottom/ Oversold")
upT=low-ATR*coeff
downT=high+ATR*coeff

Trend=0.0
Trend := cci(src,period)>=0 ? (upT<nz(Trend[1]) ? nz(Trend[1]) : upT) : (downT>nz(Trend[1]) ? nz(Trend[1]) : downT)

// CCI calculation 
bullish_trend = cci(src, period) > cci_value
bearish_trend = cci(src, period) < cci_value
neutral_trend = cci(src, period) >= -cci_value and cci(src, period) <= cci_value
cci_beartime = cci(src, period) >= cci_top
cci_bulltime = cci(src, period) <= cci_bottom

// RSI Indicators -----------------------------------------------------------------------------

// Input parameters
rsi_length = input(title="RSI Length", type=input.integer, defval=14)
rsi_oversold = input(title="RSI Oversold", type=input.float, defval=25.0)
rsi_overbought = input(title="RSI Overbought", type=input.float, defval=80.0)

// Calculate RSI value
rsi_value = rsi(close, rsi_length)

// RSI Moving Average
rsi_MaLength = input(14, minval=1, title="RSI Moving Average Length")
rsi_Ma = ema(rsi_value, rsi_MaLength)

// RSI Buy and Sell Signals
rsi_UpCross = crossover(rsi_value, rsi_Ma)
rsi_DownCross = crossunder(rsi_value, rsi_Ma)
rsioversold = rsi_value < rsi_oversold
rsioverbought = rsi_value > rsi_overbought

// FBB 0.702 Crossing ------------------------------------------------------------------------

//setting up the variables
FBB_length = input(200, "Fibbonaci Length", minval=1)
FBB_mult = input(3.0, "Fibbonacci Multiplikator", minval=0.001, maxval=50)

// Calculation of the 0.702 bands
dev = FBB_mult * stdev(src, FBB_length)
FBB_basis = vwma(src, FBB_length)
lower_FBB= FBB_basis - (0.702*dev)
upper_FBB= FBB_basis + (0.702*dev)

// calculation of the crossing
FBB_UpCross = crossover(src, lower_FBB)
FBB_DownCross = crossover(src, upper_FBB)


//Plotting -----------------------------------------------------------------------------------
//Plotting of the trendline 
color1= cci(src,period)>=0 ? #0022FC : #FC0400
plot(Trend, color=color1, linewidth=2, title="Trend Line")

// Plot circles below the candle for a bullish trend and above the candle for a bearish trend
plotshape(bullish_trend, location=location.belowbar, style=shape.circle, color=color.green, size=size.auto, title="Bullish Trend/Dots")
plotshape(bearish_trend, location=location.abovebar, style=shape.circle, color=color.red, size=size.auto, title="Bearish Trend/Dots")
plotshape(neutral_trend, location=location.abovebar, style=shape.square, color=color.rgb(255, 255, 255), size=size.auto, title="Neutral Trend")
alertcondition(neutral_trend, title="Trend change", message="Trend went neutral")
alertcondition(bullish_trend, title="Bullish Trend", message="Trend is Bullish")
alertcondition(bearish_trend, title="Bearish Trend", message="Trend went Bearish")

//Plotting of CCI Oversold & Overbought signals
plotshape(cci_bulltime, style=shape.flag, location=location.belowbar, color=color.rgb(4, 245, 12), size=size.small, title="CCI Buy Signal")
plotshape(cci_beartime, style=shape.flag, location=location.abovebar, color=color.rgb(241, 5, 5), size=size.small, title="CCI Sell Signal")
// Alert conditions for CCI overbought and oversold
alertcondition(cci_beartime, title="CCI Sell Signal", message="Sell Signal, CCI value indicates overbought")
alertcondition(cci_bulltime, title="CCI Buy Signal", message="Buy Signal, CCI value indicates oversold") 

//Plotting of crossover signals
plotshape(rsi_UpCross, style=shape.triangleup, location=location.belowbar, color=color.rgb(0, 255, 8), size=size.tiny, title="RSI Buy Signal")
plotshape(rsi_DownCross, style=shape.triangledown, location=location.abovebar, color=color.rgb(241, 3, 3), size=size.tiny, title="RSI Sell Signal")
// Alert conditions for RSI crossovers
alertcondition(rsi_UpCross, title="RSI Buy Signal", message="Buy Signal, RSI has crossed above its moving average")
alertcondition(rsi_DownCross, title="RSI Sell Signal", message="Sell Signal, RSI has crossed below its moving average")

//plotting overbought and oversold RSI
bar_color1 = rsioversold ? #da0aff : na
barcolor(bar_color1, title="Oversold RSI")

bar_color2 = rsioverbought ? color.rgb(19, 15, 243) : na
barcolor(bar_color2, title="Overbought RSI")

// Plot Fibbonacci 0.702 crossing and their lines
plotshape(FBB_UpCross, location=location.belowbar, style=shape.circle, color=#eeff00, size=size.small, title="FIB 0.702 Buy Signal")
plotshape(FBB_DownCross, location=location.abovebar, style=shape.circle, color=#eeff00, size=size.small, title="FIB 0.702 Sell Signal")
plot(lower_FBB, color=color.purple, linewidth=2, title="-0.702")
plot(upper_FBB, color=color.purple, linewidth=2, title="0.702")
alertcondition(FBB_UpCross, title="FIB 0.702 Buy Signal", message="Price crossed lower 0.702 Fibbonacci Band, buy?")
alertcondition(FBB_DownCross, title="FIB 0.702 Sell Signal", message="Price crossed higher 0.702. Fibbonacci Band, sell?")
