mhallsmoore / qstrader

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

No Tearsheet after Installation -- Problem with python-tk #258

Closed LiamBui closed 6 years ago

LiamBui commented 6 years ago

Hi there,

I was having trouble running the backtest example from the readme. I run

$ python buy_and_hold_backtest.py

And result in the following:

(qstraderp3) root@desktop:~/qstrader/examples# python buy_and_hold_backtest.py
Running Backtest...
---------------------------------
Backtest complete.
Sharpe Ratio: 0.25
Max Drawdown: 79.59%
Traceback (most recent call last):
  File "buy_and_hold_backtest.py", line 70, in <module>
    run(config, testing, tickers, filename)
  File "buy_and_hold_backtest.py", line 58, in run
    results = backtest.start_trading(testing=testing)
  File "/root/venv/qstraderp3/lib/python3.5/site-packages/qstrader/trading_session.py", line 167, in start_trading
    self.statistics.plot_results()
  File "/root/venv/qstraderp3/lib/python3.5/site-packages/qstrader/statistics/tearsheet.py", line 611, in plot_results
    fig = plt.figure(figsize=(10, vertical_sections * 3.5))
  File "/root/venv/qstraderp3/lib/python3.5/site-packages/matplotlib/pyplot.py", line 535, in figure
    **kwargs)
  File "/root/venv/qstraderp3/lib/python3.5/site-packages/matplotlib/backends/backend_tkagg.py", line 84, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/root/venv/qstraderp3/lib/python3.5/site-packages/matplotlib/backends/backend_tkagg.py", line 92, in new_figure_manager_given_figure
    window = Tk.Tk()
  File "/usr/lib/python3.5/tkinter/__init__.py", line 1871, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

After Googling the problem, I saw that this problem can be bypassed by adding the following lines to the top of the example backtest python file:

import matplotlib
matplotlib.use('Agg')

However, I'm still not seeing a tearsheet after running (and completing) the following command:

$ python buy_and_hold_backtest.py

I simply get the sharpe ratio, max drawdown, and in ~/out, I get a list of the transactions made.

Is there something that I'm doing wrong in the installation process? How can I resolve this problem?

JamesKBowler commented 6 years ago

Have you looked at this mate?

211

LiamBui commented 6 years ago

Hey, thanks for the response. I looked at that issue and had looked for previous issues regarding the tearsheet. None of the previous issues raised accurately described my problem, and none of the solutions worked. The problem I'm facing is that the tearsheet does not appear whatsoever.

dieterjansen commented 6 years ago

Hi LiamBui,

Changing plt.show(block=False) to plt.show(block=True) in tearsheet.py has always worked for me. There have been release dependant problems related to the Matplotlib backend in the past (maybe with QSForex rather than QSTrader, can’t remember) but if you have installed in a virtualenv with the requirements.txt file you should have avoided that.

Sorry I can’t help.

Cheers, Dieter.

LiamBui commented 6 years ago

Thanks, Dieter! I had already tried that, but thank you for the input :)

I did find a solution, however. Context: I'm running Anaconda in Windows 10, but had been running bash from the Anaconda prompt (silly lol). Turns out, in order to display the tearsheet with bash, all I had to do was add the line export DISPLAY=:0 to my .bashrc file. Also, while stumbling upon this solution, I also found that if you're running qstrader from the Ubuntu bash shell in Windows 10, you'll run into the same error as the one described above. You can solve the issue by following the steps here.