# How to Plot the P/E Ratio in ThinkorSwim With 10 Years of History

> Free P/E ratio indicator for ThinkorSwim: close over 252 days of reported EPS, with lowest, highest and 200-bar median labels. Video: Starbucks at 21.67.

Page: https://tosindicators.com/indicators/pe-ratio  
Video: https://www.youtube.com/watch?v=gbJo6jMawSg  
Section: Indicators  
Skill: Beginner  
Access: free  
Updated: 2026-10-17

In this tutorial I plot the P/E ratio in ThinkorSwim with a few lines of thinkScript. The indicator divides the closing price by the earnings reported over the past 252 trading days, draws that trailing 12-month P/E as a line under the chart, and adds labels for the current, lowest, highest and median P/E.

Sites like Yahoo Finance and the Wall Street Journal show you today's P/E. In ThinkorSwim you also get the history behind it, 10 years or more, and you can line it up with the chart setups you already trade. It's for investors and swing traders who value companies with steady earnings.

## Key Takeaways

- The P/E Ratio indicator divides the close by trailing 12-month earnings: the sum of `GetActualEarnings()` over the last 252 daily bars. It plots as a line in a lower pane.

- The indicator's 4 labels show the current P/E (white), the lowest and highest P/E on the chart (yellow), and the median P/E of the last 200 bars, light green when the current P/E is at or below it and light red when above.

- In the video, Starbucks read a P/E of 21.6657 in ThinkorSwim, against 19.19 on Yahoo Finance and 19.26 on the Wall Street Journal.

- On a 10-year daily chart, Starbucks' lowest P/E was 10.7916, its highest 120.755 in April 2021 and its median 21.97. In March 2020 it fell to 19.70.

- The P/E ratio is close to useless when earnings are negative. It works best on stocks with steady earnings.

## What Is the P/E Ratio?

The P/E ratio, or price-to-earnings ratio, is a stock's price divided by its earnings per share. A P/E of 20 means investors pay $20 for each $1 of yearly earnings. The trailing P/E uses the last 12 months of reported earnings, and the forward P/E uses estimates for the next 12 months. This indicator is a trailing P/E.

A P/E that's low for the stock's own history means the market is pricing its earnings more cheaply than usual. A high one means investors are paying up. Neither tells you why, so the P/E is a starting point for research.

