本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// EARNINGS VALUATION BANDS — P/E HISTORY
// Five thin lines on a stock chart: earnings per share (trailing twelve months)
// times a price/earnings multiple. Where price sits between the lines shows
// how richly the market values each dollar of earnings, in the price scale.
//
// Band levels, two modes:
// Own P/E history (default): the multiples are the 10th, 25th, 50th, 75th
// and 90th percentile of the stock's OWN daily P/E over the lookback
// window. Each bar only uses P/E values from bars before it, so the lines
// never use future data; they drift as the history grows.
// Fixed P/E multiples: five multiples you choose (for example 15x).
//
// EPS source, two choices:
// Reported, diluted (TTM): request.financial, from the financial statements.
// Earnings releases: sum of the last four quarterly EPS actuals from
// request.earnings, the figure compared with analyst estimates (usually
// adjusted). It changes on the release date.
//
// A single text line at the chart edge shows EPS, current P/E, its percentile
// in the stock's own history, the median P/E and the distance of price to the
// median line. P/E is undefined while EPS is zero or negative; lines break there.
//
// The lines describe valuation, they are not support and resistance and not a
// fair value forecast.
//
// Chart timeframes: 1D, 1W or 1M. Stocks, ETFs with financial data, ADRs.
//
// MIT License, see LICENSE in the repository. Not financial advice.
indicator("Earnings Valuation Bands — P/E History", shorttitle = "P/E Bands", overlay = true, max_labels_count = 10)
//---- inputs -----------------------------------------------------------
grpBand = "Bands"
modeIn = input.string("Own P/E history", "Band levels", options = ["Own P/E history", "Fixed P/E multiples"], group = grpBand,
tooltip = "Own P/E history: multiples at the 10/25/50/75/90th percentile of this stock's own P/E over the lookback. Fixed: the five multiples below.")
epsIn = input.string("Reported, diluted (TTM)", "EPS source", options = ["Reported, diluted (TTM)", "Earnings releases (last 4 quarters)"], group = grpBand,
tooltip = "Reported: diluted EPS from the financial statements, trailing twelve months. Earnings releases: sum of the last four quarterly EPS actuals as published on the release date (usually adjusted, the number compared with estimates).")
m1 = input.float(10.0, "Fixed multiple 1", minval = 0.5, group = grpBand, inline = "f1")
m2 = input.float(15.0, "2", minval = 0.5, group = grpBand, inline = "f1")
m3 = input.float(20.0, "Fixed multiple 3", minval = 0.5, group = grpBand, inline = "f2")
m4 = input.float(30.0, "4", minval = 0.5, group = grpBand, inline = "f2")
m5 = input.float(40.0, "Fixed multiple 5", minval = 0.5, group = grpBand)
grpHist = "Own history"
yearsIn = input.int(10, "Lookback (years)", minval = 2, maxval = 30, group = grpHist,
tooltip = "Window for the P/E percentiles. Only bars with positive EPS count.")
minYears = input.float(2.0, "Minimum history (years)", minval = 0.5, maxval = 10.0, step = 0.5, group = grpHist,
tooltip = "Own-history lines start once this much P/E history exists before the bar.")
grpDisp = "Display"
midCol = input.color(color.new(#D08A2E, 0), "Median / middle line", group = grpDisp)
bandCol = input.color(color.new(#7E8A9A, 0), "Other lines", group = grpDisp)
lineW = input.int(1, "Line width", minval = 1, maxval = 4, group = grpDisp)
showLbl = input.bool(true, "Multiple at the right edge", group = grpDisp)
showText = input.bool(true, "Show text line", group = grpDisp)
textPos = input.string("Bottom center", "Text position", options = ["Bottom center", "Bottom left", "Bottom right", "Top center", "Top right"], group = grpDisp)
//---- checks -------------------------------------------------------------
if not timeframe.isdwm
runtime.error("Use a 1D, 1W or 1M chart. Earnings change once per quarter, intraday bars add nothing.")
YEAR_MS = 365.25 * 24 * 60 * 60 * 1000
bool useRel = epsIn == "Earnings releases (last 4 quarters)"
bool useOwn = modeIn == "Own P/E history"
//---- EPS ---------------------------------------------------------------
float epsRep = request.financial(syminfo.tickerid, "EARNINGS_PER_SHARE_DILUTED", "TTM", gaps = barmerge.gaps_off, ignore_invalid_symbol = true, currency = syminfo.currency)
float epsQ = request.earnings(syminfo.tickerid, earnings.actual, gaps = barmerge.gaps_on, ignore_invalid_symbol = true, currency = syminfo.currency)
// last four release actuals; a value arrives only on the bar of a release
var array<float> lastQ = array.new<float>()
if not na(epsQ)
lastQ.push(epsQ)
if lastQ.size() > 4
lastQ.shift()
float epsRel = lastQ.size() == 4 ? lastQ.sum() : na
float eps = useRel ? epsRel : epsRep
float pe = not na(eps) and eps > 0 ? close / eps : na
//---- own P/E history (only bars before the current one) -----------------
var array<float> peHist = array.new<float>()
var array<int> tHist = array.new<int>()
int cutoff = time - int(yearsIn * YEAR_MS)
while tHist.size() > 0 and tHist.first() < cutoff
tHist.shift()
peHist.shift()
float spanY = tHist.size() > 0 ? (time - tHist.first()) / YEAR_MS : 0.0
bool histOk = peHist.size() >= 20 and spanY >= minYears
float p10 = histOk ? peHist.percentile_linear_interpolation(10) : na
float p25 = histOk ? peHist.percentile_linear_interpolation(25) : na
float p50 = histOk ? peHist.percentile_linear_interpolation(50) : na
float p75 = histOk ? peHist.percentile_linear_interpolation(75) : na
float p90 = histOk ? peHist.percentile_linear_interpolation(90) : na
// percentile rank of a value inside the history
pctRank(array<float> a, float v) =>
int c = 0
for x in a
c += x <= v ? 1 : 0
a.size() > 0 ? 100.0 * c / a.size() : na
// rank of the current P/E, taken before this bar joins the history
float peRank = histOk and not na(pe) and barstate.islast ? pctRank(peHist, pe) : na
// the bar joins the history only after the levels of this bar are fixed
if barstate.isconfirmed and not na(pe)
peHist.push(pe)
tHist.push(time)
//---- band levels --------------------------------------------------------
float k1 = useOwn ? p10 : m1
float k2 = useOwn ? p25 : m2
float k3 = useOwn ? p50 : m3
float k4 = useOwn ? p75 : m4
float k5 = useOwn ? p90 : m5
bool epsOk = not na(eps) and eps > 0
float b1 = epsOk and not na(k1) ? eps * k1 : na
float b2 = epsOk and not na(k2) ? eps * k2 : na
float b3 = epsOk and not na(k3) ? eps * k3 : na
float b4 = epsOk and not na(k4) ? eps * k4 : na
float b5 = epsOk and not na(k5) ? eps * k5 : na
color outerCol = color.new(bandCol, 50)
plot(b1, "Line 1 (p10 / multiple 1)", color = outerCol, linewidth = lineW, style = plot.style_linebr)
plot(b2, "Line 2 (p25 / multiple 2)", color = bandCol, linewidth = lineW, style = plot.style_linebr)
plot(b3, "Line 3 (median / multiple 3)", color = midCol, linewidth = lineW + 1, style = plot.style_linebr)
plot(b4, "Line 4 (p75 / multiple 4)", color = bandCol, linewidth = lineW, style = plot.style_linebr)
plot(b5, "Line 5 (p90 / multiple 5)", color = outerCol, linewidth = lineW, style = plot.style_linebr)
//---- multiples at the right edge (plain text) -------------------------
fmtK(float k) =>
str.tostring(k, k >= 100 ? "0" : "0.0") + "x"
var array<label> tags = array.new<label>()
if barstate.islast
for l in tags
l.delete()
tags.clear()
if showLbl
array<float> ys = array.from(b1, b2, b3, b4, b5)
array<float> ks = array.from(k1, k2, k3, k4, k5)
array<string> ns = useOwn ? array.from("p10 ", "p25 ", "median ", "p75 ", "p90 ") : array.from("", "", "", "", "")
for i = 0 to 4
float y = ys.get(i)
if not na(y)
color c = i == 2 ? midCol : i == 0 or i == 4 ? outerCol : bandCol
tags.push(label.new(bar_index, y, " " + ns.get(i) + fmtK(ks.get(i)), style = label.style_label_left,
color = color.new(color.black, 100), textcolor = c, size = size.small))
//---- text line (plain text, no background) ----------------------------
string tPos = switch textPos
"Bottom left" => position.bottom_left
"Bottom right" => position.bottom_right
"Top center" => position.top_center
"Top right" => position.top_right
=> position.bottom_center
var table tb = table.new(tPos, 1, 1, frame_width = 0, border_width = 0)
if showText and barstate.islast
string srcTxt = useRel ? "EPS releases, last 4 q" : "EPS diluted TTM"
string s = ""
if na(eps)
s := "No earnings data for " + syminfo.ticker + (syminfo.type == "stock" or syminfo.type == "dr" or syminfo.type == "fund" ? "" : " (this works on stocks)")
if useRel and lastQ.size() > 0
s += ", fewer than 4 releases loaded"
else
s := srcTxt + " " + str.tostring(eps, "0.00") + " " + syminfo.currency
if eps <= 0
s += " | EPS not positive, P/E undefined"
else
s += " | P/E " + str.tostring(pe, "0.0")
if histOk
s += " | own " + str.tostring(spanY, "0.0") + " y: median " + str.tostring(p50, "0.0") + ", range p10 to p90 " + str.tostring(p10, "0.0") + " to " + str.tostring(p90, "0.0")
if not na(peRank)
s += " | current P/E at p" + str.tostring(peRank, "0")
if epsOk
s += " | price vs median line " + (close >= b3 ? "+" : "") + str.tostring(100.0 * (close / b3 - 1.0), "0.0") + " %"
if spanY < yearsIn - 0.5
s += " | history since " + str.format_time(tHist.first(), "yyyy-MM-dd") + ", shorter than the lookback"
else
s += " | own history " + str.tostring(spanY, "0.0") + " y, needs " + str.tostring(minYears, "0.0") + " y"
table.cell(tb, 0, 0, s, text_color = color.new(#7E8A9A, 0), text_size = size.small)