交易策略:投资的计算价值

问题描述 投票:0回答:2

我是Python的新手,这是我的问题:

我有这个DataFrame:

pf = pd.DataFrame({'Dates':['2018-07-31','2018-07-31','2018-08-31','2018-08-31','2018-09-30','2018-09-30'],
                   "Name":["Apple",'Faceook','JP Morgan',"Boeing",'Tesla','Disney'],
                   "Monthly Return":[-0.02,0.11,-0.03, 0.02,-0.08,0.10],
                  "Total Weight":[0.7,0.2,0.1,0.5,0.3,0.2]})

我的目标是根据每月的库存,权重和回报来返回投资的最终价值。

假设我们从$ 1.000开始。我正在寻找类似下面的内容,假设每个月的剩余资本为完全再投资

      Dates      Name     Monthly Return    Total Weight     P&L   Remaining Investment
0   2018-07-31  Apple         -0.02            0.7           686 
1   2018-07-31  Faceook        0.11            0.3           333          1019
2   2018-08-31  JP Morgan     -0.03            0.5           494.21
3   2018-08-31  Boeing         0.02            0.5           519.69       1013.91
4   2018-09-30  Tesla         -0.08            0.1           93.28
5   2018-09-30  Disney         0.10            0.9           1003.77      1097.05

我首先是这样做的:

A = 1000
pf['P&L']= A * pf['Total Weight'] * (1+pf['Monthly Return'])

但是问题是,这仅适用于第一个月,而其他月份则不适用,因为我们没有具有相同的起点(7月31日为1000美元,8月31日为1019美元)。

也许我必须创建一个循环吗?

如果您有任何解决此问题的想法,请随时与我们分享!谢谢!

python pandas time-series finance
2个回答
2
投票

对于这样的事情,使用零美元投资组合来追踪回报会更容易。

df = pd.DataFrame({'Dates':['2018-07-31','2018-07-31','2018-08-31','2018-08-31','2018-09-30','2018-09-30'],
                   "Name":["Apple",'Faceook','JP Morgan',"Boeing",'Tesla','Disney'],
                   "Monthly Return":[-0.02,0.11,-0.03, 0.02,-0.08,0.10],
                  "Total Weight":[0.7,0.2,0.1,0.5,0.3,0.2]})

df['weighted_return'] = df['Monthly Return'] * df['Total Weight']
rets = df.groupby(['Dates'])['weighted_return'].sum()
cumulative_rets = (rets + 1).cumprod() - 1
cumulative_rets

所以您的退货系列是:

Dates
2018-07-31   0.00800
2018-08-31   0.01506
2018-09-30   0.01100
Name: weighted_return, dtype: float64

现在,您可以考虑如何合并交易成本,滑点等。零美元投资组合方法的主要问题在于,它假设您可以购买零碎股票。通常在学术论文中使用这种零美元方法。

您应签出pyfolio以显示结果并计算风险指标。


0
投票

假设我们的每月收益稳定。然后,我们需要附加的整数变量months

investment = 1000
months = 1
pf['P&L'] = investment * pf['Total Weight'] * (1+pf['Monthly Return'])**months
© www.soinside.com 2019 - 2024. All rights reserved.