Financials | Revenue

heljohn · study · 246 行 · 点赞 11 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org
// © HelJohn
// Revenue[1.0]
 
 
//@version=6
indicator('Revenue', format = format.volume, precision = 2, overlay = false)
 
// --- Timeframe Check
if timeframe.isintraday
    runtime.error("⚠️ This indicator requires a Daily (D) timeframe or higher. Please switch your chart resolution.")
 
// --- Ticker
string InpTicker = ticker.new(syminfo.prefix, syminfo.ticker, session.extended)
 
 
 
// ---------- Input
output      = input.string('Actual',    'Output type',              group = '', options = ['Actual', 'Per Share'],                          tooltip = '𝗔𝗰𝘁𝘂𝗮𝗹 \nRaw reported total dollar/currency value. \n\n𝗣𝗲𝗿 𝗦𝗵𝗮𝗿𝗲 \nDivided by outstanding shares.')
period      = input.string('FQ/FH',     'Period',                   group = '', options = ['FQ/FH', 'FY'],                                  tooltip = '𝗙𝗤 – 𝗙𝗶𝗻𝗮𝗻𝗰𝗶𝗮𝗹 𝗤𝘂𝗮𝗿𝘁𝗲𝗿 \nA 3-month isolated report that updates four times per year.\n\n𝗙𝗛 – 𝗙𝗶𝗻𝗮𝗻𝗰𝗶𝗮𝗹 𝗦𝗲𝗺𝗶𝗮𝗻𝗻𝘂𝗮𝗹 \nA 6-month isolated report that updates twice a year.\n\n𝗙𝗬 – 𝗙𝗶𝗻𝗮𝗻𝗰𝗶𝗮𝗹 𝗬𝗲𝗮𝗿 \nA 12-month isolated report that updates once per year')
roc_mode    = input.string('YoY',       'Rate of Change',           group = '', options = ['YoY'],                                          tooltip = '𝗥𝗮𝘁𝗲 𝗼𝗳 𝗖𝗵𝗮𝗻𝗴𝗲 \nThe percentage growth comparison labels\n\n\n𝗬𝗼𝗬 ‑ 𝗬𝗲𝗮𝗿 𝗼𝘃𝗲𝗿 𝗬𝗲𝗮𝗿 \nCompares the period against the same period last year.')
rocLabAct   = input.bool(true,          'Rate of Change  ',         group = '', inline = 'i1')
modeLabAct  = input.bool(true,          'Mode',                     group = '', inline = 'i1')
 
