pmorissette / ffn

ffn - a financial function library for Python
pmorissette.github.io/ffn
MIT License
1.97k stars 294 forks source link

ffn.core.to_monthly not picking the last month? #1

Closed sivananth closed 3 years ago

sivananth commented 9 years ago

Hi,firstly i want to say thank you! this is an awesome package for financial researchers.

This is my code -

from pandas.io.data import DataReader import ffn moat=DataReader('MOAT','yahoo').reset_index().loc[:,['Date','Adj Close']].set_index('Date') moat=moat[moat.index<='31-aug-2014'] moat.tail() ffn.core.to_monthly(moat).tail()

my question is, why do we not see the August data after converting to monthly?

pmorissette commented 9 years ago

Hey @sivananth,

Thanks for the kind words. In this case, it does indeed appear that the last month is not there since the last day in the dataset is August 29. Let me look into this because it should be there.

However, here is a way to get what you are looking for:

import ffn
moat = ffn.get('moat')
moat = moat.to_monthly()[:'2014-08']
print moat.tail()

As you can see, you have the august data as well. Also, I am using ffn's "get" function - it downloads the adjusted close from yahoo - easier this way (it uses DataReader).

Cheers, Phil

sivananth commented 9 years ago

Hi @pmorissette , i noticed something else - indexes=ffn.get('spy').to_monthly()[:'2015-03'] stats=ffn.core.GroupStats(indexes) stats.stats.loc['three_year'] 0.1671192 indexes.tail(37) .... pow(206.42999/132.43004,1/3.0)-1.0 0.15947688146798633

As you see, the three year return ending Mar 15 doesnt match; it doesnt seem related to the earlier date issue as well; the returns for other horizons (i checked one and 5 yr) seem alright

If i try the same code for 3 year ending April 15, the 3 yr returns are quite close -

indexes=ffn.get('spy').to_monthly()[:'2015-04'] indexes.tail(37) pow(208.46001/131.54597,1/3.0)-1.0 0.165865369061164 stats=ffn.core.GroupStats(indexes) stats.stats.loc['three_year'] 0.1659879

Am i missing something here?

Regards Siva

pmorissette commented 9 years ago

Hey @sivananth,

Thanks for the feedback.

The code is basically looking at the last date (2015-03-31) and subtracting 3 years from it. This leaves us with the date 2012-03-31 - let's call it t0. Once it has that date, it takes a slice of the initial price series from t0 until the end representing the last 3 years of data.

In this case, it appears that 2012-03-31 was not a trading day, so the last day of that month was actually 2012-03-30. Therefore, 2012-03-30 is not included in the filtered price series and the numbers are off since the first day in the slice is 2012-04-30.

This issue is tricky to solve. If we knew we always had monthly data, it would be easy to fix, but this is not the case. The code must remain flexible to support any kind of frequency and as of now, this frequency is unknown.

I'm open to ideas on how to solve this issue. You can look at the code in PerformanceStats._update for a better idea on how this all works.

Hope this helps, Phil

sivananth commented 9 years ago

Hi @pmorissette

I'm a rookie coder, but one option i usually fall back when the periods are not known in advance is to use pandas resample to daily frequency (or the frequency you require), and then work on the calculation (like trailing returns).

I've a minor request - is it possible to include the max drawdown period also, in addition to max drawdown? MSCI factsheets show the drawdown period for their major indexes (https://www.msci.com/resources/factsheets/index_fact_sheet/msci-world-index.pdf)

Thanks a lot for your contribution!

pmorissette commented 9 years ago

Hey @sivananth,

You can obtain detailed drawdown information (start date, end date, duration and drawdown) by using the ffn.drawdown_details function.

A quick example:

spy = ffn.get('spy', start='2000-01-01').spy
# get details
dd = ffn.drawdown_details(spy.to_drawdown_series())
# find details for max drawdown
dd[dd.drawdown == dd.drawdown.max()]

Hope this helps, Phil

Stuj79 commented 7 years ago

Firstly, many apologies if this is not the correct place for this question, and also want to say how awesome this module is!

I am able to use the GroupStats and PerformanceStats objects and their associated methods, however whenever I try to use anything from the API under the ffn.core area, I just get error messages telling me:

"'module' object has no attribute xyz"

For example I am trying to use the "calc_calmar_ratio" function - I have tried importing ffn, importing ff.core, using "from ffn import" and all sorts but I can not get it to work.

Could you please help me to use the ff.core functions as your module is a life saver!

many thanks.