Indicator · Pine Script v6 · Own pane

PE Ratio and TTM EPS

by ankurksoni· code credited to ankursoni

A separate-pane indicator, short title PE/EPS, that styles a price to earnings view after a screener chart.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code78
Settings8inputs
LicenceNone statedfile: MPL-2.0
Last update2 days ago27 Oct 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

78 lines of Pine Script v6 from trading view/peRatioTTMEPS.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

peRatioTTMEPS.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ankursoni

// PE Ratio & TTM EPS — Screener.in style chart
// - PE ratio (TTM) plotted as a line, meant to be read off the RIGHT price scale
// - TTM EPS plotted as full-quarter blocks (columns), meant to be read off the LEFT price scale
// - A flat dashed line marks the median PE over all pulled history
// - Each reported quarter is shown as a shaded block spanning the whole quarter,
//   alternating shaded/unshaded so consecutive quarters are visually distinguishable
//
// NOTE: Pine Script cannot assign a plot to the left/right scale from code.
// After adding this indicator, right-click the "TTM EPS" plot in the pane's
// data window / legend -> "Scale" -> "Left" so it renders on the left axis,
// exactly like the reference screener.in chart.

//@version=6
indicator('PE Ratio & TTM EPS (Screener Style)', shorttitle = 'PE/EPS', overlay = false, format = format.price, precision = 2, max_labels_count = 100)

string GRP1 = "════════ Data ═════════"
epsSource = input.string('Diluted', 'TTM EPS Source', options = ['Diluted', 'Basic'], group = GRP1, tooltip = 'Which EPS figure to use for the TTM EPS blocks')
usePriceFallback = input.bool(true, 'Fallback to Close/EPS if PE field unavailable', group = GRP1, tooltip = 'Some symbols do not publish a PE_RATIO field. If enabled, PE is computed as Close / TTM EPS when the field is missing')