q1_pos      = input.color(#62b0f0dd, "Reported    ",              group = '🎨 Visual', inline = 'p1')
q2_pos      = input.color(#3da2f5dd, "",                          group = '🎨 Visual', inline = 'p1')
q3_pos      = input.color(#1a91f1dd, "",                          group = '🎨 Visual', inline = 'p1')
q4_pos      = input.color(#0a77d1dd, "",                          group = '🎨 Visual', inline = 'p1')
 
 
// ---------- Input data
base_raw_fq     = request.financial(syminfo.tickerid, 'TOTAL_REVENUE', "FQ", barmerge.gaps_on, ignore_invalid_symbol=true)
base_raw_fh     = request.financial(syminfo.tickerid, 'TOTAL_REVENUE', "FH", barmerge.gaps_on, ignore_invalid_symbol=true)
base_raw_fy     = request.financial(syminfo.tickerid, 'TOTAL_REVENUE', "FY", barmerge.gaps_on, ignore_invalid_symbol=true)
close_ma5       = ta.sma(close, 5)
 
 
 
 
// Function     ............................................................................................................................................... 📜
 
// --- Scaling
scale_y_metric(output_type, rev_value, shares_count) =>
    float valid_shares = (na(shares_count) or shares_count <= 0) ? syminfo.shares_outstanding_total : shares_count
    float y_est_scaled = na
    float market_cap   = not na(valid_shares) ? (valid_shares * close_ma5) : na
    
    if output_type == 'Per Share'
        y_est_scaled := not na(valid_shares) ? (rev_value / valid_shares) : na
    
    else
        y_est_scaled := rev_value
    y_est_scaled
 
// --- Currency String Formatting
format_currency(value) =>
    if na(value)
        "N/A"
    else if value >= 1000000000 or value <= -1000000000
        str.tostring(value / 1000000000, "#.##") + "B"
    else if value >= 1000000 or value <= -1000000
        str.tostring(value / 1000000, "#.##") + "M"
    else
        str.tostring(value, "#.##")
 
// --- Output Text Formatter
format_output_text(out_type, scaled_val, raw_val) =>
    if na(scaled_val) and na(raw_val)
        "N/A"
    else if out_type == "Actual"
        format_currency(scaled_val)
    else if out_type == "Per Share" 
        str.tostring(scaled_val, "0.00")
    else
        str.tostring(raw_val, "0.00")
 
// --- Growth Processing
get_growth_data(r_mode, p_mode, float[] rev_prev, rev_now) =>
    int lookback_index = (p_mode == "FY" ? 1 : (p_mode == "FH" ? 2 : 4))
    float val_historical = array.size(rev_prev) > lookback_index ? array.get(rev_prev, lookback_index) : na
    float change_percent = na
    
    if na(val_historical)
        change_percent := 0.0 
    else if val_historical != 0
        bool crosses_zero = (rev_now * val_historical) < 0
        if crosses_zero
            change_percent := 0.0
        else
            change_percent := ((rev_now - val_historical) / math.abs(val_historical)) * 100
    math.round(change_percent, 2)
 
// --- Seasons for color
period_season(int end_time, string p_mode) =>
    int get_season = 1
    int Month = month(end_time, syminfo.timezone) 
    if p_mode == "FH"
        if Month >= 1 and Month <= 7
            get_season := 1
        else
            get_season := 2
    else
        if Month >= 2 and Month <= 4
            get_season := 1
        else if Month >= 5 and Month <= 7
            get_season := 2
        else if Month >= 8 and Month <= 10  
            get_season := 3
        else
            get_season := 4
    get_season
//
 
 
 
 
// --- 1. CODE     ............................................................................................................................................ 📜
 
// Auto Switching Intelligence ---
var int fq_updates_counter = 0
if not na(base_raw_fq)
    fq_updates_counter += 1
string period_mode = "FQ"
if period == "FY"
    period_mode := "FY"
else
    period_mode := (fq_updates_counter == 0 and not na(base_raw_fh)) ? "FH" : "FQ"
//
 
// --- Period data ---
float raw_req_data = period_mode == "FY" ? base_raw_fy : (period_mode == "FH" ? base_raw_fh : base_raw_fq)
//
 
 
// --- DATA | Outstanding Shares, Revenue & Time Track
float raw_rev_sync  = request.security(InpTicker, timeframe.period, raw_req_data, lookahead = barmerge.lookahead_on)
float shares_out    = request.financial(InpTicker, "TOTAL_SHARES_OUTSTANDING", period_mode, gaps = barmerge.gaps_off, ignore_invalid_symbol=true)
float financ_time   = request.financial(InpTicker, "TOTAL_REVENUE", period_mode, barmerge.gaps_off, ignore_invalid_symbol=true)
int   daily_time    = request.security(InpTicker, "D", time, lookahead = barmerge.lookahead_on)
 
 
 
// --- Variables
var int last_fiscal_date = na
 
 
// Code
float raw_gaps = period_mode == "FY" ? base_raw_fy : (period_mode == "FH" ? base_raw_fh : base_raw_fq)
bool is_new_report = not na(raw_gaps)
 
// Revenue
float revenue_data = scale_y_metric(output, raw_rev_sync, shares_out)
 
 
 
 
// --- 2. REVENUE Visual     ................................................................................................................................... 📜
 
var int[]   time_history       = array.new_int()
var int[]   table_date_history = array.new_int()
var float[] revenue_history    = array.new_float()
var float[] raw_history        = array.new_float() 
var int[]   period_end_times   = array.new_int()
var int[]   season_end_times   = array.new_int()
 
if ta.change(financ_time) != 0 or is_new_report
    array.insert(period_end_times, 0, time_close)
    array.insert(season_end_times, 0, daily_time)
    if array.size(period_end_times) > 500
        array.pop(period_end_times)
        array.pop(season_end_times)
//
 
 
// DRAW
if is_new_report 
    int true_period_end = array.size(period_end_times) > 0 ? array.get(period_end_times, 0) : time_close
    int true_daily_date = array.size(season_end_times) > 0 ? array.get(season_end_times, 0) : daily_time
    int true_fiscal_date = true_daily_date
 
    //
    if not na(last_fiscal_date)
        true_fiscal_date := last_fiscal_date
 
    array.insert(time_history, 0, true_period_end)
    array.insert(table_date_history, 0, true_fiscal_date)
    array.insert(revenue_history, 0, revenue_data)
    array.insert(raw_history, 0, raw_rev_sync) 
 
    if array.size(time_history) > 500
        array.pop(time_history)
        array.pop(table_date_history)
        array.pop(revenue_history)
        array.pop(raw_history)
 
 
// DRAW
    if array.size(time_history) >= 2
        int   box_start_time = array.get(time_history, 1)
        int   box_end_time   = array.get(time_history, 0)
        int   middle_time    = int(math.avg(box_start_time, box_end_time))
        float top_y          = array.get(revenue_history, 0)
        float raw_val        = array.get(raw_history, 0) 
        
    
        // 🎨 Visual
        int   true_fiscal_date_color = array.get(table_date_history, 0)
        int   color_idx = array.size(revenue_history)
        int   color_season = period_season(true_fiscal_date_color, period_mode)
        
        color q_color = na
        if period_mode == "FY"
            q_color := color_idx % 4 == 0 ? q1_pos : color_idx % 4 == 1 ? q2_pos : color_idx % 4 == 2 ? q3_pos : q4_pos
        else if period_mode == "FH"
            q_color := color_season == 1 ? q1_pos : q3_pos
        else
            q_color := color_season == 1 ? q1_pos : color_season == 2 ? q2_pos : color_season == 3 ? q3_pos : q4_pos
        //
 
        float growth_rate   = get_growth_data(roc_mode, period_mode, revenue_history, top_y)
        color roc_bg_color  = (growth_rate > 0) ? #537961 : (growth_rate < 0 ? #a54848 : #a8a8a8)
        string value_text   = format_output_text(output, top_y, raw_val) 
        string roc_text     = rocLabAct ? (growth_rate > 0 ? "+" : "") + str.tostring(growth_rate, "0.00") + "%" : ""
 
 
        // 🖊️ Draw
        if not na(top_y)
            box.new(box_start_time, top_y, box_end_time, 0.0, xloc=xloc.bar_time, border_color=#0a77d120, border_width=2, bgcolor=q_color)
            if true
                label.new(middle_time, 0.0, value_text, xloc=xloc.bar_time, yloc=yloc.price, color=#363a45a0, textcolor=color.white, style=label.style_label_center, size=size.small)
            if rocLabAct
                label.new(middle_time, top_y, roc_text, xloc=xloc.bar_time, yloc=yloc.price, color=roc_bg_color, textcolor=color.white, style=label.style_label_center, size=size.small)
//
 
 
 
 
 
// --- 3. 🛈 Info
var table modeBox = table.new(position.bottom_left, 1, 1)
if modeLabAct and barstate.islast
    table.cell(modeBox, 0, 0, output, text_color = #000000, bgcolor = #ffffffe0, text_size = size.small,            tooltip = '𝗔𝗰𝘁𝘂𝗮𝗹 \nRaw reported total dollar/currency value. \n\n𝗣𝗲𝗿 𝗦𝗵𝗮𝗿𝗲 \nDivided by outstanding shares.')
 
var table infoBox = table.new(position.bottom_right, 1, 1)
if barstate.islast
    table.cell(infoBox, 0, 0, '🛈', text_color = #000000, bgcolor = #ffffff75, text_size = size.normal ,            tooltip = '𝗥𝗲𝘃𝗲𝗻𝘂𝗲 \nRepresents the entire amount of money a business brings in through the sale of its goods or services before any expenses are deducted')

← 返回列表