QuantRocket logo
Disclaimer


Moonshot Intro › Part 7: Debug Moonshot Strategies


Debugging Moonshot Strategies¶

The recommended workflow for developing a Moonshot strategy is to start in a notebook, where you can develop your code interactively, then transfer the finished code to a .py file for backtesting. (This approach is described in more detail in the usage guide.) Once you've transferred your code to a .py file, you can continue to debug your strategy interactively using the technique described in this notebook. The basic technique is to open the .py file in JupyterLab and attach a console for interactive exploration.

The debugging steps are as follows:

  1. Open your Moonshot strategy file umd.py in the JupyterLab editor.
  2. Right-click in the file and select "Create Console for Editor"
  3. Select the entire contents of the file (Ctrl+A on Windows or Cmd+A on Mac), then click Ctrl+Enter to load the file contents into the console.
  4. In the Console window, instantiate your Moonshot strategy and name the variable self. Here, we are exploring the UpMinusDownDemo strategy:
self = UpMinusDownDemo()
  1. Load prices for the desired date range by calling your strategy's get_prices method (this method is defined on the Moonshot base class):
prices = self.get_prices(start_date="2017-01-01", end_date="2017-02-01")
  1. To debug prices_to_signals, select the body of the method (everything excluding the method definition at the top and the return statement at the bottom), then click Shift+Enter. This copies the selected lines to the console and executes them. Normally the prices variable is passed as a parameter to prices_to_signals, but in this case we already loaded a prices variable in the previous step, so the executed code uses that.
  2. At this point, all the local variables from the prices_to_signals method are loaded in the console and can be inspected interactively.
  3. This process can be continued to explore additional methods like signals_to_target_weights. Since both a signals variable and prices variable are now loaded in the console, you can select and execute the body of signals_to_target_weights and it will use those variables. You can then inspect the variables created in signals_to_target_weights.

See the video below for a step-by-step demonstration.

In [1]:
from IPython.display import VimeoVideo
VimeoVideo('925534381', width=700, height=394)
Out[1]:
⚠️ Due to an open issue in the current version of JupyterLab used in QuantRocket, please use Ctrl + Enter to copy code from the file editor to the console, not Shift + Enter as stated in the video.

Back to Introduction