动态投资组合重新平衡Python

时间:2019-11-10 13:26:50

标签: python pandas numpy

我面临我的数据问题。我被困了两天。

我想使用今天的值和接下来的251个值,每天计算投资组合的年收益。     我应该每天进行此计算以创建收益的时间序列。

e.g. Calculation 1: Day 1 + following 251 values
Calculation 2: Day 2 + following 251 values
Calculation 3: Day 3 + following 251

I tried to develop the dynamic structure of the calculation, but I failed.
Is it possible for some of you guys to come up with some ideas? 

Highly appreciate! 

# Starting at 18-03-2008, we compute the optimal annual weights an         
returns of our portfolio, day-by-day

DowJones['Number'] = range(1, 1711)

i = 1

while i < 1458:

DJ30=DowJones[DowJones['Number']< i+253]`
DJ30.iloc[1:]`
i=i+1`
DJ30.iloc[1]`
DJ30 = DJ30.iloc[i:]` 
DJ30 = DJ30.set_index('Date')`
returns = DJ30.pct_change()`

1 个答案:

答案 0 :(得分:0)

要获取以下251个值的总和:排序,滚动,总和:

df['one_year_sum'] = df.sort_values(by=['datetime'],ascending=False)['data'].rolling(251).sum()

评论后编辑:

df['Number'].pct_change(251) # Or -251