本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org
// © HelJohn
// Earnings[1.0]
//@version=6
indicator('Earnings', 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('Per Share', 'Output type', group = '', options = ['Per Share', 'Actual'], tooltip = '𝗣𝗲𝗿 𝗦𝗵𝗮𝗿𝗲 \nEarnings Per Share (EPS) \n\n𝗔𝗰𝘁𝘂𝗮𝗹 \nRaw total dollar/currency value.')
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(#5af0b1dd, "Positive ", group = '🎨 Visual', inline = 'p1')
q2_pos = input.color(#35d894dd, "", group = '🎨 Visual', inline = 'p1')
q3_pos = input.color(#1bbd79dd, "", group = '🎨 Visual', inline = 'p1')
q4_pos = input.color(#0e995ddd, "", group = '🎨 Visual', inline = 'p1')
q1_neg = input.color(#ff8a8add, "Negative ", group = '🎨 Visual', inline = 'p2')
q2_neg = input.color(#f55050de, "", group = '🎨 Visual', inline = 'p2')
q3_neg = input.color(#db2d2ddd, "", group = '🎨 Visual', inline = 'p2')
q4_neg = input.color(#940f0fdd, "", group = '🎨 Visual', inline = 'p2')
// ---------- Input data
base_raw_fq = request.financial(InpTicker, 'EARNINGS_PER_SHARE_BASIC', "FQ", barmerge.gaps_on, ignore_invalid_symbol = true)
base_raw_fh = request.financial(InpTicker, 'EARNINGS_PER_SHARE_BASIC', "FH", barmerge.gaps_on, ignore_invalid_symbol = true)
base_raw_fy = request.financial(InpTicker, 'EARNINGS_PER_SHARE_BASIC', "FY", barmerge.gaps_on, ignore_invalid_symbol = true)
close_ma5 = ta.sma(close, 5)
// Function ........................................................................................................................................................... 📜
// --- Scaling
scale_y_metric(output_type, eps_value, shares_count, rev_val) =>
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 == 'Actual'
y_est_scaled := not na(valid_shares) ? (eps_value * valid_shares) : na
else
y_est_scaled := eps_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
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"
//
// Timeframe
string safe_tf = timeframe.isintraday ? "D" : timeframe.period
// --- DATA | Outstanding Shares, Revenue & Time Track
float raw_eps_event = request.earnings(InpTicker, earnings.actual, gaps = barmerge.gaps_off, ignore_invalid_symbol = true)
float estimate_event= request.earnings(InpTicker, earnings.estimate, gaps = barmerge.gaps_off, ignore_invalid_symbol = true)
float fin_eps_fy = request.financial(InpTicker, "EARNINGS_PER_SHARE_BASIC", "FY", barmerge.gaps_off, ignore_invalid_symbol = true)
float rev_sync = request.financial(InpTicker, "TOTAL_REVENUE", period_mode, ignore_invalid_symbol=true)
float shares_out = request.financial(InpTicker, "TOTAL_SHARES_OUTSTANDING", period_mode, ignore_invalid_symbol=true)
float financ_time = request.financial(InpTicker, "EARNINGS_PER_SHARE_BASIC", period_mode, barmerge.gaps_off, ignore_invalid_symbol=true)
int daily_time = request.security(InpTicker, "D", time, lookahead = barmerge.lookahead_on, ignore_invalid_symbol = true)
// --- DATA | Future
int future_time = earnings.future_period_end_time
// --- Variables
var float eps_now = na
var float estimate_now = na
bool future_time_changed = ta.change(future_time) != 0
bool financ_time_changed = ta.change(financ_time) != 0
float raw_eps_sync = period_mode == "FY" ? base_raw_fy : raw_eps_event
bool estimate_changed = not na(estimate_event) and (na(estimate_now) or estimate_event != estimate_now)
bool is_new_report_earn = (nz(raw_eps_sync) != nz(eps_now) or (period_mode != "FY" and estimate_changed) or (period_mode != "FY" and future_time_changed)) and not na(raw_eps_sync)
if is_new_report_earn
eps_now := raw_eps_sync
estimate_now := estimate_event
// Earnings
float earn_data = scale_y_metric(output, raw_eps_sync, shares_out, rev_sync)
// --- 2. EARNINGS Visual ................................................................................................................................................. 📜
var box last_hist_box = na
var label last_val_lbl = na
var label last_roc_lbl = na
var int[] period_end_times = array.new_int()
var int[] exact_daily_times = array.new_int()
var int[] time_history = array.new_int()
var int[] table_date_history = array.new_int()
var float[] earn_history = array.new_float()
var float[] raw_history = array.new_float()
if financ_time_changed
array.insert(period_end_times, 0, time_close)
array.insert(exact_daily_times, 0, daily_time)
if array.size(period_end_times) > 500
array.pop(period_end_times)
array.pop(exact_daily_times)
//
// DRAW TRIGGER
if is_new_report_earn
int true_period_end = array.size(period_end_times) > 0 ? array.get(period_end_times, 0) : time_close
int true_daily_date = array.size(exact_daily_times) > 0 ? array.get(exact_daily_times, 0) : daily_time
int true_fiscal_date = true_daily_date
if future_time_changed and not na(future_time[1])
true_fiscal_date := future_time[1]
array.insert(time_history, 0, true_period_end)
array.insert(table_date_history, 0, true_fiscal_date)
array.insert(earn_history, 0, earn_data)
array.insert(raw_history, 0, raw_eps_sync)
if array.size(time_history) > 500
array.pop(time_history)
array.pop(table_date_history)
array.pop(earn_history)
array.pop(raw_history)
// 🖊️ Draw
if array.size(time_history) >= 2 and array.size(earn_history) >= 1
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(earn_history, 0)
float raw_val = array.get(raw_history, 0)
int true_fiscal_date_color = array.get(table_date_history, 0)
// 🎨 Visual
bool earn_pos = top_y > 0
int color_idx = array.size(earn_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 ? (earn_pos ? q1_pos : q1_neg) : (earn_pos ? q3_pos : q3_neg)
else
q_color := color_season == 1 ? (earn_pos ? q1_pos : q1_neg) : color_season == 2 ? (earn_pos ? q2_pos : q2_neg) : color_season == 3 ? (earn_pos ? q3_pos : q3_neg) : (earn_pos ? q4_pos : q4_neg)
//
float growth_rate = get_growth_data(roc_mode, period_mode, earn_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") + "%" : ""
if not na(top_y)
last_hist_box := 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
last_val_lbl := 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
last_roc_lbl := 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)
//
// --- 5. 🛈 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 = '𝗣𝗲𝗿 𝗦𝗵𝗮𝗿𝗲 \nEarnings Per Share (EPS) \n\n𝗔𝗰𝘁𝘂𝗮𝗹 \nRaw total dollar/currency value.')
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 net profit a business keeps after all expenses, costs, taxes, and interest are deducted')