Financial Radar Chart by zdmre
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © zdmre

//@version=5
indicator('Financial Radar Chart by zdmre', overlay=false, max_lines_count=500, max_labels_count=500)
//inputs
period = input.string('FY', 'Period', options=['FQ', 'FY'], tooltip='FQ: Quarter\nFY: Annual')
sh_plot = input.bool(true,title="Show Plot&Tag")
radius = input.int(100, "Radius", group="Style")
offset = input.int(20, "Offset", group="Style")

//datas
de = request.financial(syminfo.tickerid,'TOTAL_DEBT', period) / request.financial(syminfo.tickerid,'TOTAL_EQUITY', period) 
roic = request.financial(syminfo.tickerid,'RETURN_ON_INVESTED_CAPITAL', period)
evtoebitda = request.financial(syminfo.tickerid,'ENTERPRISE_VALUE_EBITDA', period)
dyield = request.financial(syminfo.tickerid,'DIVIDENDS_YIELD', period)
roe = request.financial(syminfo.tickerid,'RETURN_ON_EQUITY', period)
opmar = request.financial(syminfo.tickerid, 'OPERATING_MARGIN', period)
TSO = request.financial(syminfo.tickerid, "TOTAL_SHARES_OUTSTANDING", period)
FCF= request.financial(syminfo.tickerid, "FREE_CASH_FLOW", period)
MarketCap = TSO * close
pfcf = MarketCap / FCF
rg = request.financial(syminfo.tickerid, "REVENUE_ONE_YEAR_GROWTH", "FY")

//calculations
de_val =  de < 0 ? 0 : de < 0.1 ? 1 : de < 0.2 ? 0.9 :  de < 0.3 ? 0.8 : de < 0.4 ? 0.7 : de < 0.5 ? 0.6 : de < 0.6 ? 0.5 : de < 0.7 ? 0.4 : de < 0.8 ? 0.3 : de < 0.9 ? 0.2 : de < 1 ? 0.1 : 0 
roic_val = roic < 0 ? 0 : roic > 50 ? 1 : roic > 40 ? 0.9 :  roic > 30 ? 0.8 :  roic > 20 ? 0.7 : roic > 10 ? 0.5 : roic > 5 ? 0.2 : 0
evtoebitda_val = evtoebitda < 0 ? 0 : evtoebitda < 3 ? 1 : evtoebitda < 5 ? 0.8 : evtoebitda < 7 ? 0.7 : evtoebitda < 8 ? 0.6 : evtoebitda < 10 ? 0.4 : evtoebitda < 12 ? 0.2 : 0
dyield_val = dyield > 0.5 ? 1 : dyield > 0.3 ? 0.9 : dyield > 0.2 ? 0.8 : dyield > 0.1 ? 0.7 : dyield > 0 ? 0.5 : 0
roe_val = roe < 0 ? 0 : roe > 50 ? 1 : roe > 40 ? 0.9 : roe > 30 ? 0.8 : roe > 20 ? 0.7 : roe > 10 ? 0.5 : roe > 5 ? 0.2 : 0 
opmar_val = opmar < 0 ? 0 : opmar > 50 ? 1 : opmar > 30 ? 0.9 : opmar > 20 ? 0.8 : opmar > 15 ? 0.6 : opmar > 10 ? 0.4 : opmar > 0 ? 0.2 : 0
pfcf_val = pfcf < 0 ? 0 : pfcf < 5 ? 1 : pfcf < 7 ? 0.9 : pfcf < 10 ? 0.8 : pfcf < 16 ? 0.6 : pfcf < 18 ? 0.5 : pfcf < 20 ? 0.4 : pfcf < 22 ? 0.3 : pfcf < 30 ? 0.2 : pfcf < 40 ? 0.15 : pfcf < 50 ? 0.1 : pfcf < 60 ? 0.05 : 0
rg_val = rg > 0.2 ? 1: rg > 0.16 ? 0.9 : rg > 0.14 ? 0.8 : rg > 0.12 ? 0.7 : rg > 0.1 ? 0.5 : rg > 0.07 ? 0.4 : rg > 0.04 ? 0.3 : rg > 0.02 ? 0.2 : rg > 0 ? 0.1 : 0

