Blueshift By QuantInsti and the Alpaca API: Tools for Algorithmic Trading
How to backtest, optimize, and automate your trading strategy using Blueshift integrated with Alpaca’s API.
Co-authored with Isaac Rhea
Algorithmic trading is becoming more accessible with cheaper computational resources and increasingly widespread knowledge of computer programming. Anyone that has a desire to automate their trading strategies can now do so at a marginal cost. This article focuses on a variety of tools that are continuously making the process to write, optimize and automate your trading strategy fairly easy. More particularly, the focus will be on two tools — Blueshift by QuantInsti and the Alpaca API for stock trading that is commission free. Prior to exploring these tools, a little background will prove to be beneficial in understanding the potential of these tools.
The Problem
Quantopian has become one of the most popular algorithmic trading tools used for learning and practicing algorithms. The platform enables users to write and backtest their algorithms using a variety of APIs. The main API, on which the whole platform is built, is a popular trading pythonic package called Zipline. They initially made live trading available by scheduling trades through the Robinhood API, which has since been terminated. Users are able to fine tune their algorithms and once they have maximized profits, users can enter their algorithms into competitions and seek sponsorship for funds. For those who are writing algorithms solely for the purpose of obtaining sponsors and funds, this is an excellent platform; however, for those that are wanting to optimize their algorithms for live trading and automation of such algorithms, this is not the ideal tool for writing and backtesting your algorithms. The reason being that all strategies written in Quantopian must be transitioned to conform with whatever trading API used to live trade. This process can be time consuming and inconvenient. However, the backtest environment created by Quantopian is incredibly useful and makes all the difference when trying to optimize your algorithm.
The Alpaca API has been developed to cater to algorithmic traders and makes live trading as well as paper trading incredibly easy. However, their API does not have any backtesting modules, so that leaves users to find other backtesting libraries or to write their own. So, ideally you would want a platform that allows users to backtest their algos and then have the capability to make their algo go live using the Alpaca API. This is where Blueshift by QuantInsti enters the picture.
The Solution
The Blueshift platform is very similar to Quantopian and the basics of strategy backtesting remain the same. It uses the Zipline package to conduct backtests, reports similar metrics, and is easy to use. For example, I have run a quick backtest of a buy and hold strategy template from Blueshift. The portfolio included stocks like Amazon, Apple, and Boeing and rebalances at the beginning of each month.
The returns of the strategy are shown along with a market benchmark to measure performance. The graph of returns shows that my portfolio’s performance closely followed the broader market. Beta is a statistic used to analyze this correlation and relate portfolio returns with the returns of the market and gives insights to the strategies expected risk. In this case, a Beta of 0.75 indicates that we expect the strategy to return 0.75% for every 1% of returns of the market portfolio (perhaps measured by the SPY ETF). At the same time, we only expect the strategy to lose -0.75% if the market declines by 1%. Thus, the risk of this portfolio is slightly lower than that of the market. Using this and other metrics users can optimize a trading strategy. Watch for our upcoming article for a more in-depth discussion of backtest metrics.
If you have any experience researching trading strategies through backtests this probably sounds very familiar. But the key benefit of Blueshift has to do with live trading. With just a few clicks, users can link an Alpaca brokerage account to a Blueshift algorithm and begin live trading. Eureka! The problem is solved.
To transition a backtest to live trading, you should have previously created an account with Alpaca. This can be a live trading or paper trading account. If you have that, click ‘Go Live’ in the top right corner of the screen. Blueshift will automatically convert your python code for the Zipline module to orders through Alpaca. In some cases, small adjustments to the algorithm are necessary to go live and Blueshift will handle those. Then, simply select Alpaca as the brokerage account and allow Blueshift to connect to Alpaca.
Now that you understand the context of why Blueshift is such a powerful tool, below is a step-by-step guide to setting up Blueshift.
Step-by-Step guide to setting up Blueshift
Step 1. Sign up for Blueshift
Blueshift is free and really easy to use. Here is the link to their main page where you can sign up for an account.
Once there, navigate to “Sign Up” on the top right of the page. You can create an account via this page or you can Oauth with Google or QuantInsti. Once logged in you will be taken to the home page that looks like this.
This area is where users are able to organize all of their different algorithms/strategies. Conveniently, Blueshift has already uploaded some examples for Forex (Foreign Exchange) and Equity strategies. These are great ways to get started.
Step 2. Create Strategy
By clicking on the “Create Strategy” button on the top right, you can get started with your own algorithm. It will prompt you to name your strategy and select a Strategy Template. The template that you select will have some starter code, but you are not required to keep the starter code.
Step 3. Write and Optimize Strategy
Upon selecting “Create Strategy” you will be taken to a new screen where you can write your algorithm on the left and perform a variety of different actions on the right. This is where you spend the majority of your time as you tweak your algorithm to optimize returns and other metrics.
Step 4. Run Quick Backtest/Full Backtest
On the right panel, you can enter in the dataset you would like to use for the backtest, the beginning date and ending date, and the capital amount for the algorithm. To run the quick backtest, select run.
Certain metrics are provided via the Blueshift platform when running the quick backtest. For the purpose of this article, we will not dive deep into explaining what those metrics mean, but feel free to check out this article here for help.
Logs on the bottom will print/display anything that you tell your code to return. Errors will inform you of any complications/status of your algo. The Description tab allows you to add more detail if desired.
Blueshift offers a more in-depth backtest by clicking on “New Backtest”, the Orange button. This backtest takes a little more time and the notification center at the top right hand corner is used to notify the user of the status of the backtest. Blueshift also automatically names the backtest for you. Their naming conventions are pretty interesting. Click on the notification to access the full backtest.
The full backtest provides quite a bit more information than the quick backtest. It produces Key Metrics, a basic tear sheet, transactions, error logs, output logs and the code of the algo.
The full backtest is one of the major benefits of using Blueshift or Quantopian (they offer the same full backtest) because it provides huge amounts of information that allows users to maximize returns prior to going live with their strategies.
Step 5. Go Live
This is the best part about Blueshift. Typically, you would spend a lot of time transitioning your code from where you were backtesting it, to where you are able to actually trade with it. However, with Blueshift you just click a few buttons. Start by clicking the “Go Live” button on the page where you can edit your strategy and run the quick backtest. Once you select this button, Blueshift will prompt the user with the following information.
This helps the user understand what is going on behind the scenes to make your code ready to live trade. After you select Accept and Execute you will be directed to Configure your settings for Live Trading. By selecting “Go to Settings” you will be able to select a few different options.
By selecting “Go to Settings” you will be able to select a few different options.
Pay close attention to these settings, as they will protect you from risky positions.
The next step is to define algorithm parameters such as the broker account, the amount of capital and the date you would like the strategy to end. You are able to select either a live trading account or a paper trading account. Once you select either or you will be redirected to OAuth. This connects the two platforms together, with your permission.
This is Alpaca’s disclaimer for integrating with Blueshift.
If everything connects properly, you will see your account in the broker details on the right. The last portion of this step is to set the amount of capital you are wanting to trade and the end date of the algorithm.
Blueshift will only run algorithms automatically for one trading week. So, if you were to go live with an algorithm on a Tuesday, it would stop running on Friday.
For paper trading, be sure to allocate the same amount of capital to match with Blueshift, it will not automatically set the capital in Alpaca. Do this before you connect your broker, because when you set a new capital amount in Alpaca it resets your API Keys.
This gives you an idea of what was explained above.
The terms and conditions will need to be accepted.
A quick review, to ensure that you have everything correct.
Select confirm to automate your fully backtested and optimized algorithm. You will be directed to the trading console where the logs are outputted. Blueshift also offers a Dashboard and Trade modules for further analysis and metrics.
The Dashboard graphs the current net value on the left and on the right displays a variety of metrics.
One interesting current characteristic of Blueshift is that automated algorithms are named automatically, there is no way to edit or customize your algorithms once they have gone live. For example, this live algorithm was given the name, “Yellowringer Runner”
The Trades module provides a list of all orders created by the algorithm. It will display that it is listening for trades if no trades have been made. Once trades have been made, you will see the ticker, the number of stocks made and the value of the position.
If you navigate to the Overview button at the top, this provides a snapshot of your status if you have multiple algorithms operating at once.
My Algo will display all active algorithms with the functionality to view inactive algos as well.
To navigate back to the strategies home to write, backtest and optimize more algorithms, click your profile avatar at the top right and select strategies. If you are wanting to navigate back. To access your live algorithms, Blueshift currently does not have a link on there page to jump back to the algorithm sections. The best and most efficient way is to change the URL from research to live/dashboard.
Note: if you are logged out and you try and access these links, you will be asked to log back in.
https://blueshift.quantinsti.com/research/strategies/
https://blueshift.quantinsti.com/live/dashboard/
Blueshift Limitations and Important Notes
Blueshift’s capability to automatically start live trading based on a backtest algorithm is awesome. However, there are a few limitations to be aware of. First, you can only backtest trading strategies for the S&P 500 or the top 500 stocks on the NASDAQ exchange. If you want to trade other securities you will have to go back to the traditional approach. If you are comfortable coding your own backtests, Alpaca users have access to tons of data through their brokerage account.
Another limitation is that Blueshift stops all active algorithms after the market closes on Fridays. This is likely to prevent users from starting an algorithm and forgetting about it, and it means that you have to log on again and restart it the next week. Finally, there are a few issues in the website design that make it slightly inconvenient to navigate the platform but these should be ironed out with improvements soon. It’s not perfect, but Blueshift by QuantInsti is a hidden gem and a powerful new tool for algorithmic trading.
For those looking for a fully automated solution, check out this article that shows how to set up an algorithmic trading bot in 7 steps.
Conclusion
Blueshift provides a powerful solution to transitioning backtest code that leverages the zipline API into a live trading algorithm in minutes. This is made possible by stock trading APIs like the Alpaca API.
With these tools, algo traders will be empowered to write, test, optimize and automate their trading strategies.
Technology and services are offered by AlpacaDB, Inc. Brokerage services are provided by Alpaca Securities LLC (alpaca.markets), member FINRA/SIPC. Alpaca Securities LLC is a wholly-owned subsidiary of AlpacaDB, Inc.