QuantConnect provides a great feature that allows users to perform research and generate backtest ideas. Jupyter notebooks, known as “QuantBooks” are available and be used to interactively explore data. This article will cover how to open a “QuantBook” on Quantconnect and start playing with some OHLC data.
What is a QuantBook?
It is simply a Jupter notebook that has access to QuantConnect’s API and Data. This makes it a great tool for exploring the API and performing some analysis on historical data without having to run backtests. For more information on Jupyter notebooks see here:
How do I use one?
This is the first potential stumbling block. The official documentation does not really tell you how to launch a QuantBook. Fortunately, once you know how, it is quite easy.
Disclaimer: QuantConnect actively develop and improve their site often. This is partly why the number of step by step QuantConnect articles on this site has plateaued in recent months. They can be out of date rather quickly. It is a good thing though! They are actively improving all the time. With this in mind, the instructions below are correct at the time of writing.
Each algorithm can have one or more QuantBooks contained within it! This makes sense as you might want to perform research which is dedicated to that strategy. As you now might be able to guess, that means we find the QuantBook within the algorithm development environment!
First, open the “Algorithm Lab” from the top level menu and then select “Create Algorithm”
Next, you can build a template for the algorithm using some pre-defined code chunks. However, for the basis of this tutorial, we will just create a “basic template algorithm”.
You should now see a development environment that looks something like the following image. Notice that there is a
Files section in the left pane. Within this there is
research.ipynb section. This is it!
If you click on
research.ipynb it will load a Jupyter notebook will launch containing a basic example showing how to plot Bollinger bands over the SPY.
You can also create additional notebooks for the project by clicking on “Add New Notebook”.
Playing with the data
A QuantBook will allow you to explore the data and framework without needing to constantly run backtests. This can be invaluable if you are developing an alpha and you don’t want to increase your backtest count just look at data or run
dir() on an object.
The default example takes care of grabbing some data for us.
# QuantBook Analysis Tool
# For more information see [https://www.quantconnect.com/docs/research/overview]
qb = QuantBook()
spy = qb.AddEquity("SPY")
history = qb.History(qb.Securities.Keys, 360, Resolution.Daily)
# Indicator Analysis
bbdf = qb.Indicator(BollingerBands(30, 2), spy.Symbol, 360, Resolution.Daily)
Let’s build upon this and use the historical data to plot a candlestick chart.
mplfinance, a module which will help us to conveniently plot financial data.
import mplfinance as mpf
Next, reduce the amount of historical data to 30 days. Reducing the amount of candles will help use see the candlesticks a bit more clearly at the end of the tutorial.
history = qb.History(qb.Securities.Keys, 30, Resolution.Daily)
All data is returned as a
pandasdataframe. Pandas is very popular data analysis library that is widely used in data science. It has a lot of convenience functions for working with large datasets. For more information see:
Now we have our data, we need to extract the OHLC data for SPY out of the dataframe. That might sound unintuitive at first. However, it is because we can actually request data for multiple instruments at the same time and so the dataframe may have more than one instrument within it.
ohlc = history.loc["SPY"]
Finally, we should rename and re-arrange the order of the columns as the QuantConnect provides data in the order
volume but matplotlib would like
ohlc = ohlc[['open', 'high', 'low', 'close', 'volume']]
ohlc.columns = ['Open', 'High', 'Low', 'Close', 'Volume']
We are now ready to plot with:
And you should see something like this!
Find This Post Useful?
If this post saved you time and effort, please consider support the site! There are many ways to support us and some won’t even cost you a penny.