简体   繁体   English

在 Python 中回测投资组合

[英]Backtesting a portfolio in Python

I am trying to do a backtest on a Markowitz portfolio.我正在尝试对 Markowitz 投资组合进行回测。 So far I've tried zipline, backtrader and QSTrader (although QSTrader may work, but there is no documentation so its very hard).到目前为止,我已经尝试过 zipline、backtrader 和 QSTrader(虽然 QSTrader 可能有效,但没有文档,所以很难)。 I haven't had any luck with creating the backtest as I've wanted.我没有任何运气来创建我想要的回测。

My data structure is a csv consisting of the adjusted closing prices for 200 different stocks.我的数据结构是 csv,包含 200 只不同股票的调整后收盘价。 I want to do a portfolio rebalance every quarter or year.我想每季度或每年进行一次投资组合重新平衡。 I already have the code for the actual portfolio optimization and the weights it returns.我已经有了实际投资组合优化的代码和它返回的权重。 I just need an actual framework to plug those weights into and then redo the calculation every quarter yearly.我只需要一个实际的框架来插入这些权重,然后每年每季度重做一次计算。 I've been at it for around 5 hours so far and I just simply cant get any backtest to work.到目前为止,我已经做了大约 5 个小时,我只是无法进行任何回测。 Zipline is very confusing in how it handles data and even more so when it comes to importing a local csv with the structure i described. Zipline 在如何处理数据方面非常令人困惑,在导入具有我描述的结构的本地 csv 时更是如此。 Backtrader suffers bit of the same problem. Backtrader 也遇到了同样的问题。 QSTrader seems to just not work for me and it throws the following error after loading the data: QSTrader 似乎对我不起作用,它在加载数据后抛出以下错误:

Traceback (most recent call last):
  File "d:\Finansiering. Modern Portfolio Theory Projekt\Finansiering_Backtrader.py", line 53, in <module>
    strategy_backtest.run()
  File "D:\Anaconda\envs\zipline\lib\site-packages\qstrader\trading\backtest.py", line 398, in run
    self.qts(dt, stats=stats)
  File "D:\Anaconda\envs\zipline\lib\site-packages\qstrader\system\qts.py", line 172, in __call__
    rebalance_orders = self.portfolio_construction_model(dt, stats=stats)
  File "D:\Anaconda\envs\zipline\lib\site-packages\qstrader\portcon\pcm.py", line 289, in __call__
    target_portfolio = self._generate_target_portfolio(dt, full_weights)
  File "D:\Anaconda\envs\zipline\lib\site-packages\qstrader\portcon\pcm.py", line 139, in _generate_target_portfolio
    return self.order_sizer(dt, weights)
  File "D:\Anaconda\envs\zipline\lib\site-packages\qstrader\portcon\order_sizer\dollar_weighted.py", line 168, in __call__
    'modifying the backtest start date and re-running.' % (asset, dt)
ValueError: Asset price for "A" at timestamp "2006-01-31 21:00:00+00:00" is Not-a-Number (NaN). This can occur if the chosen backtest start date is earlier than the first available price for a particular asset. Try modifying the backtest start date and re-running.

The start date of the backtest is correct, basically at the end of the month when it needs to rebalance it just gets a stroke.回测的开始日期是正确的,基本上在月底需要重新平衡时,它只是中风。 I haven't been able to fix this either.我也无法解决这个问题。

I hope someone has a semi plug and play solution to this.我希望有人对此有一个半即插即用的解决方案。

I'm not that experienced with zipline either, but it seems that zipline handles data with a data_bundle class.我对 zipline 也不是很有经验,但似乎 zipline 使用 data_bundle class 处理数据。 The documentation is not very clear indeed.文档确实不是很清楚。 Here's what you'll probably need to do:以下是您可能需要做的事情:

  1. Formatize your data in the csv files - better build a function for that.在 csv 文件中格式化您的数据 - 为此更好地构建 function。 The strict format of data bundles is included in the documentation under the section of custom csv bundle.数据包的严格格式包含在自定义 csv 包部分下的文档中。 Keep in mind that they need to have the exact same start and end dates, be aligned with your trading calendar, and have the EXACT same format as the csv provided in the documentation.请记住,它们需要具有完全相同的开始和结束日期,与您的交易日历保持一致,并且具有与文档中提供的 csv 完全相同的格式。

  2. You should have a ".zipline" folder, create/edit extension.py in it with the following contents: 1) register a trading calendar;你应该有一个“.zipline”文件夹,在里面创建/编辑extension.py,内容如下: 1) 注册一个交易日历; 2) register the custom csv bundle - both of which can be found in the documentation. 2) 注册自定义 csv 捆绑包 - 两者都可以在文档中找到。

  3. Type $zipline ingest -b [bundle_name] in your prompt - this should ingest the data.在提示符中键入 $zipline ingest -b [bundle_name] - 这应该会摄取数据。

  4. Call the data of each asset with "symbol" and specify the bundle name when running a backtest.使用“符号”调用每个资产的数据,并在运行回测时指定捆绑名称。

Hope this could help you.希望这可以帮助你。

I think bt - flexible backtesting for Python is more suited to what you want to do.我认为bt - Python 的灵活回测更适合您想要做的事情。 The library is designed to periodically rebalance financial portfolio.该库旨在定期重新平衡金融投资组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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