string GRP2 = "════════ Style ═════════"
peColor = input.color(#5b6fed, 'PE Line Color', group = GRP2)
peWidth = input.int(2, 'PE Line Width', minval = 1, maxval = 4, group = GRP2)
epsBlockColor = input.color(#7ec8e3, 'TTM EPS Block Color', group = GRP2)
epsBlockTransp = input.int(75, 'TTM EPS Block Transparency', minval = 0, maxval = 95, group = GRP2)
medianColor = input.color(color.gray, 'Median PE Line Color', group = GRP2)
showShadeAlternating = input.bool(true, 'Alternate Shading Per Quarter', group = GRP2, tooltip = 'When on, only every other reported quarter is shaded, matching the screener.in look. Turn off to shade every quarter')

// ───────────────────────── Financial data ─────────────────────────

epsFinId = epsSource == 'Diluted' ? 'EARNINGS_PER_SHARE_DILUTED' : 'EARNINGS_PER_SHARE_BASIC'

epsTTM = request.financial(syminfo.tickerid, epsFinId, 'TTM', barmerge.gaps_off, ignore_invalid_symbol = true)
peField = request.financial(syminfo.tickerid, 'PRICE_EARNINGS_RATIO', 'TTM', barmerge.gaps_off, ignore_invalid_symbol = true)

// Fallback PE uses the weekly close, independent of the chart's own timeframe
weeklyClose = request.security(syminfo.tickerid, 'W', close, lookahead = barmerge.lookahead_off)

peComputed = epsTTM > 0 ? weeklyClose / epsTTM : na
pe = na(peField) and usePriceFallback ? peComputed : peField

// ───────────────────────── Quarter block detection ─────────────────────────

newQuarter = not na(epsTTM) and (na(epsTTM[1]) or epsTTM != epsTTM[1])

var bool shadeOn = false
if newQuarter
    shadeOn := not shadeOn

epsDisplay = (not showShadeAlternating or shadeOn) and not na(epsTTM) ? epsTTM : na

// ───────────────────────── Median PE (all pulled history) ─────────────────────────

var peHistory = array.new_float(0)
if not na(pe)
    array.push(peHistory, pe)
    if array.size(peHistory) > 100000
        array.shift(peHistory)

var int firstBarTime = na
if barstate.isfirst
    firstBarTime := time

var line medianLine = na
var label medianLabel = na
if barstate.islast and array.size(peHistory) > 0
    medianPE = array.median(peHistory)
    line.delete(medianLine)
    label.delete(medianLabel)
    medianLine := line.new(x1 = firstBarTime, y1 = medianPE, x2 = time, y2 = medianPE, xloc = xloc.bar_time, extend = extend.none, color = medianColor, style = line.style_dashed, width = 1)
    medianLabel := label.new(x = time, y = medianPE, xloc = xloc.bar_time, text = 'Median PE = ' + str.tostring(medianPE, '#.##'), style = label.style_label_left, color = color.new(color.gray, 100), textcolor = medianColor, size = size.small)

// ───────────────────────── Plots ─────────────────────────

plot(epsDisplay, title = 'TTM EPS (Quarter Blocks)', style = plot.style_columns, color = color.new(epsBlockColor, epsBlockTransp))
plot(pe, title = 'PE', color = peColor, linewidth = peWidth)

Overview

PE Ratio and TTM EPS plots the trailing price to earnings ratio as a line and trailing twelve-month earnings per share, diluted or basic, as quarter-wide blocks, with a dashed median PE line over the loaded history. If the symbol has no published PE field, an option computes it from the weekly close divided by trailing earnings. Alternate quarters can be shaded. The EPS plot needs to be moved to the left scale by hand. It depends on financial data being available for the symbol.

Details

Published by

ankurksoni on GitHub

Code credit

ankursoni, as written in the script's header

Draws

Separate pane below the chart

Markets

Stocks

Timeframes

H4 and above

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
TTM EPS SourcestringDilutedepsSource
Fallback to Close/EPS if PE field unavailablebooltrueusePriceFallback
PE Line Colorcolor#5b6fedpeColor
PE Line Widthint2peWidth
TTM EPS Block Colorcolor#7ec8e3epsBlockColor
TTM EPS Block Transparencyint75epsBlockTransp
Median PE Line Colorcolorcolor.graymedianColor
Alternate Shading Per QuarterbooltrueshowShadeAlternating
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Asymmetrical RSI

by everget
947 stars

Asymmetrical RSI is an indicator that computes a custom relative strength index from the rate of change of a user-selected source. It plots the resulting ARSI line in a separate pane with color coding that turns green above the overbought level and red below t

Indicator v4 Oscillators & momentum
Updated 11 days ago · GPL-3.0 GitHub repository of Asymmetrical RSI

This indicator calculates momentum setups based on the Relative Strength Index (RSI) and plots in a separate pane. It offers multiple scanning modes such as Continuous Break, Continuous Retracement, Flip Break, Flip Retracement, and both regular and hidden div

Indicator v6 Oscillators & momentumDivergence Repaints
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - RSI Directional Momentum Indicator
550 stars

Range Volume Change compares volume inside a chosen session window with the same window on the previous day and plots that change as a percentage. It accumulates volume during the window and, when enabled, volume outside it, drawing growing columns in a separa

Indicator v5 VolumeSessions & time Repaints
Updated 10 months ago · None stated GitHub repository of Range Volume Change

Beep Boop

by magic8bot
409 stars

This indicator calculates a modified MACD oscillator and signal line alongside a trend EMA filter, plotting the output as a stylized column histogram in a separate pane. Instead of plotting true MACD distance, it quantifies positive and negative histogram mome

Indicator v4 Oscillators & momentumTrend
Updated 3 years ago · MIT GitHub repository of Beep Boop

Mojito v3 Strategy

by namuan
318 stars

The Mojito v3 Strategy indicator evaluates volatility term structure by calculating the ratio between front-month and second-month VIX futures contracts. It smooths this ratio using a configurable median filter and compares the result against user-defined uppe

Indicator v5 Volatility & bands
Updated 2 months ago · MIT GitHub repository of Mojito v3 Strategy

More indicators

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers