Skip to content
All library documents

Interactive Debugging of Moonshot Trading Strategies in JupyterLab

Notebook QuantRocket

Summary

This guide explains how to inspect a Moonshot strategy interactively after moving it from a development notebook into a Python file. In JupyterLab, the user opens the strategy file, attaches a console, loads the file’s contents, creates a strategy instance, and fetches prices for a chosen date range. They can then run the body of a method such as price-to-signal conversion in the console and inspect the local variables it creates. The same process applies to later stages, including converting signals into target weights.

The document presents a practical debugging workflow rather than a trading strategy or performance study. It includes a video demonstration, but reports no backtest results or evidence about strategy profitability. It also flags a JupyterLab issue: use Ctrl+Enter to send selected editor code to the console, despite the video mentioning Shift+Enter. This technique depends on the strategy’s methods and required variables being available in the interactive session.

Key ideas

  • Load the strategy file into a JupyterLab console to explore it interactively.
  • Instantiate the strategy and fetch prices before running selected method code.
  • Inspect intermediate variables to diagnose signal and target-weight calculations.
  • Use Ctrl+Enter to send selected code from the editor to the console in the described JupyterLab version.

Tags

Full text
# Debugging Moonshot Strategies


<a href="https://www.quantrocket.com"><img alt="QuantRocket logo" src="https://www.quantrocket.com/assets/img/notebook-header-logo.png"></a><br>
<a href="https://www.quantrocket.com/disclaimer/">Disclaimer</a>

***
[Moonshot Intro](Introduction.ipynb) › 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](https://qrok.it/p/moonshot-interactive-dev).) 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](../moonshot/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:

```python
self = UpMinusDownDemo()
```

5. Load prices for the desired date range by calling your strategy's `get_prices` method (this method is defined on the `Moonshot` base class):

```python
prices = self.get_prices(start_date="2017-01-01", end_date="2017-02-01")
```

6. 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.
7. At this point, all the local variables from the `prices_to_signals` method are loaded in the console and can be inspected interactively.
8. 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.

```python
from IPython.display import VimeoVideo
VimeoVideo('925534381', width=700, height=394)
```

<div class="alert alert-warning"> ⚠️ 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.</div>

***

[Back to Introduction](Introduction.ipynb)

Shown in full with attribution under the source's licence. Licence: Apache-2.0

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.