Open Source

Debugging Python in Visual Studio

Debugging Python in Visual Studio

Introduction

We often want to use an IDE for algorithm development because it provides comprehensive facilities such as a source code editor and a debugger. QuantConnect delivers a robust online source code editor, but not a debugger. For some algorithm developers, a debugger is an essential tool; therefore they opt to work offline with full-featured IDE.

Visual Studio is a full-featured IDE that makes debugging easy. Unlike other solutions that only allows debugging a single process/language, we can use it to debug a python algorithm in Lean (C# engine).

In this tutorial, we'll show you how to debug your algorithm in LEAN from Visual Studio.

Prerequisites

Python Tools for Visual Studio debug (ptvsd) server python library. It can be easily installed using pip:

pip install ptvsd

Installing LEAN with Python

The first step is to install LEAN and get it running with python algorithms.

To do this install:

  • Download LEAN from Github.
  • Install Visual Studio.
  • Open QuantConnect.Lean.sln in Visual Studio.
  • Build the solution by clicking Build Menu -> Build Solution.
  • Press F5 to run LEAN.

By default, Lean will run the C# version of BasicTemplateAlgorithm. From here the next step is to run the python version of the BasicTemplateAlgorithm successfully.

To run that algorithm:

  • Edit config.json with the following paramenters:
  •       "algorithm-type-name": "BasicTemplateAlgorithm",
          "algorithm-language": "Python",
          "algorithm-location": "../../../Algorithm.Python/BasicTemplateAlgorithm.py",
      
  • Build the solution by clicking Build Menu -> Build Solution
  • Press F5 to run

Attaching the Debugger

The process that we will use to attach the python debugger requires that we run Lean without debugging and attach the process.

First, we will add the ptvsd library, and the following statements:

import ptvsd
ptvsd.enable_attach()
ptvsd.wait_for_attach()

These statements will hold the algorithm execution until Lean is attached to the python debugger.

The following statement sets the breakpoint:

ptvsd.break_into_debugger()

For example, place it in the OnData() method:

import ptvsd
ptvsd.enable_attach()
print(f'''Python Tool for Visual Studio Debugger {ptvsd.__version__}
Please attach the python debugger:
- In Visual Studio, select Debug > Attach to Process (or press Ctrl+Alt+P) to open the Attach to Process dialog box.
- For Connection type, select Python remote (ptvsd)
- In the Connection target box, select tcp://localhost:5678/ and click "Attach" button''')
ptvsd.wait_for_attach()

class BasicTemplateAlgorithm(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2013,10, 7)
        self.SetEndDate(2013,10,11)
        self.SetCash(100000)
        self.AddEquity("SPY", Resolution.Second)
        self.Debug(f"numpy test >>> print numpy.pi: {np.pi}")

    def OnData(self, data):
        ptvsd.break_into_debugger()

        close = data["SPY"].Close
        if not self.Portfolio.Invested:
            self.SetHoldings("SPY", 1)

Finally, attach the debugger to the process:

  • Run Lean without debugging by clicking on Debug -> Start Without Debugging
  • In Visual Studio, select Debug -> Attach to Process (or press Ctrl+Alt+P) to open the Attach to Process dialog box.
  • For Connection type, select Python remote (ptvsd)
  • In the Connection target box, select tcp://localhost:5678/ and click "Attach" button

Once you attach the process the code execution will stop after ptvsd.break_into_debugger() statement.

Limitations

Unfortunately, this debugging scenario entails some limitations.

It is not possible to concurrently debug the python algorithm and Lean (C# code). That means that the debugger will not stop at breakpoints in Lean nor we can step into methods defined in Lean (e.g., SetHoldings).

The algorithm will always stop after ptvsd.break_into_debugger() call and it not possible to untoggled it. However, we can use conditional statements to prevent its call and avoid unnecessary breaks. In order to resume the algorithm execution without breakpoints, we need to detach the process (Debug -> Detach All).

Summary

In this tutorial, we have learned how to debug python algorithms in Visual Studio. It is not quite as smooth as native python yet, but knowing how to debug python algorithms in LEAN can give you a new level of understanding of how they work and allow for easier debugging of your strategy.

You can also see our Documentation and Videos. You can also get in touch with us via Chat.

Did you find this page Helpful ?