本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("PE Rating by The Noiseless Trader", overlay=false, precision=2)
// ——— Inputs
lookbackBars = input.int(756, "Lookback (trading days) for PE High/Low", minval=30)
lookbackBarsInt = input.int(252, "Lookback (trading days) for PE (Int)", minval=30)
showPEline = input.bool(true, "Show P/E line")
markerStyle = input.string("Dot", "Extremes marker", options = ["Dot","Vertical line"])
// ——— Daily price & Diluted EPS (TTM)
priceD = request.security(syminfo.tickerid, "D", close)
epsD = request.financial(syminfo.tickerid, "EARNINGS_PER_SHARE_DILUTED", "TTM")
// ——— P/E = Price ÷ Diluted EPS (TTM)
peD = (not na(epsD) and epsD != 0) ? priceD / epsD : na
// ——— Highest/Lowest PE over trading-day windows
peHigh = ta.highest(peD, lookbackBars)
peLow = ta.lowest(peD, lookbackBars)
idxHigh = ta.highestbars(peD, lookbackBars)
idxLow = ta.lowestbars(peD, lookbackBars)
// PE (Int) = lowest in last 252 trading days
peLow252 = ta.lowest(peD, lookbackBarsInt)
idxLow252 = ta.lowestbars(peD, lookbackBarsInt)
// Compute timestamps ONLY on the last bar to avoid history-index errors
var int timeHigh = na
var int timeLow = na
var int timeLowInt = na
if barstate.islast
if not na(idxHigh) and idxHigh >= 0
timeHigh := time[idxHigh]
if not na(idxLow) and idxLow >= 0
timeLow := time[idxLow]
if not na(idxLow252) and idxLow252 >= 0
timeLowInt := time[idxLow252]
// ——— Plot P/E line (context)
plot(showPEline ? peD : na, title="P/E (Daily)", linewidth=2, color=color.new(color.teal, 0))
// ——— Horizontal levels (extend across pane)
var line hiLine = na
var line loLine = na
var line medianLine = na
var line loIntLine = na // PE (Int)
if barstate.islast
// clear old lines
if not na(hiLine)
line.delete(hiLine)
if not na(loLine)
line.delete(loLine)
if not na(medianLine)
line.delete(medianLine)
if not na(loIntLine)
line.delete(loIntLine)
// PE High (Red solid)
if not na(peHigh)
hiLine := line.new(bar_index - 1, peHigh, bar_index, peHigh,
extend=extend.both, color=color.new(color.red, 0), width=2)
label.new(bar_index, peHigh,
"PE High: " + str.tostring(peHigh, format.mintick),
xloc=xloc.bar_index, textcolor=color.white, color=color.new(color.red, 0),
style=label.style_label_left)
// PE Low (Green solid)
if not na(peLow)
loLine := line.new(bar_index - 1, peLow, bar_index, peLow,
extend=extend.both, color=color.new(color.green, 0), width=2)
label.new(bar_index, peLow,
"PE Low: " + str.tostring(peLow, format.mintick),
xloc=xloc.bar_index, textcolor=color.white, color=color.new(color.green, 0),
style=label.style_label_left)
// PE Median (Blue dotted)
if not na(peHigh) and not na(peLow)
peMedian = (peHigh + peLow) / 2.0
medianLine := line.new(bar_index - 1, peMedian, bar_index, peMedian,
extend=extend.both, color=color.new(color.blue, 0), width=2, style=line.style_dotted)
label.new(bar_index, peMedian,
"PE Median: " + str.tostring(peMedian, format.mintick),
xloc=xloc.bar_index, textcolor=color.white, color=color.new(color.blue, 0),
style=label.style_label_left)
// PE (Int) (Green dotted), only if different from PE Low
if not na(peLow252) and (na(peLow) or peLow252 != peLow)
loIntLine := line.new(bar_index - 1, peLow252, bar_index, peLow252,
extend=extend.both, color=color.new(color.green, 0), width=2, style=line.style_dotted)
label.new(bar_index, peLow252,
"PE (Int): " + str.tostring(peLow252, format.mintick),
xloc=xloc.bar_index, textcolor=color.white, color=color.new(color.green, 0),
style=label.style_label_left)
// ——— Mark the exact high/low day (for the 756-trading-day window)
peHighValid = not na(peHigh) and not na(timeHigh)
peLowValid = not na(peLow) and not na(timeLow)
if markerStyle == "Dot"
if peHighValid
label.new(x=timeHigh, y=peHigh, xloc=xloc.bar_time, yloc=yloc.price,
text="● " + str.tostring(timeHigh, "yyyy-MM-dd"),
textcolor=color.white, color=color.new(color.red, 0),
style=label.style_label_down, size=size.tiny)
if peLowValid
label.new(x=timeLow, y=peLow, xloc=xloc.bar_time, yloc=yloc.price,
text="● " + str.tostring(timeLow, "yyyy-MM-dd"),
textcolor=color.white, color=color.new(color.green, 0),
style=label.style_label_up, size=size.tiny)
else
yMin = na(peLow) or na(peHigh) ? na : math.min(peLow, peHigh)
yMax = na(peLow) or na(peHigh) ? na : math.max(peLow, peHigh)
if peHighValid and not na(yMin) and not na(yMax)
pad = (yMax - yMin) * 0.10
line.new(x1=timeHigh, y1=yMin - pad, x2=timeHigh, y2=yMax + pad, xloc=xloc.bar_time,
extend=extend.none, color=color.new(color.red, 0), width=2)
label.new(x=timeHigh, y=yMax + pad, xloc=xloc.bar_time, yloc=yloc.price,
text="PE High\n" + str.tostring(timeHigh, "yyyy-MM-dd"),
textcolor=color.white, color=color.new(color.red, 0), style=label.style_label_up)
if peLowValid and not na(yMin) and not na(yMax)
pad = (yMax - yMin) * 0.10
line.new(x1=timeLow, y1=yMin - pad, x2=timeLow, y2=yMax + pad, xloc=xloc.bar_time,
extend=extend.none, color=color.new(color.green, 0), width=2)
label.new(x=timeLow, y=yMax + pad, xloc=xloc.bar_time, yloc=yloc.price,
text="PE Low\n" + str.tostring(timeLow, "yyyy-MM-dd"),
textcolor=color.white, color=color.new(color.green, 0), style=label.style_label_up)
// ——— Candle coloring on the price chart
dayAnchor = time("D")
isHighDay = peHighValid and dayAnchor == timeHigh
isLowDay = peLowValid and dayAnchor == timeLow
barcolor(isHighDay ? color.red : na)
barcolor(isLowDay ? color.green : na)
// ——— Heads-up if EPS is missing
if barstate.islast and na(epsD)
label.new(bar_index, na, "No Diluted EPS (TTM) data for this symbol",
style=label.style_label_left, textcolor=color.white, color=color.orange)