The P/E ratio doesn't fit every stock. If earnings are negative, the number is close to useless. It's most useful where earnings are steady and the P/E has traditionally been a good way to value the company. For a valuation based on assets instead, see our [price to book ratio indicator](https://tosindicators.com/indicators/pb-ratio), and for income stocks, our [dividend yield indicator](https://tosindicators.com/indicators/dividend-yield).

## Why Plot the P/E Ratio in ThinkorSwim

Plotting the P/E ratio in ThinkorSwim gives you 2 things a quote page doesn't:

- **Context.** Yahoo Finance or the Wall Street Journal show the P/E right now. On a chart you see how it has moved over 10 years or more, so you can tell whether today's reading is high or low for that company.

- **Interaction.** You can run calculations on the P/E, like its lowest, highest and median values, and line it up with technical setups on the same chart.

In the video I use Starbucks as the example. Its chart had a technical setup, and its P/E was near the level of its March 2020 low. Seeing both in one place is the reason to build this.

![Visa 10-year daily chart with the P/E Ratio study in a lower pane, showing the P/E line and its four labels](https://tosindicators.com/wp-content/uploads/2022/05/PE-Ratio-Indicator-for-ThinkOrSwim.jpg)The P/E Ratio study in the lower pane of a 10-year daily Visa (V) chart. The 4 labels in the top left show the current, lowest, highest and median P/E.

## How the P/E Ratio Code Works

The P/E ratio formula has the price on top and trailing 12-month earnings on the bottom. ThinkorSwim already has the price: it's `close`. Earnings take 2 steps.

### Step 1: Earnings on Earnings Days

```
def earningsValue = if IsNan(GetActualEarnings()) then 0 else GetActualEarnings();
```

`GetActualEarnings()` returns the reported earnings per share on an earnings day and NaN (not a number) on every other bar. The `if IsNan(...) then 0` part turns those empty bars into 0, so the sum in the next step works.

To check it, I plotted `earningsValue` in chart bubbles. They matched the earnings figures along the bottom of the Starbucks chart: 0.32, -0.46 and 0.51. The download keeps a commented-out bubble line at the bottom that prints the P/E in a bubble on every bar: delete the `#` to turn it on.

### Step 2: Trailing 12-Month Earnings

```
def earningsTTM = Sum(earningsValue, 252);
```

`Sum` adds up the last 252 bars, one trading year on a daily chart, which normally covers the last 4 quarterly reports. In the video, Starbucks' trailing 12-month earnings came to 3.32.

### Step 3: The P/E Line

```
declare lower;
plot pe  = close / earningsTTM;
```

`declare lower` puts the study in its own pane under the price chart. I first wrote `pe` as a `def` and looked at it in chart bubbles. Changing it to `plot` draws a line, so you see how the P/E moved year by year.

### Step 4: Lowest, Highest and Median

```
def LowestPE = LowestAll(pe);
def HighestPE = HighestAll(pe);
def MedianPE = Median(pe,200);
```

`LowestAll` and `HighestAll` read every bar loaded on the chart, so on a 10-year chart they give the cheapest and most expensive the stock has been in 10 years.

`Median(pe,200)` is the middle value of the last 200 bars, about 9 to 10 months of daily bars, so the median label covers the same window on a 10-year or a 20-year chart. For a longer median, raise the 200 (an edit that isn't in the video).

### Step 5: The Labels

```
AddLabel(yes, "P/E Ratio: " + pe, color.white);
AddLabel(yes, "Lowest P/E: " + LowestPE, color.yellow);
AddLabel(yes, "Highest P/E: " + HighestPE, color.yellow);
```

The first 3 labels print the current P/E in white and the lowest and highest P/E in yellow.

```
AddLabel(yes, "Median P/E: " + MedianPE, if pe

## thinkScript code

```
#TOS Indicators
#Home of the Volatility Box
#Indicator: P/E Ratio
#Code written in 2022
#Full Tutorial: https://www.tosindicators.com/indicators/pe-ratio

declare lower;

def earningsValue = if IsNan(GetActualEarnings()) then 0 else GetActualEarnings();
def earningsTTM = Sum(earningsValue, 252);
plot pe  = close / earningsTTM;

def LowestPE = LowestAll(pe);
def HighestPE = HighestAll(pe);
def MedianPE = Median(pe,200);

AddLabel(yes, "P/E Ratio: " + pe, color.white);
AddLabel(yes, "Lowest P/E: " + LowestPE, color.yellow);
AddLabel(yes, "Highest P/E: " + HighestPE, color.yellow);
AddLabel(yes, "Median P/E: " + MedianPE, if pe <= MedianPE then color.light_green else color.light_red);

#AddChartBubble(pe, close, pe, color.yellow);
```

## Questions

### How do I plot the P/E ratio in ThinkorSwim?

Open the Charts tab, click Studies, choose Create and paste in the code from this page. It sums GetActualEarnings() over 252 daily bars for trailing 12-month earnings, divides the close by that sum, and plots the P/E in a lower pane with labels for the current, lowest, highest and median P/E.

### How is the P/E ratio calculated?

Divide the stock price by earnings per share. The trailing P/E uses the last 12 months of reported earnings. In the video, Starbucks' trailing 12-month earnings came to 3.32, and dividing the close by that gave a P/E of 21.6657 in ThinkorSwim.

### Why is the P/E in ThinkorSwim different from Yahoo Finance?

The sources don't always agree. In the video, Starbucks read 21.6657 in ThinkorSwim, 19.19 on Yahoo Finance and 19.26 on the Wall Street Journal. The indicator's value is the history: it measures today's P/E and past P/E the same way, so comparing a stock with itself stays consistent.

### What does the median P/E label mean?

It shows the median P/E of the last 200 bars. The label turns light green when the current P/E is at or below the median and light red when it's above. In the video, Starbucks had a median of 21.97 and a current P/E of 21.6657, so it was trading a little under its median.

### How far back does the historical P/E go?

As far back as the chart's time frame. On a 10-year daily chart, the lowest and highest labels cover 10 years. When I switched Starbucks to 20 years, the lowest P/E reached further back, but the Highest P/E label stopped plotting because the data didn't go back before 2008. The median label always uses the last 200 bars.

### Can I use the P/E ratio on every stock?

No. If a company's earnings are negative, the P/E ratio is close to useless. It works best on stocks with steady earnings, where the P/E has traditionally been a good valuation measure. For other angles, look at book value with our price to book ratio indicator, or at dividends.

### What is a good P/E ratio?

There's no single number. A P/E means most against the stock's own history. In the video, Costco's P/E of 34 was below its median of 39.95 but well above its lowest of 16, while Starbucks at 21.67 was close to its March 2020 low of 19.70.

### What does GetActualEarnings() return in thinkScript?

It returns the reported earnings per share on the bar where the company reported, and NaN on every other bar. The P/E indicator turns those NaN values into 0, so Sum() can add up the last 252 bars into trailing 12-month earnings.

## Related

- [Price to Book Ratio Indicator and Scan for ThinkorSwim](https://tosindicators.com/indicators/pb-ratio)
- [How to Build a Fair Value Gap Indicator in ThinkorSwim](https://tosindicators.com/indicators/fair-value-gap)
- [How to Build a Heiken Ashi Trend Dots Indicator in ThinkorSwim](https://tosindicators.com/indicators/heiken-ashi)
- [How to Build a Commodities Tracker in ThinkorSwim](https://tosindicators.com/indicators/commodities-tracker)
- [Trend Squeeze Indicator for ThinkorSwim](https://tosindicators.com/indicators/trend-squeeze)
- [Volume Intensity Indicator for ThinkorSwim](https://tosindicators.com/indicators/volume-intensity)

