gpt4 book ai didi

python - 动态投资组合再平衡 Python

转载 作者:太空宇宙 更新时间:2023-11-03 20:09:21 25 4
gpt4 key购买 nike

我的数据遇到问题。我已经被困了两天了。

我想使用今天的值和接下来的 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()`

最佳答案

要获取接下来 251 个值的总和:排序、滚动、求和:

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

评论后编辑:

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

关于python - 动态投资组合再平衡 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58789117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com