Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas, how to get close price from returns?

I'm trying to convert from returns to a price index to simulate close prices for the ffn library, but without success.

import pandas as pd

times = pd.to_datetime(pd.Series(['2014-07-4',
'2014-07-15','2014-08-25','2014-08-25','2014-09-10','2014-09-15']))

strategypercentage = [0.01, 0.02, -0.03, 0.04,0.5,-0.3]
df = pd.DataFrame({'llt_return': strategypercentage}, index=times)

df['llt_close']=1
df['llt_close']=df['llt_close'].shift(1)*(1+df['llt_return'])

df.head(10)


        llt_return  llt_close
2014-07-04  0.01    NaN
2014-07-15  0.02    1.02
2014-08-25  -0.03   0.97
2014-08-25  0.04    1.04
2014-09-10  0.50    1.50
2014-09-15  -0.30   0.70

How can I make this correct?

like image 550
Rami Ramich Avatar asked Sep 20 '25 14:09

Rami Ramich


1 Answers

You can use the cumulative product of return-relatives.

A return-relative is one-plus that day's return.

>>> start = 1.0
>>> df['llt_close'] = start * (1 + df['llt_return']).cumprod()

>>> df
            llt_return  llt_close
2014-07-04        0.01     1.0100
2014-07-15        0.02     1.0302
2014-08-25       -0.03     0.9993
2014-08-25        0.04     1.0393
2014-09-10        0.50     1.5589
2014-09-15       -0.30     1.0912

This assumes the price index starts at start on the close of the trading day prior to 2014-07-04.

On 7-04, you have a 1% return and the price index closes at 1 * (1 + .01) = 1.01.

On 7-15, return was 2%; close price will be 1.01 * (1 + .02) = 1.0302.

Granted, this is not completely realistic given you're forming a price indexing from irregular-frequency data (missing dates), but hopefully this answers your question.

like image 149
Brad Solomon Avatar answered Sep 23 '25 10:09

Brad Solomon