Tutorial: Backtesting an alternative data trading strategy on LEAN

Get started with alternative data on QuantConnect’s LEAN engine, and backtest a simple strategy using WallStreetBets discussion data.

Quiver Quantitative
5 min readNov 22, 2020

Introduction: QuantConnect’s LEAN Engine is a powerful, open-source algorithmic trading engine built for easy strategy research, backtesting and live trading. They integrate with several common data providers and brokerages to make it quick and simple to deploy new algorithms.

Quiver Quantitative is the latest of these integrations, and five of Quiver’s alternative datasets are currently accessible through LEAN (with more to be added in the coming months).

In this tutorial, we’ll be walking through how to start testing alternative data strategies in LEAN. Our goal is to create and backtest a simple algorithm that goes long or short on Tesla depending on how often it was mentioned on WallStreetBets (the popular retail investing forum) the previous day.

  1. Install LEAN

To start, you will need to install QuantConnect’s LEAN engine. You can do this by following the instructions on their GitHub page, linked below:

LEAN can be installed on Windows, Mac, and Linux operating systems. In this tutorial, we are going to be running LEAN through Visual Studio on MacOS, although the steps and outcome should be the same across platforms.

2. Get a Quiver API key

You will now need to sign up for a Quiver API key. This key will enable you to access the WallStreetBets data, along with several other alternative datasets.

Sign up for the API here

The WallStreetBets dataset is available under the Trader plan. You can get a one-month free trial for this plan with promo code “QUANTCONNECT” at checkout.

After signing up, you will be able to find your authorization token on the API dashboard.

This token needs to be added to the LEAN config file. Go to the folder where you installed LEAN, and then open Launcher > Config.json. In this file, there is a variable called ‘quiver-auth-token’ where you can paste your authorization token.

Save that file, are you are now ready to download Quiver’s alternative datasets through the LEAN engine.

3. Download WallStreetBets and pricing data

LEAN makes it easy to download WallStreetBets data from the Quiver API, as well as stock pricing data from Yahoo Finance. To start, go to your LEAN folder > Launcher and open the Program.cs file.

Add these lines to the top of the file:

using QuantConnect.ToolBox.QuiverDataDownloader;
using QuantConnect.ToolBox.YahooDownloader;
using System.Collections.Generic;

and add the following lines at the start of the Main function:

IList<String> testValues = new String[] { “TSLA” };
YahooDownloaderProgram.YahooDownloader(testValues, “Daily”,new DateTime(2019, 1, 1),new DateTime(2020, 10, 1));

var x = new QuiverWallStreetBetsDataDownloader(“/PATH/Data/alternative/quiver”);
x.Run();

replace PATH with the path to your LEAN folder. The beginning of your Program.cs file should now look something like this:

You can now save and run the program, and LEAN will start by downloading TSLA’s pricing history between January 2019 and October 2020.

It will then begin using the Quiver API to download WallStreetBets data for every company included in Quiver’s universe. This should take around 20 minutes from start to finish.

The end result is distinct CSV files for each company, showing how often their stock was mentioned every day on the WallStreetBets forum.

With the pricing and WallStreetBets data ready, you can now begin backtesting.

4. Create and backtest an algorithm

For this example, we will test the theory that Tesla’s stock price is driven by retail interest, and outperforms when it’s being discussed on WallStreetBets. Our strategy will buy TSLA stock when the ticker was mentioned more than 15 times in the previous day on WSB, and take a small short position if it wasn’t.

The LEAN engine comes pre-installed with a sample WallStreetBets data algorithm. You can open it by going to your LEAN folder > Algorithm.CSharp > AltData > QuiverWallStreetBetsDataAlgorithm.cs.

Original sample algorithm

We will only need to make a few changes to this file to have it match our strategy. First, let’s change the end date to October 1, 2020:

SetEndDate(2020, 10, 1);

Next, we’ll need to gather data on TSLA instead of AAPL:

var tsla = AddEquity(“TSLA”, Resolution.Daily).Symbol;
var quiverWSBSymbol = AddData<QuiverWallStreetBets>(tsla).Symbol;

Finally, we can update the OnData function with our new strategy:

if (point.Mentions > 15)
{
SetHoldings(point.Symbol.Underlying, 1);
}
else
{
SetHoldings(point.Symbol.Underlying, -.2);
}

To use this new strategy, we have to change the ‘algorithm-type-name’ variable in the Config.cs file:

“algorithm-type-name”: “QuiverWallStreetBetsDataAlgorithm”,

You can now run Program.cs again to backtest the WallStreetBets strategy (you can remove the data download commands in Program.cs that we added earlier, as the pricing data has already been downloaded). Here are the results we received after running the backtest:

Backtest Results

The backtest shows a 1.78 Sharpe Ratio and 62% probabilistic Sharpe Ratio. Not bad for a couple dozen lines of code!

With LEAN, it’s easy to write more complex strategies, incorporate other datasets, add more stocks, and trade at different frequencies. You can also quickly connect your brokerage account and take a strategy live, once you find an algorithm that you’re confident in.

Keep an eye out of more content in the coming weeks, as we continue to explore the possibilities of alternative data strategies on LEAN.

Questions or comments? Please contact us at james@quiverquant.com

--

--