Skip to main content

P/E Ratio Indicator for ThinkOrSwim With 10 Years of History (Free Download)

IndicatorFreeBeginner13 min video23 lines of codeMay 20, 2022

A free P/E ratio indicator for ThinkOrSwim. It plots the trailing 12-month P/E as a line, with labels for the current, lowest, highest and median P/E.

P/E Ratio on a SBUX chart in ThinkOrSwim
On this page

Overview

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, and for income stocks, our dividend yield indicator.

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
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

This step’s code is free with an account. Create one or sign in.

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

This step’s code is free with an account. Create one or sign in.

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

This step’s code is free with an account. Create one or sign in.

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

This step’s code is free with an account. Create one or sign in.

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

This step’s code is free with an account. Create one or sign in.

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

This step’s code is free with an account. Create one or sign in.

The median label is the one I read first. It turns light green when the current P/E is at or below the median and light red when it's above, a first read on whether the stock is in cheaper territory than usual. If you're new to labels, our AddLabel tutorial covers the syntax.

LabelWhat it showsColor
P/E RatioClose divided by trailing 12-month earningsWhite
Lowest P/ELowest P/E on the chartYellow
Highest P/EHighest P/E on the chartYellow
Median P/EMedian P/E of the last 200 barsLight green at or below the median, light red above

How to Add the P/E Ratio Indicator to ThinkOrSwim

The P/E ratio indicator is one study, and these are the steps I follow in the video:

  1. Copy the code from the Code tab on this page, or download the .ts file.
  2. In ThinkOrSwim, open the Charts tab, click the Studies icon and choose Create.
  3. Delete the default code, paste in the P/E code, name the study and click OK.
  4. Add it to a daily chart. It opens in a lower pane because of declare lower.
  5. Set the chart's time frame to 10 years of daily bars, the setting I use in the video.

The download is the same code as the Code tab. To import the .ts file instead of pasting, follow our guide on how to import a study into ThinkOrSwim.

Starbucks and Costco in the Video

Starbucks is the main P/E ratio example in the video, with Costco as a second check. These are the values from the day I recorded, on 10-year daily charts:

StockCurrent P/ELowest P/EHighest P/EMedian P/E
Starbucks21.665710.7916120.755 (April 2021)21.97
Costco34164939.95

Starbucks had hit its 127.2% Fibonacci extension and pulled back to its 50% retracement. At the same time, its P/E of 21.67 was getting close to the 19.70 it fell to in March 2020, and it sat a little below its median. That pairing of a technical pullback with a P/E near past lows is how I look for undervalued spots. Our Fibonacci Trading Course covers those levels.

Costco was below its median of 39.95 too, at a P/E of 34. Against its lowest P/E of 16, though, it still had room to fall. The labels give you a baseline for what counts as cheap for each stock.

Why ThinkOrSwim's P/E Differs From Yahoo Finance

The P/E ratio in ThinkOrSwim won't always match other sites. In the video, Starbucks read 21.6657 in ThinkOrSwim, 19.19 on Yahoo Finance and 19.26 on the Wall Street Journal: close, with a slight gap.

The benefit of the ThinkOrSwim number is consistency. The indicator measures today's P/E and every past P/E on the chart the same way, so comparing the stock with its own history is apples to apples.

Changing the Chart Length

The lowest and highest P/E labels change with the chart's time frame, because LowestAll and HighestAll read every loaded bar. The current and median labels don't. When I switched Starbucks from 10 years to 20 years, the lowest P/E reached further back, and the Highest P/E label stopped plotting, since the data didn't go back before 2008. I went back to 10 years for the rest of the video.

The 252 in the code means 252 bars, one trading year on a daily chart, so keep the study on a daily chart.

Reading the Indicator on Other Stocks

The P/E ratio indicator works the same way on any stock that reports earnings. The screenshots below show it on Walmart and Target on May 20, 2022, each on a 10-year daily chart. In both, the price and the P/E line drop sharply at the right edge, and the median label is green, so the current P/E sits at or below its 200-bar median.

Walmart 10-year daily chart with the P/E Ratio study in the lower pane and a green Median P/E label
Walmart (WMT) on a 10-year daily chart on May 20, 2022. The price and the P/E line fall sharply at the right edge, and the green Median P/E label means the current P/E is at or below its 200-bar median.
Target 10-year daily chart with the P/E Ratio study in the lower pane and a green Median P/E label
Target (TGT) on a 10-year daily chart on May 20, 2022, with the same sharp drop in price and in the P/E line at the right edge, and a green Median P/E label.

A green median label is the first read. Next, compare the current P/E with the lowest and highest labels, and check the price chart for a setup.

How I Use the P/E Ratio

The P/E ratio indicator gives me a baseline for value, and the chart decides the timing. The setup I want is a P/E near the low end of the stock's own range, with the median label green, plus a technical setup like Starbucks' pullback to its 50% retracement.

A low P/E can get lower, so treat a green label as a read on value, not an entry. Decide where your stop goes before you buy, and size the position so a stop-out costs a small, fixed share of your account.

If you like working with data, you can take it further with range calculations on the same P/E series to define what you consider undervalued. For other ways to value a company, compare the P/E with our Dividend Discount Model for dividend payers, or with the price to book ratio for a view based on book value.

Common Problems and Fixes

Most P/E ratio indicator problems come from the data behind it:

  • Negative or strange readings. If a company lost money over the last 12 months, the P/E has no useful meaning. The fix is to value those stocks another way.
  • A different number from Yahoo Finance. Expected. Compare the stock with its own history in ThinkOrSwim instead of matching another site to the decimal.
  • No Highest P/E label on a 20-year chart. In the video this happened on Starbucks, with no data before 2008. Go back to a 10-year chart.
  • Labels you'd like in other colors. Edit the color in each AddLabel line. I started the median label as color.cyan in the video before switching to the green and red version.

Full thinkScript code

P/E Ratio.tsthinkScript · 23 lines
The full code is free with an account

Create a free account or sign in to copy all 23 lines and download the file.

Download

P/E Ratio Indicator

Download the P/E Ratio Indicator for ThinkOrSwim. The download is a .zip with one file, TI_PE_RatioSTUDY.ts: the P/E line and its 4 labels, the same code as the Code tab on this page. Import it as a study, or create a new study and paste in the code. If you haven't imported a study before, here's how to import into your ThinkOrSwim platform.

Download free

Video: P/E Ratio, built line by line

13 minutes on YouTube.

Watch on YouTube

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.

Get each new indicator by email

One email on Sundays with the week's new tool and its code. A free account also unlocks every .txt download.

No credit card. Unsubscribe anytime.