[英]Multiple stocks portfolio return with rebalancing
So I have a portfolio composition (some shares included in the portfolio), and at specific dates I need to rebalance -that means the composition of the portfolio changes. 所以我有一个投资组合(投资组合中包含一些股票),并且在特定日期我需要重新平衡 - 这意味着投资组合的组成发生了变化。 Dataframe includes columns ['Names';
Dataframe包括列['Names'; 'Date_rebalancing';
'Date_rebalancing'; 'Weights']
“权重”]
Then I have another big dataframe with daily return of all possible stocks. 然后我有另一个大数据框,每天可以返回所有可能的股票。 I need to extract the portfolio return daily.
我需要每天提取投资组合回报。
So when the rebalancing date happens portfolio_return_next_day is sumproduct(weights; returns_only_for_those_companies). 因此,当重新平衡日期发生时,portfolio_return_next_day是sumproduct(weights; returns_only_for_those_companies)。 Daily return is computed.
计算每日回报。
stock_return=prices.apply(lambda x: x/x.shift(1)-1)
I expect a list of daily portfolio returns 我期待每日投资组合回报列表
On your big dataframe, you need to filter so that it matches your portfolio. 在您的大数据框架上,您需要进行过滤以使其与您的投资组合相匹配。 So use .isin()
所以使用.isin()
df_select = df_big.loc[df_big['name'].isin.[df_port['name'],:]
df_select is now a dataframe with just your stock daily values. df_select现在是一个只包含每日股票价值的数据框。 To get daily returns, there is a built in function pct_return() .
为了获得每日回报,有一个内置函数pct_return() 。 (# defaults to one period)
(#默认为一个时期)
Let's say your column of closing prices is called 'Close'. 假设您的收盘价格栏名为“关闭”。 Then create a new columns from the daily returns.
然后从每日返回中创建一个新列。
df_select['return'] = df['Close'].pct_return()
This will give you the dataframe with your stocks and percent return daily values. 这将为您提供包含您的股票和百分比每日返回值的数据框。
df_unique_dates
are the dates where the rebalancing should take place. df_unique_dates
是重新平衡应该发生的日期。 I already done the returns for each stock in stock_return
but at each rebalancing date I have to select only those stocks that appear in portfolio list_stocks
我已经做了每只股票的回报在
stock_return
,但在每一个重新平衡日期我只选择那些出现在投资组合股票list_stocks
for i in range(len(df_unique_dates)):
list_stocks=dataset.Name.loc[df_unique_dates[i]]
for j in range(len(stock_return)):
if stock_return.index[j]>df_unique_dates[i] and stock_return.index[j]<=df_unique_dates[i+1]:
stock_return['portfolio']=0.1*stock_return[list_stocks].sum(axis=1)
stock_return
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.