Fundamnetals + Strength + RiskManagement
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// credits to© HeWhoMustNotBeNamed and @Tradingview
// cvsingh-Added Risk Management

//@version=4
study("Fundamnetals + Strenth + RiskManagement", overlay=true)

res = input("", title="Indicator Timeframe", type=input.resolution)
ratingSignal = input(defval = "All", title = "Rating is based on", options = ["MAs", "Oscillators", "All"])

Position = input(title="Table Position", defval=position.bottom_right,
                         options=[
                         position.bottom_center, position.bottom_left, position.bottom_right,
                         position.middle_center, position.middle_left, position.middle_right,
                         position.top_center, position.top_left, position.top_right
                         ])
textSize = input(title="Text Size", defval=size.small, options=[size.huge, size.large, size.normal, size.small, size.tiny, size.auto])

capital = input(title="Capital", defval=100000, group="Risk Management")
risk = input(title="% Risk", defval=1, group="Risk Management")
lookback = input(title="Lookback for SL", defval=10, group="Risk Management")

viewFunda = input(title="View Fundamentals?", defval=true, group="Fundamentals")
viewSolvency = input(title="View Solvency?", defval=true, group="Fundamentals")
viewProfit = input(title="View Profitaility?", defval=true, group="Fundamentals")
viewLiquidity = input(title="View Liquidity?", defval=true, group="Fundamentals")
viewGrowth = input(title="View Growth?", defval=true, group="Fundamentals")
viewTech = input(title="View Technicals?", defval=true, group="Technicals")
viewStrenth = input(title="View Strenth?", defval=true, group="Technicals")
viewRisk= input(title="View RiskManagement?", defval=true, group="Technicals")

useMtf1 = input(false, "", type=input.bool, inline="mtf1", group="Show MTF")
mtf1 = input("60", "", type=input.resolution, inline="mtf1", group="Show MTF")
useMtf2 = input(false, "", type=input.bool, inline="mtf2", group="Show MTF")
mtf2 = input("240", "", type=input.resolution, inline="mtf2", group="Show MTF")
useMtf3 = input(true, "", type=input.bool, inline="mtf3", group="Show MTF")
mtf3 = input("1D", "", type=input.resolution, inline="mtf3", group="Show MTF")
useMtf4 = input(true, "", type=input.bool, inline="mtf4", group="Show MTF")
mtf4 = input("1W", "", type=input.resolution, inline="mtf4", group="Show MTF")
useMtf5 = input(true, "", type=input.bool, inline="mtf5", group="Show MTF")
mtf5 = input("1M", "", type=input.resolution, inline="mtf5", group="Show MTF")

colBuy = input(#5b9cf6, "Buy       ", group="Color Settings", inline="Buy Colors")
colStrongBuy = input(#2962ff, "", group="Color Settings", inline="Buy Colors")
colNeutral = input(#a8adbc, "Neutral ", group="Color Settings", inline="Neutral")
colSell = input(#ef9a9a, "Sell     ", group="Color Settings", inline="Sell Colors")
colStrongSell = input(#f44336, "",  group="Color Settings", inline="Sell Colors")
tableTitleColor = input(#295b79, "Headers", group="Color Settings", inline="Headers")

period = "FY"

sl = lowest(low, lookback)
maxloss = capital*(risk/100)
position = int(maxloss/(close-sl))
invetment = close*position

f_getFinancials(financial_id, period) => financial(syminfo.tickerid, financial_id, period)
f_getColorByScore(score) => (score == 2 ? color.green : score == 1? color.lime : score == 0? color.silver : score == -1? color.orange : score == -2? color.red : color.purple)
f_getOptimalFinancialBasic(financial_id) =>
    f_getFinancials(financial_id, syminfo.currency == "USD"? "FQ" : "FY")
    
//////Calculations
// Awesome Oscillator
AO() => 
    sma(hl2, 5) - sma(hl2, 34)
// Stochastic RSI
StochRSI() =>
    rsi1 = rsi(close, 14)
    K = sma(stoch(rsi1, rsi1, rsi1, 14), 3)
    D = sma(K, 3)
    [K, D]
// Ultimate Oscillator
tl() => close[1] < low ? close[1]: low
uo(ShortLen, MiddlLen, LongLen) =>
    Value1 = sum(tr, ShortLen)
    Value2 = sum(tr, MiddlLen)
    Value3 = sum(tr, LongLen)
    Value4 = sum(close - tl(), ShortLen)
    Value5 = sum(close - tl(), MiddlLen)
    Value6 = sum(close - tl(), LongLen)
    float UO = na
    if Value1 != 0 and Value2 != 0 and Value3 != 0
        var0 = LongLen / ShortLen
        var1 = LongLen / MiddlLen
        Value7 = (Value4 / Value1) * (var0)
        Value8 = (Value5 / Value2) * (var1)
        Value9 = (Value6 / Value3)
        UO := (Value7 + Value8 + Value9) / (var0 + var1 + 1)
    UO
// Ichimoku Cloud
donchian(len) => avg(lowest(len), highest(len))
ichimoku_cloud() =>
    conversionLine = donchian(9)
    baseLine = donchian(26)
    leadLine1 = avg(conversionLine, baseLine)
    leadLine2 = donchian(52)
    [conversionLine, baseLine, leadLine1, leadLine2]
    
calcRatingMA(ma, src) => na(ma) or na(src) ? na : (ma == src ? 0 : ( ma < src ? 1 : -1 ))
calcRating(buy, sell) => buy ? 1 : ( sell ? -1 : 0 )
calcRatingAll() =>
    //============== MA =================
    SMA10 = sma(close, 10)
    SMA20 = sma(close, 20)
    SMA30 = sma(close, 30)
    SMA50 = sma(close, 50)
    SMA100 = sma(close, 100)
    SMA200 = sma(close, 200)
    
    EMA10 = ema(close, 10)
    EMA20 = ema(close, 20)
    EMA30 = ema(close, 30)
    EMA50 = ema(close, 50)
    EMA100 = ema(close, 100)
    EMA200 = ema(close, 200)
    
    HullMA9 = hma(close, 9)
    
    // Volume Weighted Moving Average (VWMA)
    VWMA = vwma(close, 20)
    
    [IC_CLine, IC_BLine, IC_Lead1, IC_Lead2] = ichimoku_cloud()
    
    // ======= Other =============
    // Relative Strength Index, RSI
    RSI = rsi(close,14)
    
    // Stochastic
    lengthStoch = 14
    smoothKStoch = 3
    smoothDStoch = 3
    kStoch = sma(stoch(close, high, low, lengthStoch), smoothKStoch)
    dStoch = sma(kStoch, smoothDStoch)
    
    // Commodity Channel Index, CCI
    CCI = cci(close, 20)
    
    // Average Directional Index
    float adxValue = na, float adxPlus = na, float adxMinus = na
    [P, M, V] = dmi(14, 14)
    adxValue := V
    adxPlus := P
    adxMinus := M
    // Awesome Oscillator
    ao = AO()
    
    // Momentum
    Mom = mom(close, 10)
    // Moving Average Convergence/Divergence, MACD
    [macdMACD, signalMACD, _] = macd(close, 12, 26, 9)
    // Stochastic RSI
    [Stoch_RSI_K, Stoch_RSI_D] = StochRSI()
    // Williams Percent Range
    WR = wpr(14)
    
    // Bull / Bear Power
    BullPower = high - ema(close, 13)
    BearPower = low - ema(close, 13)
    // Ultimate Oscillator
    UO = uo(7,14,28)
    if not na(UO)
        UO := UO * 100
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    PriceAvg = ema(close, 50)
    DownTrend = close < PriceAvg
    UpTrend = close > PriceAvg
    // calculate trading recommendation based on SMA/EMA
    float ratingMA = 0
    float ratingMAC = 0
    
    float ratingSMA10 = na
    if not na(SMA10)
        ratingSMA10 := calcRatingMA(SMA10, close)
        ratingMA := ratingMA + ratingSMA10
        ratingMAC := ratingMAC + 1
    float ratingSMA20 = na
    if not na(SMA20)
        ratingSMA20 := calcRatingMA(SMA20, close)
        ratingMA := ratingMA + ratingSMA20
        ratingMAC := ratingMAC + 1
    float ratingSMA30 = na
    if not na(SMA30)
        ratingSMA30 := calcRatingMA(SMA30, close)
        ratingMA := ratingMA + ratingSMA30
        ratingMAC := ratingMAC + 1
    float ratingSMA50 = na
    if not na(SMA50)
        ratingSMA50 := calcRatingMA(SMA50, close)
        ratingMA := ratingMA + ratingSMA50
        ratingMAC := ratingMAC + 1
    float ratingSMA100 = na
    if not na(SMA100)
        ratingSMA100 := calcRatingMA(SMA100, close)
        ratingMA := ratingMA + ratingSMA100
        ratingMAC := ratingMAC + 1
    float ratingSMA200 = na
    if not na(SMA200)
        ratingSMA200 := calcRatingMA(SMA200, close)
        ratingMA := ratingMA + ratingSMA200
        ratingMAC := ratingMAC + 1

    float ratingEMA10 = na
    if not na(EMA10)
        ratingEMA10 := calcRatingMA(EMA10, close)
        ratingMA := ratingMA + ratingEMA10
        ratingMAC := ratingMAC + 1
    float ratingEMA20 = na
    if not na(EMA20)
        ratingEMA20 := calcRatingMA(EMA20, close)
        ratingMA := ratingMA + ratingEMA20
        ratingMAC := ratingMAC + 1
    float ratingEMA30 = na
    if not na(EMA30)
        ratingEMA30 := calcRatingMA(EMA30, close)
        ratingMA := ratingMA + ratingEMA30
        ratingMAC := ratingMAC + 1
    float ratingEMA50 = na
    if not na(EMA50)
        ratingEMA50 := calcRatingMA(EMA50, close)
        ratingMA := ratingMA + ratingEMA50
        ratingMAC := ratingMAC + 1
    float ratingEMA100 = na
    if not na(EMA100)
        ratingEMA100 := calcRatingMA(EMA100, close)
        ratingMA := ratingMA + ratingEMA100
        ratingMAC := ratingMAC + 1
    float ratingEMA200 = na
    if not na(EMA200)
        ratingEMA200 := calcRatingMA(EMA200, close)
        ratingMA := ratingMA + ratingEMA200
        ratingMAC := ratingMAC + 1

    float ratingHMA = na
    if not na(HullMA9)
        ratingHMA := calcRatingMA(HullMA9, close)
        ratingMA := ratingMA + ratingHMA
        ratingMAC := ratingMAC + 1
    
    float ratingVWMA = na
    if not na(VWMA)
        ratingVWMA := calcRatingMA(VWMA, close)
        ratingMA := ratingMA + ratingVWMA
        ratingMAC := ratingMAC + 1
    
    float ratingIC = na
    if not (na(IC_Lead1) or na(IC_Lead2) or na(close) or na(close[1]) or na(IC_BLine) or na(IC_CLine))
        ratingIC := calcRating(
         IC_Lead1 > IC_Lead2 and close > IC_Lead1 and close < IC_BLine and close[1] < IC_CLine and close > IC_CLine,
         IC_Lead2 > IC_Lead1 and close < IC_Lead2 and close > IC_BLine and close[1] > IC_CLine and close < IC_CLine)
    if not na(ratingIC)
        ratingMA := ratingMA + ratingIC
        ratingMAC := ratingMAC + 1
    
    ratingMA := ratingMAC > 0 ? ratingMA / ratingMAC : na
    
    float ratingOther = 0
    float ratingOtherC = 0

    float ratingRSI = na
    if not(na(RSI) or na(RSI[1]))
        ratingRSI := calcRating(RSI < 30 and RSI[1] < RSI, RSI > 70 and RSI[1] > RSI)
        ratingOtherC := ratingOtherC + 1
        ratingOther := ratingOther + ratingRSI

    float ratingStoch = na
    if not(na(kStoch) or na(dStoch) or na(kStoch[1]) or na(dStoch[1]))
        ratingStoch := calcRating(kStoch < 20 and dStoch < 20 and kStoch > dStoch and kStoch[1] < dStoch[1], kStoch > 80 and dStoch > 80 and kStoch < dStoch and kStoch[1] > dStoch[1])
        ratingOtherC := ratingOtherC + 1
        ratingOther := ratingOther + ratingStoch

    float ratingCCI = na
    if not(na(CCI) or na(CCI[1]))
        ratingCCI := calcRating(CCI < -100 and CCI > CCI[1], CCI > 100 and CCI < CCI[1])
        ratingOtherC := ratingOtherC + 1
        ratingOther := ratingOther + ratingCCI

    float ratingADX = na
    if not(na(adxValue) or na(adxPlus[1]) or na(adxMinus[1]) or na(adxPlus) or na(adxMinus))
        ratingADX := calcRating(adxValue > 20 and adxPlus[1] < adxMinus[1] and adxPlus > adxMinus, adxValue > 20 and adxPlus[1] > adxMinus[1] and adxPlus < adxMinus)
        ratingOtherC := ratingOtherC + 1
        ratingOther := ratingOther + ratingADX

    float ratingAO = na
    if not(na(ao) or na(ao[1]))
        ratingAO := calcRating(crossover(ao,0) or (ao > 0 and ao[1] > 0 and ao > ao[1] and ao[2] > ao[1]), crossunder(ao,0) or (ao < 0 and ao[1] < 0 and ao < ao[1] and ao[2] < ao[1]))
        ratingOtherC := ratingOtherC + 1
        ratingOther := ratingOther + ratingAO

    float ratingMOM = na
    if not(na(Mom) or na(Mom[1]))
        ratingMOM := calcRating(Mom > Mom[1], Mom < Mom[1])
        ratingOtherC := ratingOtherC + 1
        ratingOther := ratingOther + ratingMOM

    float ratingMACD = na
    if not(na(macdMACD) or na(signalMACD))
        ratingMACD := calcRating(macdMACD > signalMACD, macdMACD < signalMACD)
        ratingOtherC := ratingOtherC + 1
        ratingOther := ratingOther + ratingMACD

    float ratingStoch_RSI = na
    if not(na(DownTrend) or na(UpTrend) or na(Stoch_RSI_K) or na(Stoch_RSI_D) or na(Stoch_RSI_K[1]) or na(Stoch_RSI_D[1]))
        ratingStoch_RSI := calcRating(
         DownTrend and Stoch_RSI_K < 20 and Stoch_RSI_D < 20 and Stoch_RSI_K > Stoch_RSI_D and Stoch_RSI_K[1] < Stoch_RSI_D[1],
         UpTrend and Stoch_RSI_K > 80 and Stoch_RSI_D > 80 and Stoch_RSI_K < Stoch_RSI_D and Stoch_RSI_K[1] > Stoch_RSI_D[1])
    if not na(ratingStoch_RSI)
        ratingOtherC := ratingOtherC + 1
        ratingOther := ratingOther + ratingStoch_RSI
    
    float ratingWR = na
    if not(na(WR) or na(WR[1]))
        ratingWR := calcRating(WR < -80 and WR > WR[1], WR > -20 and WR < WR[1])
    if not na(ratingWR)
        ratingOtherC := ratingOtherC + 1
        ratingOther := ratingOther + ratingWR
    
    float ratingBBPower = na
    if not(na(UpTrend) or na(DownTrend) or na(BearPower) or na(BearPower[1]) or na(BullPower) or na(BullPower[1]))
        ratingBBPower := calcRating(
         UpTrend and BearPower < 0 and BearPower > BearPower[1],
         DownTrend and BullPower > 0 and BullPower < BullPower[1])
    if not na(ratingBBPower)
        ratingOtherC := ratingOtherC + 1
        ratingOther := ratingOther + ratingBBPower
    
    float ratingUO = na
    if not(na(UO))
        ratingUO := calcRating(UO > 70, UO < 30)
    if not na(ratingUO)
        ratingOtherC := ratingOtherC + 1
        ratingOther := ratingOther + ratingUO
    
    ratingOther := ratingOtherC > 0 ? ratingOther / ratingOtherC : na
    
    float ratingTotal = 0
    float ratingTotalC = 0
    if not na(ratingMA)
        ratingTotal := ratingTotal + ratingMA
        ratingTotalC := ratingTotalC + 1
    if not na(ratingOther)
        ratingTotal := ratingTotal + ratingOther
        ratingTotalC := ratingTotalC + 1
    ratingTotal := ratingTotalC > 0 ? ratingTotal / ratingTotalC : na

    [ratingTotal, ratingOther, ratingMA]

StrongBound = 0.5
WeakBound = 0.1
    
getSignal(ratingTotal, ratingOther, ratingMA) =>
    float _res = ratingTotal
    if ratingSignal == "MAs"
        _res := ratingMA
    if ratingSignal == "Oscillators"
        _res := ratingOther
    _res

[ratingTotalCurrent, ratingOtherCurrent, ratingMACurrent]  = security(syminfo.tickerid, res, calcRatingAll())
[ratingTotal_mtf1,   ratingOther_mtf1,   ratingMA_mtf1]    = security(syminfo.tickerid, mtf1, calcRatingAll())
[ratingTotal_mtf2,   ratingOther_mtf2,   ratingMA_mtf2]    = security(syminfo.tickerid, mtf2, calcRatingAll())
[ratingTotal_mtf3,   ratingOther_mtf3,   ratingMA_mtf3]    = security(syminfo.tickerid, mtf3, calcRatingAll())
[ratingTotal_mtf4,   ratingOther_mtf4,   ratingMA_mtf4]    = security(syminfo.tickerid, mtf4, calcRatingAll())
[ratingTotal_mtf5,   ratingOther_mtf5,   ratingMA_mtf5]    = security(syminfo.tickerid, mtf5, calcRatingAll())

tradeSignal = getSignal(ratingTotalCurrent, ratingOtherCurrent, ratingMACurrent)

calcRatingStatus(value) =>
    if na(value)
        "-"
    else if -StrongBound > value
        "Strong\nSell"
    else if value < -WeakBound
        "Sell   "
    else if value > StrongBound
        "Strong\nBuy "
    else if value > WeakBound
        "Buy    "
    else
        "Neutral"

allRatingsArray = array.from("All", calcRatingStatus(ratingTotalCurrent), calcRatingStatus(ratingTotal_mtf1), calcRatingStatus(ratingTotal_mtf2),
                  calcRatingStatus(ratingTotal_mtf3), calcRatingStatus(ratingTotal_mtf4), calcRatingStatus(ratingTotal_mtf5))
                  
oscRatingsArray = array.from("Osc", calcRatingStatus(ratingOtherCurrent), calcRatingStatus(ratingOther_mtf1), calcRatingStatus(ratingOther_mtf2),
                  calcRatingStatus(ratingOther_mtf3), calcRatingStatus(ratingOther_mtf4), calcRatingStatus(ratingOther_mtf5))
                  
maRatingsArray = array.from("MAs", calcRatingStatus(ratingMACurrent), calcRatingStatus(ratingMA_mtf1), calcRatingStatus(ratingMA_mtf2),
                 calcRatingStatus(ratingMA_mtf3), calcRatingStatus(ratingMA_mtf4), calcRatingStatus(ratingMA_mtf5))

currentRes = res==""?timeframe.period=="D"?"1D":
					 timeframe.period=="W"?"1W":
					 timeframe.period=="M"?"1M":
					 timeframe.period:res
allResArray = array.from("TF", currentRes, mtf1, mtf2, mtf3, mtf4, mtf5)
usedMtfArray = array.from(true, true, useMtf1, useMtf2, useMtf3, useMtf4, useMtf5)

removeUnused(duplicatedIndex) =>
    if array.size(duplicatedIndex) > 0
        for j=0 to array.size(duplicatedIndex)-1
            int currentDupIndex  = array.shift(duplicatedIndex)
            array.remove(allResArray,     currentDupIndex-j)
            array.remove(usedMtfArray,    currentDupIndex-j)
            array.remove(allRatingsArray, currentDupIndex-j)
            array.remove(oscRatingsArray, currentDupIndex-j)
            array.remove(maRatingsArray,  currentDupIndex-j)

eraseDuplicatedAndDisabledTf() =>
    int[] duplicatedIndex = array.new_int()
    for m=1 to array.size(allResArray)-1
        bool isCurrentMtfDisabled = array.get(usedMtfArray, m) == false
        if isCurrentMtfDisabled
            array.push(duplicatedIndex, m)
    removeUnused(duplicatedIndex)
    
    for m=1 to array.size(allResArray)-1
        int firstSearchElemIndex = array.indexof(allResArray, array.get(allResArray, m))
        int lastSearchElemIndex = array.lastindexof(allResArray, array.get(allResArray, m))
        if firstSearchElemIndex == lastSearchElemIndex or array.indexof(duplicatedIndex, lastSearchElemIndex) != -1
            continue
        string searchElem = array.get(allResArray, firstSearchElemIndex) 
        for i=firstSearchElemIndex+1 to lastSearchElemIndex
            string currentElem = array.get(allResArray, i)
            if searchElem == currentElem
                array.push(duplicatedIndex, i)
    removeUnused(duplicatedIndex)

poscond = tradeSignal > WeakBound
negcond = tradeSignal < -WeakBound
posseries = poscond ? tradeSignal : 0
negseries = negcond ? tradeSignal : 0
count_rising(plot) =>
    v_plot = plot > 0 ? plot : -plot
    var count = 0
    if v_plot == 0
        count := 0
    else if v_plot >= v_plot[1]
        count := min(5, count + 1)
    else if v_plot < v_plot[1]
        count := max(1, count - 1)
    count
poscount = count_rising(posseries)
negcount = count_rising(negseries)
_pc = poscond ? poscount : negcond ? negcount : 0

colorTransp(col, transp) =>
    red = color.r(col)
    green = color.g(col)
    blue = color.b(col)
    color.rgb(red, green, blue, transp)
    
//hline(1, color=colorTransp(colBuy, 50), linestyle=hline.style_solid)
//hline(0.5, color=colorTransp(colBuy, 50), linestyle=hline.style_dashed)
//hline(-1, color=colorTransp(colSell, 50), linestyle=hline.style_solid)
//hline(-0.5, color=colorTransp(colSell, 50), linestyle=hline.style_dashed)

f_cellBgColor(_signal) =>
    _returnColor = tableTitleColor
    if _signal == "Sell   "
        _returnColor := colSell
    else if _signal == "Strong\nSell"
        _returnColor := colStrongSell
    else if _signal == "Buy    "
        _returnColor := colBuy
    else if _signal == "Strong\nBuy "
        _returnColor := colStrongBuy
    else if _signal == "Neutral" or _signal == "-"
        _returnColor := colNeutral
    _returnColor
    

f_cellAlign(_cellTitle) =>
    _align = text.align_left
    if _cellTitle == "MAs" or _cellTitle == "Osc" or _cellTitle == "All" or _cellTitle == "-"
        _align := text.align_center
    _align

f_addCell(_table, _column, _row, _cellTitle) =>
    table.cell(_table, _column, _row, _cellTitle, text_color=color.white, text_halign=f_cellAlign(_cellTitle), bgcolor=f_cellBgColor(_cellTitle), text_size=size.small)

if barstate.islast
    deq = f_getOptimalFinancialBasic("DEBT_TO_EQUITY")
    dta = f_getOptimalFinancialBasic("DEBT_TO_ASSET")
    ldta = f_getOptimalFinancialBasic("LONG_TERM_DEBT_TO_ASSETS")
    altzscore = f_getOptimalFinancialBasic("ALTMAN_Z_SCORE")
    springate = f_getFinancials("SPRINGATE_SCORE", "FY")
    
    currentRatio = f_getOptimalFinancialBasic("CURRENT_RATIO")
    quickRatio = f_getOptimalFinancialBasic("QUICK_RATIO")
    sloanRatio = f_getOptimalFinancialBasic("SLOAN_RATIO")
    interestCover = f_getOptimalFinancialBasic("INTERST_COVER")
    
    roa = f_getOptimalFinancialBasic("RETURN_ON_ASSETS")
    roe = f_getOptimalFinancialBasic("RETURN_ON_EQUITY")
    roic = f_getOptimalFinancialBasic("RETURN_ON_INVESTED_CAPITAL")
    netMargin = f_getFinancials("NET_MARGIN", "FY")
    fcfMargin = f_getFinancials("FREE_CASH_FLOW_MARGIN", "FY")
    
    pfScore = f_getOptimalFinancialBasic("PIOTROSKI_F_SCORE")
    sgr = f_getOptimalFinancialBasic("SUSTAINABLE_GROWTH_RATE")
    
    
    orientation = Position == position.bottom_center or Position == position.middle_center or Position == position.top_center? 2:
                     Position == position.bottom_left or Position == position.middle_left or Position == position.top_left? 1 : 3
    
    var financialTable = table.new(position = Position, columns = 10, rows = 20, border_width = 1)
    columnSolvency = orientation == 1? 2 : orientation == 3? 0 : 0
    columnProfitability = orientation == 1? 2 : orientation == 3? 0 : 2
    columnLiquidity = orientation == 1? 2 : orientation == 3? 0 : 4
    columnGrowth = orientation == 1? 2 : orientation == 3? 0 : 6
    columnRisk = orientation == 1? 2 : orientation == 3? 0 : 8
    //columnStrenth = orientation == 1? 2 : orientation == 3? 0 : 10

    rowSolvency = orientation == 2? 0 : 0
    rowProfitability = orientation == 2? 0 : 5
    rowLiquidity = orientation == 2? 0 : 10
    rowGrowth = orientation == 2? 0 : 14
    rowRisk = orientation == 2? 0 : 16 
    //rowStrenth = orientation == 2? 0 : 19 

    columnValSolvency = orientation == 1? 0 : orientation == 3? 1 : 0
    columnValProfitability = orientation == 1? 0 : orientation == 3? 1 : 2
    columnValLiquidity = orientation == 1? 0 : orientation == 3? 1 : 4
    columnValGrowth = orientation == 1? 0 : orientation == 3? 1 : 6
    columnValRisk = orientation == 1? 0 : orientation == 3? 1 : 8
    //columnValStrenth = orientation == 1? 0 : orientation == 3? 1 : 10
    
    rowValSolvency = orientation == 2? 1 : 0
    rowValProfitability = orientation == 2? 1 : 5
    rowValLiquidity = orientation == 2? 1 : 10
    rowValGrowth = orientation == 2? 1 : 14
    rowValRisk = orientation == 2? 1 : 16
    //rowValStrenth = orientation == 2? 1 : 19
    
    if (viewFunda == false)
        viewSolvency := viewFunda
        viewProfit := viewFunda
        viewLiquidity := viewFunda
        viewGrowth := viewFunda
    
    if (viewTech == false)
        viewRisk := viewTech
        viewStrenth := viewTech
        
    if (viewSolvency == true) //solvency
        table.cell(table_id = financialTable, column = columnSolvency, row = rowSolvency , text = "Solvency", bgcolor=color.black, text_color=color.white, text_size=textSize)
    if (viewProfit == true) //profitability
        table.cell(table_id = financialTable, column = columnProfitability, row = rowProfitability, text = "Profitability", bgcolor=color.black, text_color=color.white, text_size=textSize)
    if (viewLiquidity == true) //viewLiquidity
        table.cell(table_id = financialTable, column = columnLiquidity, row = rowLiquidity, text = "Liquidity", bgcolor=color.black, text_color=color.white, text_size=textSize)
    if (viewGrowth == true) //Growth
        table.cell(table_id = financialTable, column = columnGrowth, row = rowGrowth, text = "Value/Growth", bgcolor=color.black, text_color=color.white, text_size=textSize)
    if (viewRisk == true) //Risk
        table.cell(table_id = financialTable, column = columnRisk, row = rowRisk , text = "Risk Management", bgcolor=color.black, text_color=color.white, text_size=textSize)
    //if (viewStrenth == true) //Risk
    //    table.cell(table_id = financialTable, column = columnStrenth, row = rowStrenth , text = "Technical Strength", bgcolor=color.black, text_color=color.white, text_size=textSize)

        if (viewSolvency == true) //solvency
            deq_score = na(deq)? 0 : deq > 0.0 and deq <= 1.0 ? 2 : deq > 1.0 and deq <= 2.0 ? 1 : deq > 2 ? -1 : -2
            table.cell(table_id = financialTable, column = columnValSolvency, row = rowValSolvency, text = "DEBT_TO_EQUITY", bgcolor=f_getColorByScore(deq_score), text_size=textSize)
            table.cell(table_id = financialTable, column = columnValSolvency+1, row = rowValSolvency, text = tostring(deq), bgcolor=f_getColorByScore(deq_score), text_size=textSize)
            
            dta_score = na(dta)? 0 : dta > 0.0 and dta <= 0.2 ? 2 : dta > 0.2 and dta <= 0.4 ? 1 : dta > 0.4 and dta <= 0.6 ? 0 : dta > 0.6 ? 1 : -2
            table.cell(table_id = financialTable, column = columnValSolvency, row = rowValSolvency+1, text = "DEBT_TO_ASSET", bgcolor=f_getColorByScore(dta_score), text_size=textSize)
            table.cell(table_id = financialTable, column = columnValSolvency+1, row = rowValSolvency+1, text = tostring(dta), bgcolor=f_getColorByScore(dta_score), text_size=textSize)

            ldta_score = na(ldta)? 0 : ldta > 0.0 and ldta <= 0.2 ? 2 : ldta > 0.2 and ldta <= 0.4 ? 1 : ldta > 0.4 and ldta <= 0.6 ? 0 : ldta > 0.6 ? 1 : -2
            table.cell(table_id = financialTable, column = columnValSolvency, row = rowValSolvency+2, text = "LONG_TERM_DEBT_TO_ASSETS", bgcolor=f_getColorByScore(ldta_score), text_size=textSize)
            table.cell(table_id = financialTable, column = columnValSolvency+1, row = rowValSolvency+2, text = tostring(ldta), bgcolor=f_getColorByScore(ldta_score), text_size=textSize)

            altz_score = na(altzscore)? 0 : altzscore > 5.0 ? 2 : altzscore > 3.0 ? 1 : altzscore > 1.8? 0 : altzscore > 1 ? -1 : -2
            table.cell(table_id = financialTable, column = columnValSolvency, row = rowValSolvency+3, text = "ALTMAN_Z_SCORE", bgcolor=f_getColorByScore(altz_score), text_size=textSize)
            table.cell(table_id = financialTable, column = columnValSolvency+1, row = rowValSolvency+3, text = tostring(altzscore), bgcolor=f_getColorByScore(altz_score), text_size=textSize)

            //springate_score = na(springate)? 0 : springate > 0.862 ? 1 : -1
            //table.cell(table_id = financialTable, column = columnValSolvency, row = rowValSolvency+4, text = "SPRINGATE_SCORE", bgcolor=f_getColorByScore(springate_score), text_size=textSize)
            //table.cell(table_id = financialTable, column = columnValSolvency+1, row = rowValSolvency+4, text = tostring(springate), bgcolor=f_getColorByScore(springate_score), text_size=textSize)
    
        if (viewProfit == true) //profitability
            roe_score = na(roe)? 0 : roe >= 40? 2 : roe >=10 ? 1 : roe >=0 ? 0 : roe >= -20? -1 : -2
            table.cell(table_id = financialTable, column = columnValProfitability, row = rowValProfitability, text = "RETURN_ON_EQUITY", bgcolor=f_getColorByScore(roe_score), text_size=textSize)
            table.cell(table_id = financialTable, column = columnValProfitability+1, row = rowValProfitability, text = tostring(roe), bgcolor=f_getColorByScore(roe_score), text_size=textSize)
    
            roa_score = na(roa)? 0 : roa >= 20? 2 : roa >=5 ? 1 : roa >=0 ? 0 : roa >= -10? -1 : -2
            table.cell(table_id = financialTable, column = columnValProfitability, row = rowValProfitability+1, text = "RETURN_ON_ASSETS", bgcolor=f_getColorByScore(roa_score), text_size=textSize)
            table.cell(table_id = financialTable, column = columnValProfitability+1, row = rowValProfitability+1, text = tostring(roa), bgcolor=f_getColorByScore(roa_score), text_size=textSize)
    
            roic_score = na(roic)? 0 : roic >= 10? 2 : roic >=2 ? 1 : roic >=0 ? 0 : roic >= -5? -1 : -2
            table.cell(table_id = financialTable, column = columnValProfitability, row = rowValProfitability+2, text = "RETURN_ON_INVESTED_CAPITAL", bgcolor=f_getColorByScore(roic_score), text_size=textSize)
            table.cell(table_id = financialTable, column = columnValProfitability+1, row = rowValProfitability+2, text = tostring(roic), bgcolor=f_getColorByScore(roic_score), text_size=textSize)
    
            netMargin_score = na(netMargin)? 0 : netMargin >= 20? 2 : netMargin >=10 ? 1 : netMargin >=0 ? 0 : netMargin >= -5? -1 : -2
            table.cell(table_id = financialTable, column = columnValProfitability, row = rowValProfitability+3, text = "NET_MARGIN", bgcolor=f_getColorByScore(netMargin_score), text_size=textSize)
            table.cell(table_id = financialTable, column = columnValProfitability+1, row = rowValProfitability+3, text = tostring(netMargin), bgcolor=f_getColorByScore(netMargin_score), text_size=textSize)
    
            fcfMargin_score = na(fcfMargin)? 0 : fcfMargin >= 15? 2 : fcfMargin >=10 ? 1 : fcfMargin >=0 ? 0 : fcfMargin >= -10? -1 : -2
            table.cell(table_id = financialTable, column = columnValProfitability, row = rowValProfitability+4, text = "FREE_CASH_FLOW_MARGIN", bgcolor=f_getColorByScore(fcfMargin_score), text_size=textSize)
            table.cell(table_id = financialTable, column = columnValProfitability+1, row = rowValProfitability+4, text = tostring(fcfMargin), bgcolor=f_getColorByScore(fcfMargin_score), text_size=textSize)
    
        if (viewLiquidity == true) //Liquidity
            currentRatio_score = na(currentRatio)? 0 : currentRatio >= 1.2? (currentRatio <= 2.0 ? 2 : 1) : currentRatio >=1 ? 0 : currentRatio >=0.5 ? -1 : -2
            table.cell(table_id = financialTable, column = columnValLiquidity, row = rowValLiquidity, text = "CURRENT_RATIO", bgcolor=f_getColorByScore(currentRatio_score), text_size=textSize)
            table.cell(table_id = financialTable, column = columnValLiquidity+1, row = rowValLiquidity, text = tostring(currentRatio), bgcolor=f_getColorByScore(currentRatio_score), text_size=textSize)
    
            quickRatio_score = na(quickRatio)? 0 : quickRatio >= 1.0? (quickRatio <= 2.0 ? 2 : 1) : quickRatio >=0.9 ? 0 : currentRatio >=0.4 ? -1 : -2
            table.cell(table_id = financialTable, column = columnValLiquidity, row = rowValLiquidity+1, text = "QUICK_RATIO", bgcolor=f_getColorByScore(quickRatio_score), text_size=textSize)
            table.cell(table_id = financialTable, column = columnValLiquidity+1, row = rowValLiquidity+1, text = tostring(quickRatio), bgcolor=f_getColorByScore(quickRatio_score), text_size=textSize)
    
            //sloanRatio_score = na(sloanRatio)? 0 : abs(sloanRatio) < 10.0? 1 : abs(sloanRatio) < 25.0? 0 : -1
            //table.cell(table_id = financialTable, column = columnValLiquidity, row = rowValLiquidity+2, text = "SLOAN_RATIO", bgcolor=f_getColorByScore(sloanRatio_score), text_size=textSize)
            //table.cell(table_id = financialTable, column = columnValLiquidity+1, row = rowValLiquidity+2, text = tostring(sloanRatio), bgcolor=f_getColorByScore(sloanRatio_score), text_size=textSize)
    
            interestCover_score = na(interestCover)? 0 : interestCover >3? 1 : interestCover >2? 0: -1
            table.cell(table_id = financialTable, column = columnValLiquidity, row = rowValLiquidity+3, text = "INTERST_COVER", bgcolor=f_getColorByScore(interestCover_score), text_size=textSize)
            table.cell(table_id = financialTable, column = columnValLiquidity+1, row = rowValLiquidity+3, text = tostring(interestCover), bgcolor=f_getColorByScore(interestCover_score), text_size=textSize)
        
        if (viewGrowth == true) //Value/Growth
            pf_score = na(pfScore)? 0 : pfScore >=8? 2 : pfScore >= 5 ? 1: pfScore >2 ? 0 : pfScore > 1? -1 : -2
            table.cell(table_id = financialTable, column = columnValGrowth, row = rowValGrowth, text = "PIOTROSKI_F_SCORE", bgcolor=f_getColorByScore(pf_score), text_size=textSize)
            table.cell(table_id = financialTable, column = columnValGrowth+1, row = rowValGrowth, text = tostring(pfScore), bgcolor=f_getColorByScore(pf_score), text_size=textSize)
    
            sgr_score = na(sgr)? 0 : sgr > 10? 2 : sgr > 5? 1 : sgr > 0? 0 : sgr > -5 ? -1 : -2
            table.cell(table_id = financialTable, column = columnValGrowth, row = rowValGrowth+1, text = "SUSTAINABLE_GROWTH_RATE", bgcolor=f_getColorByScore(sgr_score), text_size=textSize)
            table.cell(table_id = financialTable, column = columnValGrowth+1, row = rowValGrowth+1, text = tostring(sgr), bgcolor=f_getColorByScore(sgr_score), text_size=textSize)
    
    if (viewTech == true) //Value/
        if (viewRisk == true) //Value/Growth
        //    risk_score = na(sl)? 0 : sl > 10? 2 : sl > 5? 1 : sl > 0? 0 : sl > -5 ? -1 : -2
            table.cell(table_id = financialTable, column = columnValRisk, row = rowValRisk, text = "POSITION SIZE", bgcolor=#40dbc1, text_size=textSize)
            table.cell(table_id = financialTable, column = columnValRisk+1, row = rowValRisk, text = tostring(position), bgcolor=#40dbc1, text_size=textSize)
            table.cell(table_id = financialTable, column = columnValRisk, row = rowValRisk+1, text = "SL", bgcolor=#40dbc1, text_size=textSize)
            table.cell(table_id = financialTable, column = columnValRisk+1, row = rowValRisk+1, text = tostring(sl), bgcolor=#40dbc1, text_size=textSize)
            table.cell(table_id = financialTable, column = columnValRisk, row = rowValRisk+2, text = "INVESTMENT", bgcolor=#40dbc1, text_size=textSize)
            table.cell(table_id = financialTable, column = columnValRisk+1, row = rowValRisk+2, text = tostring(invetment), bgcolor=#40dbc1, text_size=textSize)
            
        //if (viewStrenth == true) //Strenth
        //    risk_score = na(sl)? 0 : sl > 10? 2 : sl > 5? 1 : sl > 0? 0 : sl > -5 ? -1 : -2
        //    table.cell(table_id = financialTable, column = columnValStrenth, row = rowValStrenth, text = "Current TF", bgcolor=f_getColorByScore(risk_score), text_size=textSize)
        //    table.cell(table_id = financialTable, column = columnValStrenth+1, row = rowValStrenth, text = tostring(position), bgcolor=f_getColorByScore(risk_score), text_size=textSize)
        //    table.cell(table_id = financialTable, column = columnValStrenth, row = rowValStrenth+1, text = "Daily", bgcolor=f_getColorByScore(risk_score), text_size=textSize)
        //    table.cell(table_id = financialTable, column = columnValStrenth+1, row = rowValStrenth+1, text = tostring(sl), bgcolor=f_getColorByScore(risk_score), text_size=textSize)
        //    table.cell(table_id = financialTable, column = columnValStrenth, row = rowValStrenth+2, text = "Weekly", bgcolor=f_getColorByScore(risk_score), text_size=textSize)
        //    table.cell(table_id = financialTable, column = columnValStrenth+1, row = rowValStrenth+2, text = tostring(invetment), bgcolor=f_getColorByScore(risk_score), text_size=textSize)
        //    table.cell(table_id = financialTable, column = columnValStrenth, row = rowValStrenth+3, text = "Monthly", bgcolor=f_getColorByScore(risk_score), text_size=textSize)
        //    //table.cell(table_id = financialTable, column = columnValStrenth+1, row = rowValStrenth+3, text = tostring(invetment), bgcolor=f_getColorByScore(risk_score), text_size=textSize)
        //    table.cell(table_id = financialTable, column = columnValStrenth+1, row = rowValStrenth+3, text = "Strong\nSell", text_color=color.white, bgcolor=f_cellBgColor("Strong\nSell"), text_size=textSize)
            
f_drawInfo() =>
    var t1 = table.new(position.top_right, 4, array.size(allRatingsArray), frame_width=2, frame_color=color.white, border_width=1, border_color=color.white)
    eraseDuplicatedAndDisabledTf()
    timeframesCount = array.size(allResArray)
    if timeframesCount < 3
        for i=0 to timeframesCount-1
            f_addCell(t1, i, 1, array.get(maRatingsArray, i))
            f_addCell(t1, i, 2, array.get(oscRatingsArray, i))
            f_addCell(t1, i, 3, array.get(allRatingsArray, i))
    else
        for i=0 to timeframesCount-1
            f_addCell(t1, 0, i, array.get(allResArray, i))
            if ratingSignal == "All"
                f_addCell(t1, 1, i, array.get(maRatingsArray, i))
                f_addCell(t1, 2, i, array.get(oscRatingsArray, i))
                f_addCell(t1, 3, i, array.get(allRatingsArray, i))
            if ratingSignal == "Oscillators"
                f_addCell(t1, 1, i, array.get(oscRatingsArray, i))
            if ratingSignal == "MAs"
                f_addCell(t1, 1, i, array.get(maRatingsArray, i))

col_buy = color.from_gradient(tradeSignal, 0.0, 0.2, colNeutral, colStrongBuy)
col_sell = color.from_gradient(tradeSignal, -0.2, 0.0, colStrongSell, colNeutral)
col_gradient = color.from_gradient(tradeSignal, -0.2, 0.2, col_sell, col_buy)

if barstate.islast
    if (viewStrenth == true)
        f_drawInfo()

//plot(tradeSignal, title="Rating", linewidth = 1, style = plot.style_columns, color = color.new(col_gradient, 50 - _pc * 10))
//_cond1 = crossunder(tradeSignal, -WeakBound)
//alertcondition(_cond1, "Sell", "Ratings changed to Sell")
//_cond2 = crossover(tradeSignal, WeakBound)
//alertcondition(_cond2, "Buy", "Ratings changed to Buy")
//_cond3 = crossunder(tradeSignal, -StrongBound)
//alertcondition(_cond3, "Strong Sell", "Ratings changed to Strong Sell")
//_cond4 = crossover(tradeSignal, StrongBound)
//alertcondition(_cond4, "Strong Buy", "Ratings changed to Strong Buy")