Compute a compounded return series in Python

后端 未结 3 1774
礼貌的吻别
礼貌的吻别 2021-02-02 02:21

Greetings all, I have two series of data: daily raw stock price returns (positive or negative floats) and trade signals (buy=1, sell=-1, no trade=0).

The raw price retur

3条回答
  •  执念已碎
    2021-02-02 03:03

    The cumulative return part of this question is dealt with in Wes McKinney's excellent 'Python for Data Analysis' book on page 339, and uses cumprod() from Pandas to create a rebased/indexed cumulative return from calculated price changes.

    Example from book:

    import pandas.io.data as web
    
    price = web.get_data_yahoo('AAPL', '2011-01-01')['Adj Close']
    
    returns = price.pct_change()
    
    ret_index = (1 + returns).cumprod()
    
    ret_index[0] = 1 # Set first value to 1
    

提交回复
热议问题