Message Boards Message Boards

0
|
9525 Views
|
7 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Get historical PE-ratios with FinancialData?

Posted 5 years ago

I have tried to get historical PE-ratios with FinancialData like this:

FinancialData["MSFT","PERatio",{2000,1,1}

But with this I only get one value, i.e. the latest PE-ratio. I had expected a whole list of them. What do I do wrong?

POSTED BY: Laurens Wachters
7 Replies
Posted 5 years ago

Hi Laurens,

Thanks, but my code is not really beautiful. What is beautiful and inspiring is that you are 84 years old and continuing to study and learn.

I will attempt to explain the last two lines of the code.

timeSeries = Entity["Financial", "NASDAQ:" <> ToUpperCase[symbol]][EntityProperty["Financial", property, dateRange]]

If symbol = "msft", the first part is

Entity["Financial", "NASDAQ:MSFT"]

Which is an Entity of type Financial and name NASDAQ:MSFT. Entity documentation. For a list of all the available entity types evaluate

EntityValue[]

From an Entity you can retrieve various properties. In this case we are interested in Financial properties. For a list of available Financial property names evaluate

EntityProperties["Financial"] // CanonicalName

The second part of that line is

[EntityProperty["Financial", property, dateRange]]

The property is passed in as an argument to the financialData function and dateRange is constructed earlier in the function. It retrieves the specified property for the specified date range.

If we evaluate

property = "RawClose";
dateRange = {"Date" -> Interval[{DateObject[{2018, 11, 1}], DateObject[{2018, 11, 2}]}]};
timeSeries = Entity["Financial", "NASDAQ:MSFT"][EntityProperty["Financial", property, dateRange]]

We get a TimeSeries enter image description here

The last line is

{DateValue[First[#], {"Year", "Month", "Day"}], QuantityMagnitude[Last[#]]} & /@ timeSeries["DatePath"]]

To get a list of date / value pairs we use the DatePath property of the TimeSeries. See documentation.

timeSeries["DatePath"]

Which evaluates to enter image description here Notice that the dates are DateObject and not of the desired form of {YYYY, MM, DD} and that the price is in units of USDollars

timeSeries["DatePath"] // First // First // FullForm
(* DateObject[List[2018,11,1,0,0,0.`],"Instant","Gregorian",-6.`] *)
timeSeries["DatePath"] // First // Last // FullForm
(* Quantity[105.92`,"USDollars"] *)

So we write a pure function to convert to the needed form and Map it using /@ over the list of date / value pairs.

{DateValue[First[#], {"Year", "Month", "Day"}], QuantityMagnitude[Last[#]]} &

It converts DateObject to {YYYY, MM, DD} and gets the magnitude of the price without the USDollars.

{DateValue[First[#], {"Year", "Month", "Day"}], QuantityMagnitude[Last[#]]} & /@ timeSeries["DatePath"]
(* {{{2018, 11, 1}, 105.92}, {{2018, 11, 2}, 106.16}} *)

I think Stephen Wolfram's book that you referenced covers most of the above.

See Entities, Map and /@, pure functions, more pure functions, units.

You should also take a look at this.

Hope that helps!.

POSTED BY: Rohit Namjoshi

Thank you Rohit for your beautiful code. It works nicely. By replacing the last date by Drop[DateList[Yesterday], -3] I can also get the required lists up till yesterday. like one usually gets with e.g. FinancialData{"MSFT","Close",{2000,1,1} }.

The only thing I am left with is really understanding your code. Especially the last two lines are staying an enigma for me. I have concluded that I especially need to learn more about Entity and TimeSeries. I have tried the Help of Mathematica, but to no avail. Also the book An elementary Introduction to Wolfram Language by Sephen Wolfram did not get me much further. Do you happen to know a textbook from which I can learn more about the type of more advanced code you sent me? Although I am 84 years old, I stlll love to learn new skills.

Best regards, Laurens

POSTED BY: Laurens Wachters
Posted 5 years ago

Hi Laurens,

I was not very clear in my explanation. Attached is a notebook with a financialData function that mimics the behavior of FinancialData but uses the Entity framework. I noticed that sometimes the method fails to complete on the first or second attempt and succeeds after that. Not sure why, perhaps some underlying timeout.

Rohit

Attachments:
POSTED BY: Rohit Namjoshi

Hi Rohit,

Thanks for your help. But how do I convert the result of a free form call for the P/E ratio in a form that one normally gets with FinancialData? In the accompanying notebook I ask in free form the P/E ratio and then, as you recommend, apply InputForm. Is this something that I can use for further calculations? I would prefer to get a list like I am used to get with for instance FinancialData["msft","Close", {{2018, 9, 1}, {2018, 11, 1}}], as shown in the accompanying notebook

Attachments:
POSTED BY: Laurens Wachters
Posted 5 years ago

Hi Laurens,

I can reproduce your results on "11.3.0 for Mac OS X x86 (64-bit) (March 7, 2018)".

It is possible to get the data using free-form input. On a Mac Control = (probably the same on Windows) and type in "msft p/e ratio 2000 to 2018". enter image description here

Evaluation returns a TimeSeries enter image description here

Converting the free-form cell to InputForm gives the following which you can modify and evaluate.

Entity["Financial", "NASDAQ:MSFT"][
   EntityProperty["Financial", 
     "PriceEarningsRatio", 
     {"Date" -> Interval[{DateObject[{2000}], DateObject[{2018}]}]}]]

There are several other financial properties that can be queried using the entity framework.

EntityProperties["Financial"] // CanonicalName

{"AdjustedClose", "AdjustedHigh", "AdjustedLow", "AdjustedOpen", 
"Ask", "AskSize", "Average200Day", "Average50Day", 
"AverageVolume3Month", "Bid", "BidSize", "Change", "Change200Day", 
"Change50Day", "ChangeHigh52Week", "ChangeLow52Week", "CIK", "Close", 
"Company", "CumulativeFractionalChange", "CumulativeReturn", 
"Currency", "Dividend", "DividendPerShare", "DividendYield", 
"EarningsPerShare", "EarningsYield", "EBITDA", "Exchange", 
"FloatShares", "ForeignListing", "FractionalChange", 
"FractionalChange200Day", "FractionalChange50Day", 
"FractionalChangeHigh52Week", "FractionalChangeLow52Week", 
"FundamentalCurrency", "FundFamily", "FundStyle", "High", 
"High52Week", "Image", "IPODate", "Issue", "Last", "LastTradeSize", 
"LatestTrade", "Low", "Low52Week", "MarketCap", "Name", 
"OfficialName", "Open", "OriginalSharePrice", "Price", 
"PriceEarningsRatio", "Range52Week", "RawClose", "RawHigh", "RawLow", 
"RawOpen", "RawVolume", "Return", "Sector", "ShareClassDescription", 
"ShortSymbol", "Symbol", "Type", "Volatility20Day", 
"Volatility250Day", "Volatility50Day", "Volume", "Website"}
POSTED BY: Rohit Namjoshi

Hi Jason,

This is really weird. With the calls FinancialData["MSFT","Close", {2000, 1, 1}] and FinancialData["MSFT","Volume", {2000, 1, 1}] I do get time series, but in the format of a regular list, i.e. not with the prefix TimeSeries, as in your case. With FinancialData["MSFT", "PERatio", {2000, 1, 1}] I only get the latest PE-Ratio. See the accompanying notebook.

I work with Mathematica 11 Home Edition on Windows 10 pro on a PC with Intel i5, . Do you maybe have a different edition of Mathematica? Or should I do something on the settings of the program?

Thanks a lot for your help.

Attachments:
POSTED BY: Laurens Wachters

I don't reproduce - for me the FinancialData call returns a TimeSeries:

enter image description here

The time series has many properties you can query:

In[10]:= msft["Properties"]

Out[10]= {"DatePath", "Dates", "FinancialProperty", "FirstDate", 
"FirstTime", "FirstValue", "LastDate", "LastTime", "LastValue", 
"Path", "PathFunction", "PathLength", "Times", "ValueDimensions", 
"Values"}

And you can visualize it as well:

enter image description here

POSTED BY: Jason Biggs
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract