//@version=5
indicator('Improved Yuvis Stock prediction system with RSI, MA, and BB', shorttitle='YSPS', overlay=true, format=format.price, precision=2, timeframe='')
// Parameters
coeff = input.float(1.0, 'Multiplier', minval=0.1, maxval=10.0, step=0.1)
AP = input(14, 'Common Period')
src = input(close)
showsignalsk = input.bool(title='Show Signals?', defval=true)
useVolumeData = input.bool(title='Incorporate Volume Data?', defval=false)
stopLossPercent = input.float(1.0, 'Stop Loss %', minval=0.1, maxval=10.0, step=0.1)
maPeriod = input.int(20, 'Moving Average Period')
bbLength = input.int(20, 'Bollinger Bands Length')
bbDeviation = input.int(2, 'Bollinger Bands Deviation')
// Calculation
ATR = ta.sma(ta.tr, AP)
upT = low - ATR * coeff
downT = high + ATR * coeff
YSPS = 0.0
YSPS := (useVolumeData ? ta.rsi(src, AP) >= 50 : ta.mfi(hlc3, AP) >= 50) ? upT < nz(YSPS[1]) ? nz(YSPS[1]) : upT : downT > nz(YSPS[1]) ? nz(YSPS[1]) : downT
// Moving Average Calculation
ma = ta.sma(src, maPeriod)
// Bollinger Bands Calculation
basis = ta.sma(src, bbLength)
dev = ta.stdev(src, bbLength)
upperBB = basis + dev * bbDeviation
lowerBB = basis - dev * bbDeviation
// Signals
buySignal = ta.crossover(YSPS, YSPS[2])
sellSignal = ta.crossunder(YSPS, YSPS[2])
maBuySignal = ta.crossover(src, ma)
maSellSignal = ta.crossunder(src, ma)
bbBuySignal = ta.crossover(src, lowerBB)
bbSellSignal = ta.crossunder(src, upperBB)
K1 = ta.barssince(buySignal)
K2 = ta.barssince(sellSignal)
O1 = ta.barssince(buySignal[1])
O2 = ta.barssince(sellSignal[1])
// Get the last buy price
lastBuyPrice = ta.valuewhen(buySignal, YSPS, 0)
// Stop Loss Calculation
stopLossPrice = lastBuyPrice * (1 - stopLossPercent / 100)
// Calculate shouldSell
shouldSell = close <= stopLossPrice
// Plotting signals with Buy RSI, Sell RSI, Buy MA, Sell MA, Buy BB, and Sell BB
plotshape(buySignal and showsignalsk and O1 > K2 ? YSPS[2] * 0.9999 : na, title='Buy RSI', text='Buy RSI', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(#0022FC, 0), textcolor=color.new(color.white, 0))
plotshape(sellSignal and showsignalsk and O2 > K1 and lastBuyPrice <= YSPS[2] ? YSPS[2] * 0.9999 : na, title='Sell RSI', text='Sell RSI', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.maroon, 0), textcolor=color.new(color.white, 0))
plotshape(maBuySignal and showsignalsk ? src * 0.9999 : na, title='Buy MA', text='Buy MA', location=location.absolute, style=shape.arrowdown, size=size.tiny, color=color.green, textcolor=color.white)
plotshape(maSellSignal and showsignalsk ? src * 0.9999 : na, title='Sell MA', text='Sell MA', location=location.absolute, style=shape.square, size=size.tiny, color=color.red, textcolor=color.white)
plotshape(bbBuySignal and showsignalsk ? src * 0.9999 : na, title='Buy BB', text='Buy BB', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.yellow, textcolor=color.black)
plotshape(bbSellSignal and showsignalsk ? src * 0.9999 : na, title='Sell BB', text='Sell BB', location=location.absolute, style=shape.square, size=size.tiny, color=color.red, textcolor=color.white)
// Alert Conditions
alertcondition(buySignal[1] and O1[1] > K2, title='Confirmed BUY Alarm (RSI)', message='BUY SIGNAL APPROVED!')
alertcondition(sellSignal[1] and O2[1] > K1 and shouldSell, title='Confirmed SELL Alarm (RSI)', message='SELL SIGNAL APPROVED!')
alertcondition(maBuySignal, title='Buy MA', message='BUY MA SIGNAL APPROVED!')
alertcondition(maSellSignal, title='Sell MA', message='SELL MA SIGNAL APPROVED!')
alertcondition(bbBuySignal, title='Buy BB', message='BUY BB SIGNAL APPROVED!')
alertcondition(bbSellSignal, title='Sell BB', message='SELL BB SIGNAL APPROVED!')