//plots
plot(sh_plot ? de_val : na, title="Debt-Paying ability", color=color.yellow, style=plot.style_stepline_diamond, linewidth=2, display=display.pane)
plot(sh_plot ? roic_val : na, title="ROIC",  color=color.aqua, style=plot.style_stepline_diamond, linewidth=2, display=display.pane)
plot(sh_plot ? evtoebitda_val : na, title="EV/EBITDA",  color=color.purple, style=plot.style_stepline_diamond, linewidth=2, display=display.pane)
plot(sh_plot ? dyield_val : na, title="Dividend",  color=color.green, style=plot.style_stepline_diamond, linewidth=2, display=display.pane)
plot(sh_plot ? roe_val : na, title="ROE",  color=color.red, style=plot.style_stepline_diamond, linewidth=2, display=display.pane)
plot(sh_plot ? opmar_val : na, title="Operating ability",  color=color.orange, style=plot.style_stepline_diamond, linewidth=2, display=display.pane)
plot(sh_plot ? pfcf_val : na, title="Free Cash ability",  color=color.blue, style=plot.style_stepline_diamond, linewidth=2, display=display.pane)
plot(sh_plot ? rg_val : na, title="One Year Growth ability",  color=color.navy, style=plot.style_stepline_diamond, linewidth=2, display=display.pane)

//labels
label de_lbl = sh_plot ? label.new(bar_index+1, de_val, "Debt:"+str.tostring( de_val*100), color=color.yellow, textcolor=color.black, size = size.small, style=label.style_label_left) : na, label.delete(de_lbl[1])
label roic_lbl = sh_plot ? label.new(bar_index+1, roic_val, "ROIC:"+str.tostring( roic_val*100), color=color.aqua, textcolor=color.black, size = size.small, style=label.style_label_left) : na ,label.delete(roic_lbl[1])
label evtoebitda_lbl = sh_plot ? label.new(bar_index+1, evtoebitda_val, "EV/Ebitda:"+str.tostring( evtoebitda_val*100), color=color.purple, textcolor=color.white, size = size.small, style=label.style_label_left) : na, label.delete(evtoebitda_lbl[1])
label dyield_lbl = sh_plot ? label.new(bar_index+1, dyield_val, "Dividend:"+str.tostring( dyield_val*100), color=color.green, textcolor=color.black, size = size.small, style=label.style_label_left) : na, label.delete(dyield_lbl[1])
label roe_lbl = sh_plot ? label.new(bar_index+1, roe_val, "ROE:"+str.tostring( roe_val*100), color=color.red, textcolor=color.black, size = size.small, style=label.style_label_left) : na, label.delete(roe_lbl[1])
label opmar_lbl = sh_plot ? label.new(bar_index+1, opmar_val, "Operating:"+str.tostring( opmar_val*100), color=color.orange, textcolor=color.black, size = size.small, style=label.style_label_left) : na, label.delete(opmar_lbl[1])
label pfcf_lbl = sh_plot ? label.new(bar_index+1, pfcf_val, "Cash:"+str.tostring(pfcf_val*100), color=color.blue, textcolor=color.black, size = size.small, style=label.style_label_left) : na, label.delete(pfcf_lbl[1])
label rg_lbl = sh_plot ? label.new(bar_index+1, rg_val, "Growth:"+str.tostring(rg_val*100), color=color.navy, textcolor=color.white, size = size.small, style=label.style_label_left) : na, label.delete(rg_lbl[1])

//arrays
var categories = array.new_string(0)
if barstate.isfirst
    array.push(categories, 'DEBT PAYING Ability')
    array.push(categories, 'EV/EBITDA')
    array.push(categories, 'DIVIDEND')
    array.push(categories, 'ROE')
    array.push(categories, 'ROIC')
    array.push(categories, 'OPERATING Ability')
    array.push(categories, 'FREE CASH Ability')
    array.push(categories, 'GROWTH Ability')

values = array.new_float(0)
if barstate.islast
    array.push(values, de_val)
    array.push(values, evtoebitda_val)
    array.push(values, dyield_val)
    array.push(values, roe_val)
    array.push(values, roic_val)
    array.push(values, opmar_val)
    array.push(values, pfcf_val)
    array.push(values, rg_val)
    array.push(values, array.get(values, 0))

