# Obtaining Historical Financial Data

I'm trying to get the historical P/E ratio for a number of securities.

WolframAlpha makes this seem easy:

However, when I try to obtain a subset (or even just a single value) of this information in Mathematica I run into problems.

What's going on? I think I'm using the function properly. In the documentation center it specifics the following syntax:

FinancialData["name","prop",{start,end,…}]


What's going wrong?

UPDATE: I have been able to obtain some of the information using Mathematica, but not in the form of a function.

• You are right that FInancialData doesn't seem to have that information. I guess Wolfram Alpha is gathering data from a different source. Jul 7, 2016 at 20:25
• @MarcoB Is there a way to quickly use Wolfram Alpha hundreds of times? Jul 7, 2016 at 20:29
• I suspect not, since it relies on a network connection with its innate lag, but I am not really an expert in using Wolfram Alpha at all, so perhaps others will chime in. Is the data available through any other database? It may be easier to harvest it elsewhere. Jul 7, 2016 at 20:31
• To those who may chime in, I'm okay with significant lag, I only need to do this once (with a few thousand iterations). Jul 7, 2016 at 20:32
• If you can find a machine with both a Mathematica license and a Bloomberg license, you will appreciate the WSTP interface at github.com/MichaelSternNYC/bloomberg-to-mathematica. It moves any data accessible via the Bloomberg API into Mathematica, which would include P/E ratios from several sources going back many, many years. Jul 7, 2016 at 22:09

So the data you want is here:

WolframAlpha["p/e msft 1/3/2000 to 6/1/2000"]


How can it be accessed? Assuming you want the data from the plot, you need to see what the name of the pod you are interested is,

WolframAlpha["p/e msft 1/3/2000 to 6/1/2000", "PodIDs"]
(* {"Input", "Result", "DateRangeSpecified:PriceEarningsRatio:FinancialData"} *)


That last one is what we are looking for, so we look a little deeper

WolframAlpha["p/e msft 1/3/2000 to 6/1/2000", {"DateRangeSpecified:PriceEarningsRatio:FinancialData"}]


That one labeled "TimeSeriesData" looks intersting,

WolframAlpha["p/e msft 1/3/2000 to 6/1/2000",
{"DateRangeSpecified:PriceEarningsRatio:FinancialData",
"TimeSeriesData"}]


Bingo! That's what you need,

data = {{"DateRangeSpecified:PriceEarningsRatio:FinancialData", 1},
"TimeSeriesData"} /.
WolframAlpha[
"p/e msft 1/3/2000 to 6/1/2000", \
{"DateRangeSpecified:PriceEarningsRatio:FinancialData",
"TimeSeriesData"}];

DateListPlot[data]


This method is not foolproof - it seems that you can't enter a time period over a certain length and still get a "TimeSeries" result, so toy around with it. For example, the string "p/e msft 1/3/1990 to 6/1/1991" gives a result but "p/e msft 1/3/1990 to 6/1/1992" does not. Also, try adding TimeConstrained -> 120 as an option to WolframAlpha to avoid timeouts.

• Thank you @JasonB VERY much. How could I modify for, say the market capitalization (either for one specific date or a date range)? How did you know that "DateRangeSpecified:PriceEarningsRatio:FinancialData" was a valid? Jul 8, 2016 at 20:02
• I tried to show the process that lead to knowing the name of the pod. Look at the result of WolframAlpha["market capitalization msft 1/3/2000 to 6/1/2000", "PodIDs"]` and it should be clear what the correct string is Jul 8, 2016 at 20:08
• a lesson in reading the whole answer twice has been learned. Thank you for providing such a through step by step guide. Jul 8, 2016 at 20:12