Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The example of calculating daily returns on the homepage is wrong #517

Open
Eddi-S opened this issue Sep 24, 2020 · 2 comments
Open

The example of calculating daily returns on the homepage is wrong #517

Eddi-S opened this issue Sep 24, 2020 · 2 comments

Comments

@Eddi-S
Copy link

Eddi-S commented Sep 24, 2020

On the homepage it is described how you can use the diff method on a series to calculate the daily returns from a series of prices Link to homepage

I think that example is wrong - you write "To calculate daily returns, we need to subtract the price on previous day from the price on the current day. This is done by using the Diff extension method (another option is to use Shift together with the overloaded subtraction operator). Then we divide the difference by the current price and multiply the result by 100.0 to get value in percents."
This gives the formula:
(CurrentPrice - PreviousPrice) / CurrentPrice

But this is wrong the formula should be:
(CurrentPrice - PreviousPrice) / PreviousPrice
As described on wikipedia

The code on the homepage looks like this:
var returns = msft.Diff(1) / msft * 100.0;
But should be like this (assuming the key is a DateTime):
var returns = msft.Diff(1) / msft.SelectKeys(x => x.Key.AddDays(1) * 100.0;

@zyzhu
Copy link
Contributor

zyzhu commented Sep 24, 2020

Thanks for pointing it out. This is definitely wrong. Will fix it.

@Eddi-S
Copy link
Author

Eddi-S commented Oct 6, 2020

@zyzhu I found a more elegant way to calculate the return/pct change:
var returns = msft.Diff(1) / msft.Shift(1) * 100;
It could be useful to have a built in function called PctChange (like pandas pct_change)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants