我有一个代表股票收益的DataFrame
。为了进行分割调整,我有以下方法:
def returns(ticker, start=None, end=None):
p = historical_prices(ticker, start, end, data='d', convert=True)
d = historical_prices(ticker, start, end, data='v', convert=True)
p['Dividends'] = d['Dividends']
p['Dividends'].fillna(value=0, inplace=True)
p['DivFactor'] = 1.
p['SAClose'] = p['Close']
records, fields = p.shape
for t in range(1, records):
p['SAClose'][t] = p['Adj Close'][t] / p['DivFactor'][t-1] + \
p['Dividends'][t-1]
p['DivFactor'][t] = p['DivFactor'][t-1] * \
(1 - p['Dividends'][t-1] / p['SAClose'][t])
p['Lagged SAClose'] = p['SAClose'].shift(periods=-1)
p['Cash Return'] = p['Dividends'] / p['Lagged SAClose']
p['Price Return'] = p['SAClose'] / p['Lagged SAClose'] - 1
return p.sort_index()
请注意
SAClose
(即拆分调整收盘价)如何取决于滞后的DivFactor
值。反过来,DivFactor
取决于滞后的DivFactor
值以及当前的SAClose
值。上面的方法可行,但在循环部分非常慢。在pandas中有没有更有效的方法可以做到这一点?鉴于"循环"依赖关系(考虑到滞后),我不确定我如何进行正常的系列数学或使用正常的移位操作(例如我在
Cash Return
中所做的)。
cumprod
中。有什么想法吗? - MikeRand