Quarterly Revenue Label
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Deriko_  
//@version=5
indicator("Quarterly Revenue", overlay=true)//, scale=scale.left))

//-------------------------//
//-----USER INPUTS---------//
//-------------------------//
p_arrowColor   = input(color.black, title='Shape Color', inline="2")
p_TextColor     = input(color.new(color.white,0), title='Revenue Text Color', inline="3")
p_Textsize      = input.string("default", title="Label Size", inline="4",options=["very small", "small", "default", "large", "very large"])
p_Shapes        = input.string("triangleup", title="Shape dDefault", inline="4",options=["xcross", "cross", "triangleup", "triangledown","flag","square","diamond"])
p_posShapeColor= input(color.rgb(91, 209, 203), title='Positive Text Color' ,inline="5")
p_negShapeColor= input(color.rgb(245, 110, 110), title='Negative Text Color' , inline="6")

labelSizeOption = switch p_Textsize
    "very small" => size.tiny
    "small"      => size.small
    "default"    => size.normal
    "large"      => size.large
    "very large" => size.huge

labelShapeOption = switch p_Shapes 
    "xcross" => label.style_xcross
    "cross" => label.style_cross
    "triangleup" => label.style_triangleup //default
    "triangledown" => label.style_triangledown
    "flag" => label.style_flag
    "circle" => label.style_circle
    //"arrowup" => label.style_arrowup 
    //"arrowdown" => label.style_arrowdown
    "square" => label.style_square
    "diamond" => label.style_diamond
    
// Financial Data
TotREV = request.financial(syminfo.tickerid,'TOTAL_REVENUE','FQ', ignore_invalid_symbol=true)
EPS = request.earnings(syminfo.tickerid, earnings.actual, ignore_invalid_symbol=true)
//------------------------//
//---------REVENUE--------//
//------------------------//
revenueBarsNumber = ta.barssince(TotREV != TotREV[1]) == 0
revenue = TotREV
prevRev = ta.valuewhen(revenueBarsNumber, TotREV, 1)
//plotchar(revenue, "revenue", "", location = location.top) 

// Revenue QoQ
RevenueChangeQoQ = (revenue  -prevRev) /math.abs(prevRev) *100
//plotchar(revenue, "revenue", "", location = location.top) 
//plotchar(revenue1, "revenue1", "", location = location.top) 

//bars since the last change
vChangedetected = ta.barssince(EPS != EPS[1]) == 0  

// value since change
revValue = ta.valuewhen(vChangedetected, TotREV, 0) 
//plotchar(vChangedetected, "salesValue", "", location = location.top) 

textHeader = 'Revenue '
textRevenueChanged = RevenueChangeQoQ > 999 ? '\n+999%':RevenueChangeQoQ > 0 ? '\n+' + str.tostring(RevenueChangeQoQ, '0') + '%':'\n' + str.tostring(RevenueChangeQoQ, '0') + '%'
textTotals = revValue > 999999999 ? str.tostring(revValue/1000000000,'0.00B') :str.tostring(revValue/1000000,'0.00M')
//--------------------//
//----PLOT VALUES-----//
//--------------------//
if(vChangedetected)
    label1 = label.new(bar_index, bar_index, xloc=xloc.bar_index, yloc=yloc.belowbar, text=textHeader, style=labelShapeOption, color=color.new(color.aqua,100), textcolor=p_TextColor, size=labelSizeOption)
if(vChangedetected)
    label2 = label.new(bar_index, bar_index, xloc=xloc.bar_index, yloc=yloc.belowbar, text=textRevenueChanged+' | '+ textTotals, style=labelShapeOption, color=p_arrowColor, textcolor=RevenueChangeQoQ > 0 ? p_posShapeColor:p_negShapeColor, size=labelSizeOption)
   