Message Boards Message Boards

0
|
4194 Views
|
6 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Stock prices and time series charts

Posted 3 years ago

Hi All,

I have recently started to work with Wolfram and Mathematica. I would appreciate your support on a simple query I am trying to write.

I am using Financialdata to grab stock returns for a period (lets say 5 years). As an example

FinancialData["Humana", "Return", {{2015, 1, 1}, {2020, 12, 31}, ]

I have tested to see that the data returns (via "Path") are the daily price return. I am looking to aggregate this by month. The TimeSeriesAggregate function computes the mean as opposed to adding up the daily returns for the month.

Can anyone help?

There are a couple of follow on questions that I am sure I will need assistance on so if anyone is happy to support my queries can you please let me know. I promise they will be simple and not time-consuming for a superuser.

Thanks, Ajay

POSTED BY: Ajay Treon
6 Replies
Posted 3 years ago

Ajay:

You do not want to put the quotes around the third argument, Plus. Unfortunately, this "fix" leads to a different error:

TimeSeriesAggregate[stockpxreturn, "Month", Plus]

gives the following error:

TemporalData::dmmtch2: The dimensionality of the time series values {{{0.445985%,1.5595%,-0.993116%,2.21605%,-2.79733%,-0.106997%,-1.98835%,0.103675%,1.90903%,0.840497%,<<10>>},{-0.936829%,2.90012%,6.43326%,-1.54879%,-1.63998%,-0.0736029%,1.18131%,4.67284%,1.69583%,-1.14414%,<<10>>},{-5.78816%,14.4359%,-0.467312%,-0.968698%,-8.12239%,3.58837%,-6.03492%,-10.7154%,7.11824%,-13.9212%,<<13>>},{-2.82562%,12.2895%,-2.26586%,5.37729%,1.37579%,1.8602%,1.1342%,0.37762%,6.72342%,-0.985927%,<<10>>},{1.50441%,1.6462%,-2.36161%,-0.952019%,1.63696%,0.67285%,0.965683%,-1.63925%,2.22298%,3.21661%,<<11>>},{-2.7587%,-0.370818%,-0.614374%,2.11358%,-1.98945%,0.597165%,-4.833%,1.38673%,0.0343274%,-0.224365%,<<12>>},{1.16614%,-1.20062%,-1.4552%,-0.797924%,-0.34995%,0.076002%,3.84163%,-0.295056%,0.131527%,2.17231%,<<10>>},{0.535094%,-0.854136%,3.25426%,0.920998%,2.52925%,0.435471%,-0.950548%,1.03182%,-0.133314%,0.600713%,<<13>>},{-1.51717%,-1.92034%,-1.89259%,0.703063%,-3.63775%,-0.510972%,3.13523%,-0.90182%,-0.230001%,-0.393417%,<<11>>},{2.76995%,-0.815019%,3.17395%,-0.486713%,1.43218%,-0.673683%,0.531917%,-2.26658%,2.92198%,0.542084%,<<11>>},<<2>>}} is inconsistent with ValueDimensions->1.

Which is my fault. Most days, i end up programming in at least 6 different languages. You need Total here, not Plus.

I think this will give you what you are looking for:

TimeSeriesAggregate[stockpxreturn, "Month", Total]

Have a great rest of your day.

POSTED BY: Mike Besso
Posted 3 years ago

Mike - Thanks. This does work.

Now onto the other issues. For instance, If you select the property Return in the function FinancialData you get the daily return but when you ask for a particular month it start on the 2nd day as opposed to working out the return for the first day.

Fun and games, but loving it.

POSTED BY: Ajay Treon
Posted 3 years ago

Hi Ajay,

Just a guess - daily return for day X is computed as (day (X - 1) close - day X close) / day (X - 1) close. The date range specifies the days used to compute return so the first day's return cannot be computed. Extend the date range to include X - 1 to get the return for day X.

FinancialData["Humana", "Return", {{2020, 12, 3}, {2020, 12, 3}}]
(* {} *)

(Normal@FinancialData["Humana", "Return", {{2020, 12, 2}, {2020, 12, 3}}])[[1, 2]]
(* Quantity[-0.473201, "Percent"] *)
POSTED BY: Rohit Namjoshi
Posted 3 years ago

Thanks Rohit. Is it possible to connect as I saw one of your earlier threads on seasonal patterns and would not mind discussing it?

POSTED BY: Ajay Treon
Posted 3 years ago

Without seeing how you are calling TimeSeriesAggreate, I can only guess.

Have you tried using the third argument (f) to TimeSeriesAggregate? The third argument, defaults to "Mean", but it sounds like you might want to pass in "Plus" instead.

THANKS

POSTED BY: Mike Besso
Posted 3 years ago

Thanks Mike.

stockpxreturn = 
 FinancialData["Humana", "Return", {{2020, 1, 1}, {2020, 12, 31}}]

byMonth = TimeSeriesAggregate[stockpxreturn, "Month", "Plus"]

This still does not work. I am not sure what I am doing wrong. Logically, I am retrieving 1 year of daily data and simply trying to aggregate by month and Plot?

Any thoughts.

POSTED BY: Ajay Treon
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