pmorissette / ffn

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

Round-trip problem with `to_price_index` and `to_returns` #95

Open KikeM opened 4 years ago

KikeM commented 4 years ago

Hi,

Great job you have done here.

I have realized that the methods to_price_index and to_returns have a round-trip problem.

It is due to the fact that to_price_index is not including the start parameter it takes as an argument in the first value of the returned prices.

Minimal working example

import ffn
import numpy as np

ffn.extend_pandas()

ret_ss = pd.Series(np.random.randn(100))
prices_ss = ret_ss.to_price_index(start = 100)

ret_ss.head()
# 0   -0.934990
# 1   -0.017586
# 2   -0.116771
# 3   -0.474929
# 4    0.566763
# dtype: float64

prices_ss.head()
# 0    6.500992
# 1    6.386665
# 2    5.640886
# 3    2.961868
# 4    4.640544
# dtype: float64

# The first value is using the first return, it is not start!
assert prices_ss[0] == (100 * (ret_ss[0] + 1))

# No round trip 
prices_ss.to_returns().head()
# 0         NaN
# 1   -0.017586
# 2   -0.116771
# 3   -0.474929
# 4    0.566763
# dtype: float64