Reputation: 11
i am facing a problem with my data. I'm stuck since two days.
I want to calculate an annual return of my portfolio on a daily basis by using todays value and the next 251 values. I should do this calculation on a daily basis to create a time-series of returns.
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()`
Upvotes: 1
Views: 1100
Reputation: 13901
To get the sum om the next 251 values: Sort, roll, sum:
df['one_year_sum'] = df.sort_values(by=['datetime'],ascending=False)['data'].rolling(251).sum()
Edit after comment:
df['Number'].pct_change(251) # Or -251
Upvotes: 1