[GTH] Net Profit Margin (%)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gehteha

//@version=5
indicator(title="[GTH] Net Profit Margin (%)", overlay=false)
np = request.financial(syminfo.tickerid, "NET_MARGIN", "FQ")

col_p = input.color(title="Positive", defval=color.green, group="Coloring")
col_n = input.color(title="Negative", defval=color.red, group="Coloring")

col_p_l = input.color(title="Postive Shade", defval=color.rgb(0, 182, 6, 88))
col_n_l = input.color(title="Negative Shade", defval=color.rgb(255, 73, 73, 79))

col_line = input.color(title="Line", defval=color.black, group="Coloring")

r1 = np>0 ? 0 : np
r2 = np>0 ? np : 0
col = np>0 ? col_p : col_n
col_l = np>0 ? col_p_l : col_n_l

p1 = plot(np, title="Net Profit Margin", color=col, linewidth=3, style = plot.style_stepline)
p2 = plot(0, title="Shade", color=col_l, linewidth=1, style = plot.style_stepline, display = display.none)

fill(p1, p2, title="Shade", color=col_l)

float diff = np-np[1]
perc = np != np[1] ? (np/np[1])-1 : na

string form=str.replace(str.tostring(str.format("{0,number,percent}", math.abs(perc))), ",","",0)
plot_perc = np != np[1] ? true : false

if plot_perc
    label.new(bar_index, np, form, color = diff>0 ? col_p : col_n, style = diff>0 ? label.style_label_down : label.style_label_up, textcolor=color.white)

hline(0, title="Zero Line", color=color.black, linewidth=1, linestyle=hline.style_solid)
