[GTH] Earnings
// 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] Earnings", scale=scale.right)
r1 = request.earnings(syminfo.tickerid, earnings.actual, ignore_invalid_symbol=true)
r2 = request.earnings(syminfo.tickerid, earnings.estimate, ignore_invalid_symbol=true)
col_p = input.color(title="Postive Surprise", defval=color.green)
col_n = input.color(title="Negative Surprise", defval=color.red)
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))
h_off = input.int(title="Horizontal Label Offset", defval=0)

col = r1 > r2 ? col_p : col_n

p1 = plot(r1, title="Actual Earnings", color=r1>0 ? col_p : col_n, linewidth=3, style = plot.style_stepline) 
p2 = plot(r2, title="Estimated Earnings",color= r2<r1 ? col_p : col_n, linewidth=1, style = plot.style_stepline, display=display.none)
fill(p1, p2, color=r1>r2 ? col_p_l : col_n_l)

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

bool plot_perc = r1 != r1[1] ? true : false

string form=str.replace(str.tostring(str.format("{0,number,percent}", math.abs(perc))), ",","",0)

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