pmorissette / ffn

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

PerformanceStats ".monthly_returns" fails to work if a whole month is missing data #64

Closed Stuj79 closed 3 years ago

Stuj79 commented 6 years ago

Hi,

Firstly, LOVE this package - it has been a god send to me so many thanks for your hard work and choosing to share it with us all.

I have noticed what I believe to be a "bug" with the ".monthly_returns" when used on certain input data and with a PerformanceStats object.

When using daily periodicity data (or intra-day) as a base input to the PerformanceStats object, if there is a whole calendar month period without a corresponding entry in the series (or more than one month in a row), the resulting data returned seems to be incorrect/incomplete.

I have provided two csv input files and a Jupyter notebook that replicates the 2 issues I have found, with further explanation within.

I may be missing something my end which is causing this, but I do believe it to be a legitimate "bug". ffn_bug_examples.zip

ViktorMalesevic commented 6 years ago

Hi,

I believe this might be due to the fact that .dropna() is not being done on monthly resampled data. @JordanPlatts what are your thoughts on that?

JordanPlatts commented 6 years ago

Bug 1

The .pct_change() method uses fill forward to replace missing prices where ffn currently uses shift as you can see below.

image

This is apparent when you run .pct_change() on monthly_prices. image

I don't view this as a bug because I don't like the library making the assumption that prices should filled forward. With that said, if everyone feels different I can change it.

image

The question is do you think the price in the beginning of May was 118 and should the ffn library assume that.

Onto bug 2

JordanPlatts commented 6 years ago

It was the same issue for bug 2.

image

The price 4416.56 was used as the beginning of September.

image

Stuj79 commented 6 years ago

Thanks for your time in looking into this, and apologies for the late reply.

I guess it all come down to this question, as you put it:

"The question is do you think the price in the beginning of May was 118 and should the ffn library assume that."

I use this package almost exclusively to analyse results of systematic trading strategies, and missing data for a month implies nothing more than the fact that there were just no trades that month, and hence the "price" of my series didn't change (as it is just the index of performance), therefore the ending "price" of the last month with a non zero return can indeed be seen as the starting "price" for the next month with a non zero return.

Whether "missing data" should be treated as truly "missing" rather than just "non existent" is a subjective call I guess - partly depends on what you are actually analysing.

I would have thought (perhaps naively) that a decent proportion of users would be using the ffn package to analyse "user created" price series such as results of trading strategies/portfolios, along with those who use it to analyse pure stock exchange price data.

Once again, I do appreciate you taking the time to look into my request - much appreciated!

JoeOD commented 5 years ago

I'm having the same issue. Is there a suggestion for dealing with series of returns which contain 0s or missing values, due to the fact that the trading algo was "out of the market" for that period (position size was 0)?