Trend System Oscillator Averages Rating
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © exlux99

//@version=4

study("Multiple Oscillator Rating", overlay = true)

src=close

//-------------- FUNCTIONS 
dirmov(len) =>
    up = change(high)
    down = -change(low)
    plusDM = na(up) ? na : up > down and up > 0 ? up : 0
    minusDM = na(down) ? na : down > up and down > 0 ? down : 0
    truerange = rma(tr, len)
    plus = fixnan(100 * rma(plusDM, len) / truerange)
    minus = fixnan(100 * rma(minusDM, len) / truerange)
    [plus, minus]

adx(dilen, adxlen) =>
    [plus, minus] = dirmov(dilen)
    sum = plus + minus
    adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)
    adx



// ATR
atr = rma(tr(true), 14)
sma = sma(atr, 20)

atrTrending = if atr > sma
    true
else
    false

// ADX
adxlen = 14
dilen = 14

sig = adx(dilen, adxlen)

adxTrending = if sig > 20
    true
else
    false

// RSI
up = rma(max(change(close), 0), 14)
down = rma(-min(change(close), 0), 14)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))

rsiTrending = if rsi > 60 or rsi < 40
    true
else
    false
    
rsi_len = 14//input(14, minval=1, title="RSI Length")
//STOCH K
stoch_length =14// input(14, minval=1, title="STOCH Length")
stoch_smoothK = 8//input(8, minval=1, title="STOCH K")
//CCI
cci_len = 20//input(20, minval=1, title="CCI Length")
//ADI
//adxlen =14// input(14, title="ADX Smoothing")
//dilen = 14//input(14, title="DI Length")
//Momentum
momentum_len = 10//input(10, minval=1, title="Momentum Length")
//MACD
macd_fast = 12//input(12, type=input.integer, title="MACD fast")
macd_slow = 26//input(27, type=input.integer, title="MACD slow")

//Stochrsi fast
smoothK = 5//input(5, minval=1, title="STOCHRSI smoothK")
smoothD = 5//input(5, minval=1, title="STOCHRSI smoothD")
lengthRSI = 14//input(14, minval=1, title="RSI Length")
lengthStoch = 14//input(14, minval=1, title="STOCH Length")

//William Percentage Range
wpr_length = 14//input(14, minval=1, title="William Perc Range Length")

//Ultimate Oscillator
uo_length7 = 7//input(7, minval=1, title="UO Length 7")
uo_length14 = 14//input(14, minval=1, title="UO Length 14")
uo_length28 = 28//input(28, minval=1, title="UO Length 28")

//-------------- OSCILLATORS CALCULATION
rsi_index = 0.0
stoch_index = 0.0
adx_index = 0.0
cci_index = 0.0
ao_index = 0.0
macd_index = 0.0
mom_index = 0.0
stochrsi_index = 0.0
wpr_index = 0.0
bp_index = 0.0
uo_index = 0.0

//RSI
// up = rma(max(change(src), 0), rsi_len)
// down = rma(-min(change(src), 0), rsi_len)
// rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - 100 / (1 + up / down)

if rsi >= 80
    rsi_index := -1
    rsi_index
if rsi <= 20
    rsi_index := 1
    rsi_index

//STOCH K
stoch_k = sma(stoch(close, high, low, stoch_length), stoch_smoothK)

if stoch_k >= 80
    stoch_index := -1
    stoch_index
if stoch_k <= 20
    stoch_index := 1
    stoch_index

//CCI
cci_ma = sma(src, cci_len)
cci = (src - cci_ma) / (0.015 * dev(src, cci_len))

if cci >= 100
    stoch_index := -1
    stoch_index
if cci <= -100
    stoch_index := 1
    stoch_index

//ADX
adx_calc = adx(dilen, adxlen)

//if adx_calc >= 20 and crossover( )
//    adx_index = -1
//if adx_calc >= 20 and crossover( )
//    adx__index = 1

//Awesome Oscilator
ao = sma(hl2, 5) - sma(hl2, 34)

if ao >= 0
    ao_index := -1
    ao_index
if ao <= 0
    ao_index := 1
    ao_index

//Momentum
mom = src - src[momentum_len]

if mom >= 0
    mom_index := -1
    mom_index
if mom <= 0
    mom_index := 1
    mom_index

//MACD
macd = ema(close, macd_fast) - ema(close, macd_slow)

if macd >= 0
    macd_index := -1
    macd_index
if macd <= 0
    macd_index := 1
    macd_index

//STOCHRSI FAST
rsi1 = rsi(src, lengthRSI)
k = sma(stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK)

if k >= 80
    stochrsi_index := -1
    stochrsi_index
if k <= 20
    stochrsi_index := 1
    stochrsi_index

//Williams Percent Range (14)
wpr_upper = highest(wpr_length)
wpr_lower = lowest(wpr_length)
wpr_out = 100 * (close - wpr_upper) / (wpr_upper - wpr_lower)

if wpr_out >= -20
    stochrsi_index := -1
    stochrsi_index
if wpr_out <= -80
    stochrsi_index := 1
    stochrsi_index

//BullBearPower
//TBD

//Ultimate Oscillator (7, 14, 28)
average(bp, tr_, length) =>
    sum(bp, length) / sum(tr_, length)
high_ = max(high, close[1])
low_ = min(low, close[1])
bp = close - low_
tr_ = high_ - low_
avg7 = average(bp, tr_, uo_length7)
avg14 = average(bp, tr_, uo_length14)
avg28 = average(bp, tr_, uo_length28)
uo_out = 100 * (4 * avg7 + 2 * avg14 + avg28) / 7

if uo_out >= 50
    uo_index := 1
    uo_index
if uo_out <= 50
    uo_index := -1
    uo_index

oscillators_index = (rsi_index + stoch_index + adx_index + cci_index + stochrsi_index + ao_index + 
   mom_index + macd_index + wpr_index + uo_index) / 10

posInput = input(title="Position", defval="Top Right", options=["Bottom Left", "Bottom Right", "Top Left", "Top Right"])
var pos = posInput == "Bottom Left" ? position.bottom_left : posInput == "Bottom Right" ? position.bottom_right : posInput == "Top Left" ? position.top_left : posInput == "Top Right" ? position.top_right : na


var table perfTable = table.new(pos, 2, 4, border_width = 3)


text_size = input("AUTO", "Text Size:", options=["AUTO", "TINY","SMALL", "NORMAL", "LARGE", "HUGE"])
table_size = input(6, "Table Width:")

text = size.auto
if text_size == "TINY"
    text := size.tiny
if text_size == "SMALL"
    text := size.small
if text_size == "NORMAL"
    text := size.normal
if text_size == "LARGE"
    text := size.large
if text_size == "HUGE"
    text := size.huge
    

    
LIGHTTRANSP = 90
AVGTRANSP   = 80
HEAVYTRANSP = 70


f_fillCell(_table, _column, _row, _value, _timeframe, _c_color) =>
    //_c_color = color.blue//close > open ? i_posColor : i_negColor
    _transp = HEAVYTRANSP//abs(_value) > 10 ? HEAVYTRANSP : abs(_value) > 5 ? AVGTRANSP : LIGHTTRANSP
    _cellText = tostring(_value, "#.## %") 
    table.cell(_table, _column, _row, _cellText, bgcolor = color.new(_c_color, _transp), text_color = _c_color, width = table_size)
    table.cell_set_text_size(perfTable, 1, 0, text)
    table.cell_set_text_size(perfTable, 1, 1, text)    
    //table.cell_set_text_size(perfTable, 0, 2, text)
    
    
if barstate.islast

    table.cell(perfTable, 0, 0, "Rating confirmation 0-100 Long | -100-0 Short", text_color=color.white, text_size=text, bgcolor=color.blue)
    f_fillCell(perfTable, 0, 1, (oscillators_index*100)/100 , "% confirmation", (oscillators_index*100)/100 >0? color.lime : color.red )
