How to debug strategies

I understand that QR is designed to be piloted from JupyterLab and/or the command line. However, for obvious reasons I’d like to debug in something like PyCharm.

With previous projects I’ve used PyCharm to iron-out problems in JupyterLab notebooks and their dependencies. Upon getting the code right in the IDE, I would then restart the JupyterLab kernal and re-run the notebook, which isn’t ideal but I find to be an acceptable trouble-shooting workflow.

With docker I don’t know how to implement a similar approach, since the python files kinda don’t exist outside of JupyterLab (or the container). As a case in point, when a moonshot backtest is run from PyCharm, at a minimum the strategy file must reside over in codeload where it’s difficult to debug.

Passing code around through a code repository does not strike me as an appropriate solution, and I’m not sure whether Docker shared-volumes is suitable either.

Just wondering what workflow you use to develop and debug the strategies presented in your blog.

I recommend developing your code interactively in JupyterLab notebooks, inspecting your DataFrames and variables as you go. Then, transfer the code to a .py file to run a full Moonshot backtest. By the time you transfer the strategy to a .py file, at which point debugging becomes less convenient, hopefully it is already in pretty good shape, and you can do additional debugging by saving custom DataFrames to your results and/or printing variables to the logs.

The usage guide has a section on this recommended workflow for Moonshot. The intro video on the home page also demonstrates this approach.

Because notebooks are interactive, they are inherently good for debugging. You can also use the %debug magic and/or the iPython debugger inside notebooks.

The summary is, spend most of your time in notebooks.

Also, for moonshot strategies, (I don’t know about Zipline) you can put the following lines at the end of your strategy and use the VS Code or Theia debugger:

if __name__ == '__main__':
    strategy = my_strategy(){"DU123456": 1})

(use the name of your strategy and the name of your account ! )

Enjoy :wink: