OPEN-SOURCE SCRIPT
P/E & Forward P/E vs History

# P/E & Forward P/E vs History
## Overview
This indicator plots a stock's **trailing P/E** and an approximate **forward P/E** in a lower pane, then frames both against the stock's *own* valuation history using a mean line and standard-deviation bands. The goal is to answer a question a raw P/E number can't: not "is 22x expensive?" but "is this stock expensive **for itself**, relative to how the market has typically valued it?"
A P/E of 22 might be cheap for one company and rich for another. By comparing the current multiple to its historical mean and expressing the gap in standard deviations, the indicator turns an ambiguous absolute number into a relative, self-referential read.
## What it plots
- **Trailing P/E** (blue) — the real, reported multiple: price divided by trailing-twelve-month earnings per share.
- **Forward P/E** (orange) — a forward-looking estimate of the multiple. This is a proxy (see Limitations).
- **Mean P/E** (gray dashed) — the average trailing P/E over the chosen window.
- **±1σ band** (shaded) — one standard deviation above and below the mean. Roughly two-thirds of historical observations fall inside this zone.
- **±2σ lines** — two standard deviations out. Roughly 95% of history falls inside ±2σ; readings beyond it are statistically unusual for the stock.
- **Info table** — a corner readout of the current trailing P/E, forward P/E, mean, the live **z-score**, and a plain-English "Position" verdict (Cheap / Normal / Rich, etc.).
## How it's calculated
**Trailing P/E.** TTM earnings per share is pulled from TradingView's fundamental data, and the multiple is current price divided by that figure. You can choose diluted or basic EPS as the source. Bars where TTM EPS is zero or negative are left blank, since a P/E built on near-zero or negative earnings is not meaningful.
**Forward P/E.** Two proxy methods are offered:
1. *Analyst estimate (×4)* — the next-quarter analyst EPS estimate, annualized by multiplying by four.
2. *Growth assumption* — trailing TTM EPS grown forward by a user-set annual rate.
The forward multiple is then current price divided by the chosen forward EPS figure.
**Historical mean and bands.** Two windowing modes are available:
1. *All history* — a cumulative mean and standard deviation computed across every available bar, so the "normal" expands as more data accrues.
2. *Rolling lookback* — a moving mean and standard deviation over a fixed number of bars (default 252, about one trading year), which adapts to recent behavior.
**Z-score.** The current trailing P/E minus the mean, divided by the standard deviation. This single number says how many standard deviations above or below average the stock is trading right now. The "Position" label translates it: beyond +1σ reads as Rich, beyond −1σ as Cheap, beyond ±2σ as Very rich / Very cheap, and inside ±1σ as Normal.
## Settings
- **Trailing EPS source** — diluted (default) or basic. If one errors on a given ticker, switch to the other.
- **Show Forward P/E** — toggle the orange line.
- **Forward EPS method** — analyst-estimate (×4) or growth-assumption.
- **Assumed annual EPS growth %** — used only by the growth-assumption method.
- **Average window** — all-history or rolling lookback.
- **Rolling lookback (bars)** — window length when using rolling mode.
- **Show ±1σ / ±2σ bands** — toggle the bands.
- **Table position / text size** — placement and sizing of the info table.
## How to read it
Look at where the blue line sits relative to the shaded band rather than at its absolute level. Inside the band is ordinary. Pushing above +1σ means the market is paying more per dollar of trailing earnings than it usually does; dropping below −1σ means the opposite. The ±2σ lines mark the statistical extremes of the stock's own range. The z-score and Position label in the table give you the same read as a number and a word, so you don't have to eyeball it.
The gap between the orange and blue lines is also informative: a forward multiple well below the trailing one implies the market expects earnings to grow (a larger future denominator shrinks the multiple). If the average line is slowly drifting higher over time in all-history mode, the market has structurally re-rated the stock to a richer multiple — the definition of "normal" has moved.
## Limitations
**Forward P/E is an approximation, not a true consensus number.** Pine Script does not expose a forward twelve-month consensus EPS. The two proxy methods here are deliberate simplifications: the ×4 annualization ignores seasonality, and the growth method is only as good as the rate you assume. Treat the forward line as directional, not as a terminal-grade figure.
**Fundamental history depth is limited.** Available EPS history on the platform is finite and often spans only a handful of years, so the "all history" mean and bands cover only the data that exists — not a multi-decade record.
**Stocks only.** The indicator needs per-share earnings data. Indices, ETFs, forex, and crypto will show n/a.
**Earnings distortions.** When trailing EPS collapses toward zero around a loss quarter, the ratio can spike, go negative, or blank out. A dramatic move in the P/E line is sometimes an earnings artifact rather than a valuation event — check the earnings backdrop before reading too much into it.
## Disclaimer
This script is for research and educational purposes and is not financial advice. Fundamental data is provided as-is by the platform's data vendors and may contain gaps or errors. Always verify figures independently before making any decision.
## Overview
This indicator plots a stock's **trailing P/E** and an approximate **forward P/E** in a lower pane, then frames both against the stock's *own* valuation history using a mean line and standard-deviation bands. The goal is to answer a question a raw P/E number can't: not "is 22x expensive?" but "is this stock expensive **for itself**, relative to how the market has typically valued it?"
A P/E of 22 might be cheap for one company and rich for another. By comparing the current multiple to its historical mean and expressing the gap in standard deviations, the indicator turns an ambiguous absolute number into a relative, self-referential read.
## What it plots
- **Trailing P/E** (blue) — the real, reported multiple: price divided by trailing-twelve-month earnings per share.
- **Forward P/E** (orange) — a forward-looking estimate of the multiple. This is a proxy (see Limitations).
- **Mean P/E** (gray dashed) — the average trailing P/E over the chosen window.
- **±1σ band** (shaded) — one standard deviation above and below the mean. Roughly two-thirds of historical observations fall inside this zone.
- **±2σ lines** — two standard deviations out. Roughly 95% of history falls inside ±2σ; readings beyond it are statistically unusual for the stock.
- **Info table** — a corner readout of the current trailing P/E, forward P/E, mean, the live **z-score**, and a plain-English "Position" verdict (Cheap / Normal / Rich, etc.).
## How it's calculated
**Trailing P/E.** TTM earnings per share is pulled from TradingView's fundamental data, and the multiple is current price divided by that figure. You can choose diluted or basic EPS as the source. Bars where TTM EPS is zero or negative are left blank, since a P/E built on near-zero or negative earnings is not meaningful.
**Forward P/E.** Two proxy methods are offered:
1. *Analyst estimate (×4)* — the next-quarter analyst EPS estimate, annualized by multiplying by four.
2. *Growth assumption* — trailing TTM EPS grown forward by a user-set annual rate.
The forward multiple is then current price divided by the chosen forward EPS figure.
**Historical mean and bands.** Two windowing modes are available:
1. *All history* — a cumulative mean and standard deviation computed across every available bar, so the "normal" expands as more data accrues.
2. *Rolling lookback* — a moving mean and standard deviation over a fixed number of bars (default 252, about one trading year), which adapts to recent behavior.
**Z-score.** The current trailing P/E minus the mean, divided by the standard deviation. This single number says how many standard deviations above or below average the stock is trading right now. The "Position" label translates it: beyond +1σ reads as Rich, beyond −1σ as Cheap, beyond ±2σ as Very rich / Very cheap, and inside ±1σ as Normal.
## Settings
- **Trailing EPS source** — diluted (default) or basic. If one errors on a given ticker, switch to the other.
- **Show Forward P/E** — toggle the orange line.
- **Forward EPS method** — analyst-estimate (×4) or growth-assumption.
- **Assumed annual EPS growth %** — used only by the growth-assumption method.
- **Average window** — all-history or rolling lookback.
- **Rolling lookback (bars)** — window length when using rolling mode.
- **Show ±1σ / ±2σ bands** — toggle the bands.
- **Table position / text size** — placement and sizing of the info table.
## How to read it
Look at where the blue line sits relative to the shaded band rather than at its absolute level. Inside the band is ordinary. Pushing above +1σ means the market is paying more per dollar of trailing earnings than it usually does; dropping below −1σ means the opposite. The ±2σ lines mark the statistical extremes of the stock's own range. The z-score and Position label in the table give you the same read as a number and a word, so you don't have to eyeball it.
The gap between the orange and blue lines is also informative: a forward multiple well below the trailing one implies the market expects earnings to grow (a larger future denominator shrinks the multiple). If the average line is slowly drifting higher over time in all-history mode, the market has structurally re-rated the stock to a richer multiple — the definition of "normal" has moved.
## Limitations
**Forward P/E is an approximation, not a true consensus number.** Pine Script does not expose a forward twelve-month consensus EPS. The two proxy methods here are deliberate simplifications: the ×4 annualization ignores seasonality, and the growth method is only as good as the rate you assume. Treat the forward line as directional, not as a terminal-grade figure.
**Fundamental history depth is limited.** Available EPS history on the platform is finite and often spans only a handful of years, so the "all history" mean and bands cover only the data that exists — not a multi-decade record.
**Stocks only.** The indicator needs per-share earnings data. Indices, ETFs, forex, and crypto will show n/a.
**Earnings distortions.** When trailing EPS collapses toward zero around a loss quarter, the ratio can spike, go negative, or blank out. A dramatic move in the P/E line is sometimes an earnings artifact rather than a valuation event — check the earnings backdrop before reading too much into it.
## Disclaimer
This script is for research and educational purposes and is not financial advice. Fundamental data is provided as-is by the platform's data vendors and may contain gaps or errors. Always verify figures independently before making any decision.
Skrip open-source
Dengan semangat TradingView yang sesungguhnya, pembuat skrip ini telah menjadikannya sebagai sumber terbuka, sehingga para trader dapat meninjau dan memverifikasi fungsinya. Salut untuk penulisnya! Meskipun Anda dapat menggunakannya secara gratis, perlu diingat bahwa penerbitan ulang kode ini tunduk pada Tata Tertib kami.
Pernyataan Penyangkalan
Informasi dan publikasi ini tidak dimaksudkan, dan bukan merupakan, saran atau rekomendasi keuangan, investasi, trading, atau jenis lainnya yang diberikan atau didukung oleh TradingView. Baca selengkapnya di Ketentuan Penggunaan.
Skrip open-source
Dengan semangat TradingView yang sesungguhnya, pembuat skrip ini telah menjadikannya sebagai sumber terbuka, sehingga para trader dapat meninjau dan memverifikasi fungsinya. Salut untuk penulisnya! Meskipun Anda dapat menggunakannya secara gratis, perlu diingat bahwa penerbitan ulang kode ini tunduk pada Tata Tertib kami.
Pernyataan Penyangkalan
Informasi dan publikasi ini tidak dimaksudkan, dan bukan merupakan, saran atau rekomendasi keuangan, investasi, trading, atau jenis lainnya yang diberikan atau didukung oleh TradingView. Baca selengkapnya di Ketentuan Penggunaan.