Using QuantConnect Library feature locally within VSCode (visual studio code)

Back

Just wanted to share a solution for using custom libraries added to QuantConnect locally within VSCode. Hope this helps others!

First, see this article for instructions on adding a new library to QuantConnect if you don't know how: https://backtest-rookies.com/2019/03/22/quantconnect-adding-a-library/

Next, replicate the Library folder, and corresponding libraries locally. Personally I'm using Skylight which will automatically pull in the LIbrary folder and libraries. However, you could also manually duplicate a local Library folder with corresponding libraries. When using Skylight, the Library folder will be added alongside your project folders. In my case on Mac/OSX it was added to /Users/cnaccio/QuantConnect/Charles Naccio/Library

Lastly, assuming you're using VSCode with Pylance, open your workspace or project settings file and add the following to the "settings" section:

"python.analysis.extraPaths": [
"/Users/cnaccio/QuantConnect/Charles Naccio/Library/CapitalMastery"
],
"python.autoComplete.extraPaths": [
"/Users/cnaccio/QuantConnect/Charles Naccio/Library/CapitalMastery"
]

This will help ensure your local autocomplet works, and is compatible with running backtests on QuantConnect.

Below, I'm using a BrokerageModels.py file/module from my custom "CapitalMastery" QuantConnect Library, which contains a TradeZeroBrokerageModel class

# My Projects/BigGap/main.py

# Python Packages
from QuantConnect.Orders.Fees import ConstantFeeModel
from System import DayOfWeek, TimeSpan
from datetime import datetime, timedelta
from typing import Dict, List

# QuantConnect Packages
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Algorithm.Framework import Selection
from QuantConnect.Brokerages import *
from QuantConnect.Algorithm.Framework import *
from QuantConnect.Securities import PatternDayTradingMarginModel, Security
from QuantConnect.Data.UniverseSelection import CoarseFundamental, SecurityChanges
from QuantConnect.Data.Fundamental import FineFundamental
from QuantConnect.Data.Consolidators import TradeBarConsolidator
from QuantConnect.Data.Market import TradeBar, TradeBars

# Custom Packages
from BrokerageModels import TradeZeroBrokerageModel

class BigGap(QCAlgorithm):

def Initialize(self):

# Backtest Settings
self.SetStartDate(2020, 11, 1)
self.SetEndDate(2020, 11, 18)

# Reality Modeling
self.SetCash(50000)
self.SetBrokerageModel(TradeZeroBrokerageModel())

# Configure coarse/fine universe selection
self.UniverseSettings.Resolution = Resolution.Hour
self.AddUniverse(self.CoarseSelection, self.FineSelection)

# Track symbols
self.symbols:Dict[SymbolData] = {}

Finally, here's the BrokerageModels.py file within my CapitalMastery library

# My Projects/Library/CapitalMastery/BrokerageModels.py

# QuantConnect Packages
from QuantConnect.Brokerages import DefaultBrokerageModel
from QuantConnect.Orders.Fees import ConstantFeeModel
from QuantConnect.Securities import PatternDayTradingMarginModel

# Trade Zero Brokerage Model
class TradeZeroBrokerageModel(DefaultBrokerageModel):

def GetFeeModel(self, security):
return ConstantFeeModel(0)

def GetBuyingPowerModel(self, security):
return PatternDayTradingMarginModel()

 

With everything in place, autocomplete is working as desired; see below.

118619_1606045457.jpg

Update Backtest







 
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!