3
$\begingroup$

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

WolframAlpha makes this seem easy: WolframAlphaInput

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

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

$\endgroup$
  • $\begingroup$ You are right that FInancialData doesn't seem to have that information. I guess Wolfram Alpha is gathering data from a different source. $\endgroup$ – MarcoB Jul 7 '16 at 20:25
  • $\begingroup$ @MarcoB Is there a way to quickly use Wolfram Alpha hundreds of times? $\endgroup$ – wizlog Jul 7 '16 at 20:29
  • $\begingroup$ 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. $\endgroup$ – MarcoB Jul 7 '16 at 20:31
  • $\begingroup$ To those who may chime in, I'm okay with significant lag, I only need to do this once (with a few thousand iterations). $\endgroup$ – wizlog Jul 7 '16 at 20:32
  • 1
    $\begingroup$ 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. $\endgroup$ – Michael Stern Jul 7 '16 at 22:09
3
$\begingroup$

So the data you want is here:

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

Mathematica graphics

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"}]

Mathematica graphics

That one labeled "TimeSeriesData" looks intersting,

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

enter image description here

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]

Mathematica graphics

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.

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – wizlog Jul 8 '16 at 20:02
  • $\begingroup$ 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 $\endgroup$ – Jason B. Jul 8 '16 at 20:08
  • $\begingroup$ a lesson in reading the whole answer twice has been learned. Thank you for providing such a through step by step guide. $\endgroup$ – wizlog Jul 8 '16 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.