mhallsmoore / qstrader

QuantStart.com - QSTrader backtesting simulation engine.
https://www.quantstart.com/qstrader/
MIT License
2.93k stars 855 forks source link

Issue with Develop branch | ValueError: cannot convert float NaN to integer #264

Closed JamesKBowler closed 4 years ago

JamesKBowler commented 6 years ago

Hi all, Just looking at the develop branch and thought I would share some code for anyone trying to get a working example. Also I came across is slight error as per traceback below.

ValueError                                Traceback (most recent call last)
<ipython-input-4-193f828b3b76> in <module>()
----> 1 bts.run()

~/venv/qstrader_dev/lib/python3.5/site-packages/qstrader/simulation/backtest_trading_simulation.py in run(self)
    284             self.exchange.update(dt)
    285             self.broker.update(dt)
--> 286             self.qta.update(dt)
    287             if event.event_type == "post_market":
    288                 self.statistics.update(dt)

~/venv/qstrader_dev/lib/python3.5/site-packages/qstrader/algo/quant_trading_algo.py in update(self, dt)
     47         # Generate the list of orders
     48         self.pcm.update(dt)
---> 49         order_list = self.pcm.generate_orders(forecasts)
     50         order_list = sorted(order_list, key=lambda x: x.quantity)
     51 

~/venv/qstrader_dev/lib/python3.5/site-packages/qstrader/algo/pcm.py in generate_orders(self, forecasts)
    302 
    303             # Generate the desired portfolio
--> 304             alpha_portfolio = self._construct_desired_alpha_portfolio()
    305             risk_portfolio = self._construct_desired_risk_portfolio(
    306                 alpha_portfolio

~/venv/qstrader_dev/lib/python3.5/site-packages/qstrader/algo/fixed_weight_pcm.py in _construct_desired_alpha_portfolio(self)
    157                             (
    158                                 ac_dollar_weight * self.adjustment_factor
--> 159                             ) / asset_market_value
    160                         )
    161                     )

ValueError: cannot convert float NaN to integer

As a work around, I have added if asset_market_value is not np.NaN:, which allows the backtest to complete and print the tearsheet.

           if asset_market_value is not np.NaN:
                alpha_portfolio[asset] = {
                    "quantity": int(
                        math.floor(
                            (
                                ac_dollar_weight * self.adjustment_factor
                            ) / asset_market_value
                        )
                    )
                }

To run the example, I navigated to the examples/sixty_forty dir and opened up IPython.

from qstrader.simulation.backtest_trading_simulation import BacktestTradingSimulation
import config as settings
bts = BacktestTradingSimulation(settings)
bts.run()

Tear-off sheet output using the default AGG.csv and SPY.csv data sets.

qstrader_dev_output

Does this output look correct?

Also any news on the next release, its seems a little dead on here and on the README it says early 2018 release? Is there some other secrete channel (not Slack)?

Many Thanks

James

junaidnasir commented 4 years ago

I think this error means that data is missing, if you are running the default example and data from master branch. the AGG data starts from 2006-11-01 (you can change start date in the example file