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

//@version=5
indicator("Ratings Algo", overlay = true)
maweight = input(5, title = "MA Weighting")
oscweight = input(5, title = "OSC Weighting")
threshold = input(2.0, title = "Threshold")
longma = if ta.sma(close,50) > ta.sma(close, 200)
    1
else if ta.sma(close,50) < ta.sma(close, 200)
    -1
else
    0
    
shortma = if ta.sma(close,10) > ta.sma(close, 20)
    1
else if ta.sma(close,10) < ta.sma(close, 20)
    -1
else
    0
    
lsma = if ta.linreg(close, 10, 0) > ta.linreg(close, 20, 0)
    1
else if ta.linreg(close, 10, 0) < ta.linreg(close, 20, 0)
    -1
else 
    0
    
ema = if ta.ema(close,10) > ta.ema(close, 20)
    1
else if ta.ema(close,10) < ta.ema(close, 20)
    -1
else
    0
    
[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)
macd = if macdLine > 0 and macdLine > signalLine
    1
else if macdLine < 0 and macdLine < signalLine
    -1
else
    0
    
lsma2 = ta.linreg(ta.ema(close, 5), 10, 0) 
alma = ta.alma(close, 10, 0.85, 6)
arnold = if lsma2 > lsma2[1] and alma > alma[1]
    1
else if lsma2 < lsma2[1] and alma < alma[1]
    -1
else 
    0

obv = if ta.linreg(ta.obv, 10, 0) > ta.linreg(ta.obv, 20, 0)
    1
else if ta.linreg(ta.obv, 10, 0) < ta.linreg(ta.obv, 20, 0)
    -1
else
    0

pvt = if ta.linreg(ta.pvt, 10, 0) > ta.linreg(ta.pvt, 20, 0)
    1
else if ta.linreg(ta.pvt, 10, 0) < ta.linreg(ta.pvt, 20, 0)
    -1
else
    0
    
up = ta.change(high)
down = -ta.change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
trur = ta.rma(ta.tr, 14)
plus = fixnan(100 * ta.rma(plusDM, 14) / trur)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
minus = fixnan(100 * ta.rma(minusDM, 14) / trur)
dmi = if plus >  minus
    1
else if plus < minus
    -1
else 
    0

rsi = if ta.rsi(close, 14) > 50
    1
else if ta.rsi(close, 14) < 50
    -1
else 
    0

high_ = ta.highest(hl2, 9)
low_ = ta.lowest(hl2, 9)
round_(val) => val > .99 ? .999 : val < -.99 ? -.999 : val
value = 0.0
value := round_(.66 * ((hl2 - low_) / (high_ - low_) - .5) + .67 * nz(value[1]))
fish1 = 0.0
fish1 := .5 * math.log((1 + value) / (1 - value)) + .5 * nz(fish1[1])
fish2 = fish1[1]
fish = if fish1 > fish2
    1
else if fish2 > fish1 
    -1
else 
    0 

rating = maweight*(longma + shortma + lsma + ema + arnold)/5 + oscweight*(macd + obv + pvt + dmi + rsi + fish)/6
colorr = rating > threshold  and rating < 2.5*threshold ? color.teal : rating > 2.5*threshold ? #00FF00 : rating < -threshold and rating > -2.5 * threshold ? color.orange : rating < -2.5*threshold ? #FF0000 : color.purple
plotcandle(open, high, low, close, color = colorr, wickcolor = colorr)

