简体   繁体   中英

Dynamic portfolio rebalancing Python

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()`

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM