Hi all,

With the shut down of Quantiopian a lot of new users are coming to QuantConnect. I thought it would be helpful to make a guide compiling and detailing some of the simple concepts of QuantConnect I struggled with most when first joining. Note that all of the topics discussed here are already heavily documented by QuantConnect. This guide is written in C# however concepts can also be applied to Python. If you have any questions feel free to post a comment and I will respond!

 

Initialize() - The Constructor and Main Method

public override void Initialize() {}

Initialize is a combination of both a constructor and main method on QuantConnect. It is always ran once at the start of each backtest. Since there is no main method in QuantConnect, it is treated as such. It is often used to initiantiate universes and define variables like indicators. You also define backtest parameters in here such as:

// starting cash for backtest
SetCash(int amount);
// starting date for backtest
SetStartDate(int year, int month, int day);
// end date for backtest (optional)
SetEndDate(int year, int month, int day)

This is where you also define your universe. You can either do this manually or automatically and will be covered more in the Adding Data section.

 

AddingData - How to Get Data 

Adding data to your universe is done through two seperate processes: manually and automatically. First we will cover manual adding. To add data manually you use one of the five methods depending on the type of data you are looking to access:

 

// note resolution is defined as:
// Resolution.Tick, Resolution.Second, Resolution.Minute, Resolution.Hour, Resolution.Daily
AddEquity(string symbol, Resolution resolution);
AddFutures(string symbol, Resolution resolution);
AddOption(string symbol, Resolution resolution);
AddForex(string symbol, Resolution resolution, Market market);
AddCrypto(string symbol, Resolution resolution, Market market);

 

Note that there are other parameters that can be included for each function however this guide is for the basics. You can look at QuantConnect's documentation for more information. These functions are typically called within the Initialize() method mentioned prior. Next is adding data using an automatic universe. This is done through calling:

AddUniverse(CoarseFilterFunction, FineSelectionFunction);

within the Initialize() method. You then define the two methods as follows:

public IEnumerable<Symbol> CoarseFilterFunction(IEnumerable<CoarseFundamental> coarse) {
// returns the highest DollarVolume stocks
// returns "totalNumberOfStocks" amount of stocks
return (from stock in coarse
orderby stock.DollarVolume descending
select stock.Symbol).Take(totalNumberOfStocks);
return Universe.Unchanged;
}

public IEnumerable<Symbol> FineSelectionFunction(IEnumerable<FineFundamental> fine) {
return (from f in fine
select f.Symbol).Take(totalNumberOfStocks);
}

To see these two samples used, check the example program at the bottom.

 

OnData(Slice data) - The Active Function 

OnData is a function ran whenever there is a change in data. This is mainly based off of the resolution of your universe. For example, if you have your data resolution set to Hourly, it will run once an hour on the hour. You can then access data from within the Slice as:

// using the Slice parameter
decimal close = data[<string ticker>].Close
// a safer and more stable way directly accessing it from the universe
decimal close = Securities[<string ticker>].Close

 

Entering Positions 

There are several ways to enter positions using QuantConnect. The two primary ways are using the functions:

Order(string ticker, int quantity);
SetHoldings(string ticker, decimal percentage)

Order is used to place a position with a defined ticker and number of contracts. SetHoldings allocates a percentage of your portfolio to a specific ticker. To place a put positition, simply make the quantity negative. i.e. to place 10 sell contracts for SPY you would do:

Order("SPY", -10);

 

Thank you for reading the guide and I hope it was helpful! If you have any questions feel free to post them below and I would be happy to answer them. Also provided below is a template I use for a lot of my code which show a sample program in which it enters the top 20 high dollar volume stocks hourly when the EMA10 < EMA20. I built it modularly so it should be easy to modify.

 

Author