var ratings = table.new(position = position.bottom_left, columns = 20, rows = 3, bgcolor = color.white, border_color = #6A0DAD, border_width = 2)
table.cell(table_id = ratings, column = 1, row = 1, text = "50/200ma")
table.cell(table_id = ratings, column = 1, row = 2, text = longma == 1 ? "Buy" : longma == -1 ? "Sell" : "Hold", bgcolor = longma == 1 ? #00FF00 : longma == -1 ? #FF0000 : #FFFF00)

table.cell(table_id = ratings, column = 2, row = 1, text = "10/20ma")
table.cell(table_id = ratings, column = 2, row = 2, text = shortma == 1 ? "Buy" : shortma == -1 ? "Sell" : "Hold", bgcolor = shortma== 1 ? #00FF00 : shortma == -1 ? #FF0000 : #FFFF00)

table.cell(table_id = ratings, column = 3, row = 1, text = "10/20lsma")
table.cell(table_id = ratings, column = 3, row = 2, text = lsma == 1 ? "Buy" : lsma == -1 ? "Sell" : "Hold", bgcolor = lsma == 1 ? #00FF00 : lsma == -1 ? #FF0000 : #FFFF00)

table.cell(table_id = ratings, column = 4, row = 1, text = "10/20ema")
table.cell(table_id = ratings, column = 4, row = 2, text = ema == 1 ? "Buy" : ema == -1 ? "Sell" : "Hold", bgcolor = ema == 1 ? #00FF00 : ema == -1 ? #FF0000 : #FFFF00)

table.cell(table_id = ratings, column = 5, row = 1, text = "arnold")
table.cell(table_id = ratings, column = 5, row = 2, text = arnold == 1 ? "Buy" : arnold == -1 ? "Sell" : "Hold", bgcolor = arnold== 1 ? #00FF00 : arnold == -1 ? #FF0000 : #FFFF00)

table.cell(table_id = ratings, column = 6, row = 1, text = "macd")
table.cell(table_id = ratings, column = 6, row = 2, text = macd == 1 ? "Buy" : macd == -1 ? "Sell" : "Hold", bgcolor = macd == 1 ? #00FF00 : macd == -1 ? #FF0000 : #FFFF00)


table.cell(table_id = ratings, column = 7, row = 1, text = "obv")
table.cell(table_id = ratings, column = 7, row = 2, text = obv == 1 ? "Buy" : obv == -1 ? "Sell" : "Hold", bgcolor = obv == 1 ? #00FF00 : obv == -1 ? #FF0000 : #FFFF00)

table.cell(table_id = ratings, column = 8, row = 1, text = "pvt")
table.cell(table_id = ratings, column = 8, row = 2, text = pvt == 1 ? "Buy" : pvt == -1 ? "Sell" : "Hold", bgcolor = pvt == 1 ? #00FF00 : pvt == -1 ? #FF0000 : #FFFF00)

table.cell(table_id = ratings, column = 9, row = 1, text = "dmi")
table.cell(table_id = ratings, column = 9, row = 2, text = dmi == 1 ? "Buy" : dmi == -1 ? "Sell" : "Hold", bgcolor = dmi == 1 ? #00FF00 : dmi == -1 ? #FF0000 : #FFFF00)

table.cell(table_id = ratings, column = 10, row = 1, text = "rsi")
table.cell(table_id = ratings, column = 10, row = 2, text = rsi == 1 ? "Buy" : rsi == -1 ? "Sell" : "Hold", bgcolor = rsi == 1 ? #00FF00 : rsi == -1 ? #FF0000 : #FFFF00)

table.cell(table_id = ratings, column = 11, row = 1, text = "fish")
table.cell(table_id = ratings, column = 11, row = 2, text = fish == 1 ? "Buy" : fish == -1 ? "Sell" : "Hold", bgcolor = fish == 1 ? #00FF00 : fish == -1 ? #FF0000 : #FFFF00)

table.cell(table_id = ratings, column = 12, row = 1, text = "Total" + "/" + str.tostring(maweight + oscweight))
table.cell(table_id = ratings, column = 12, row = 2, text = str.tostring(rating), bgcolor = color.white)

Buy = colorr == #00FF00 and colorr[1] != #00FF00 
Sell= colorr == #FF0000 and colorr[1] != #FF0000 
Exit = colorr[1] == #00FF00 and colorr != #00FF00 or colorr[1] == #FF0000 and colorr != #FF0000
plotshape(Buy, title = "Buy", style = shape.labelup, location = location.belowbar, color = #00FF00, text = "STRONG", textcolor = color.white)
plotshape(Sell, title = "Sell", style = shape.labeldown, location = location.abovebar, color = #FF0000, text = "STRONG", textcolor = color.white)
plotshape(Exit, title = "Exit", style = shape.xcross, location = location.abovebar, color = #00E2FF, text = "", textcolor = color.white)
plot(rating, display = display.none)
plot(0, display = display.none)

mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")
basis = ta.sma(close, 20)
dev = mult * ta.stdev(close, 20)
upper = basis + dev
lower = basis - dev
upper2 = basis + 2*dev
lower2 = basis - 2*dev
p1 = plot(upper, display = display.none)
p2 = plot(upper2, display = display.none)
p3 = plot(lower, display = display.none)
p4 = plot(lower2, display = display.none)
fill(p1, p2, color = color.rgb(155,0,0,50))
fill(p3, p4, color = color.rgb(0,155,0,50))