dt = math.round(time - time[1])
rdr = math.pi * 2 / array.size(categories)
var border = array.new_line(0)
var d_lines0 = array.new_line(0)
var d_linesa = array.new_line(0)
var d_linesb = array.new_line(0)
var d_linesc = array.new_line(0)
var d_linesd = array.new_line(0)
var value_lines = array.new_line(0)
var name_labels = array.new_label(0)

if barstate.isfirst
    for i = 0 to array.size(categories)-1 by 1
        array.push(border, line.new(na, na, na, na))
        array.push(d_lines0, line.new(na, na, na, na))
        array.push(d_linesa, line.new(na, na, na, na))
        array.push(d_linesb, line.new(na, na, na, na))
        array.push(d_linesc, line.new(na, na, na, na))
        array.push(d_linesd, line.new(na, na, na, na))
        array.push(value_lines, line.new(na, na, na, na))
        array.push(name_labels, label.new(na, na, na))

//get coordinates
get_cord(x, y, z) =>
    y1 = math.sin(rdr * x) * y
    y2 = math.sin(rdr * (x + 1)) * z
    x1 = time + dt * offset + dt * math.round((.5 * math.cos(rdr * x) * y + .5) * radius)
    x2 = time + dt * offset + dt * math.round((.5 * math.cos(rdr * (x + 1)) * z + .5) * radius)
    [x1, y1, x2, y2]
    
//draw lines
d_line(dl, i, x1_l, x2_l, y1_l, y2_l, wd, ls, color) =>
    x_line = array.get(dl, i)
    line.set_xloc(x_line, x1_l, x2_l, xloc=xloc.bar_time)
    line.set_y1(x_line, y1_l)
    line.set_y2(x_line, y2_l)
    line.set_style(x_line, ls)
    line.set_color(x_line, color)
    line.set_width(x_line, wd)  

if barstate.islast
    for i = 0 to array.size(categories)-1 by 1
//get arrays        
        val_0 = array.get(values, i)
        val_1 = array.get(values, i + 1)
        z_line = array.get(d_lines0, i)
        value_line = array.get(value_lines, i)   

//lines
        [x1_z, y1_z, x2_z, y2_z] = get_cord(i, 0, 0)
        [x1, y1, x2, y2] = get_cord(i, 1, 1)
        [x1_a, y1_a, x2_a, y2_a] = get_cord(i, .2, .2)
        [x1_b, y1_b, x2_b, y2_b] = get_cord(i, .4, .4)
        [x1_c, y1_c, x2_c, y2_c] = get_cord(i, .6, .6)
        [x1_d, y1_d, x2_d, y2_d] = get_cord(i, .8, .8)
        [x1_val, y1_val, x2_val, y2_val] = get_cord(i, val_0, val_1) 
        d_line(d_lines0, i, x1_z, x2_z, y1_z, y2_z, 1, line.style_dotted, color.gray)
        d_line(d_linesa, i, x1_a, x2_a, y1_a, y2_a, 1, line.style_dotted, color.gray)
        d_line(d_linesb, i, x1_b, x2_b, y1_b, y2_b, 1, line.style_dotted, color.gray)
        d_line(d_linesc, i, x1_c, x2_c, y1_c, y2_c, 1, line.style_dotted, color.gray)
        d_line(d_linesd, i, x1_d, x2_d, y1_d, y2_d, 1, line.style_dotted, color.gray)
        d_line(border, i, x1, x2, y1, y2, 2, line.style_solid, color.gray)
        d_line(value_lines, i , x1_val, x2_val, y1_val, y2_val, 1, line.style_solid, color.green)        
//fillcolor
        linefill.new(value_line, z_line, color.new(color.green,50))   

//labels
        d_x1 = time + dt * offset + dt * math.round(radius / 2)
        label_loc = y1 == 1 ? label.style_label_down : y1 == -1 ? label.style_label_up : x1 > d_x1 ? label.style_label_left : label.style_label_right
        osc_name = array.get(categories, i)
        name_label = array.get(name_labels, i)
        label.set_xloc(name_label, x1, xloc=xloc.bar_time)
        label.set_y(name_label, y1)
        label.set_text(name_label, osc_name)
        label.set_textcolor(name_label, color.gray)
        label.set_color(name_label, #ffffff10)
        label.set_style(name_label, label_loc)