What is Blueshift’s Visual Programming?
There are a lot of people who have a lot of trading experience, but they are not adept in computer programming. This adds a significant entry barrier for a person to take part in algorithmic trading. Blueshift has recently added a feature called visual programming . This feature enables you to create strategies without any knowledge of coding . That’s right, you can create the strategy by only drag and drop the strategy building blocks!
You can check out the 7-part youtube series on Blueshift’s Visual Programming. The series explains the user interface and different features of the platform. It also constructs a simple crossover strategy.
Machine Learning Pipeline in Blueshift’s Visual Programming
Visual Programming is not merely an easy to use interface for defining a set of simple trading rules. Visual Programming is an almost complete programming language capable of handling any logic.
This means you can also build complex strategies such as machine learning models. You can use Blueshift to backtest or live trade a machine learning strategy as easily as you can backtest a simple crossover strategy. If you are not familiar with machine learning, you can do the free course Introduction to Machine Learning for Trading on Quantra!
Underneath the easy to use interface, lies an intelligent software engine which validates and converts the visual blocks into python code. The machine learning pipeline in Blueshift’s visual programming follows the sklearn pipeline flow .
Don’t worry if you are not familiar with the pipeline terminology! A pipeline is simply a set of transformers and estimators applied to a data set. In other words, a sequence of mathematical operations is applied to transform the data appropriately. Then, the estimator essentially tries to find patterns in the data. This pattern finding is also called “ model fitting ”. The measure of how well the model is “fit” over new data is called “ score ”. The “score” gives insight into how accurately the model can predict any unseen data.
The last estimator in the machine learning pipeline only needs to implement the “fit” function and get the results. The score calls the underlying “score” function of the estimator.
The following diagram gives an idea of how the machine learning model is built and used, along with the relevant Blueshift visual programming blocks alongside. The model is built by adding the features, target and estimators. Ideally, the features should be uncorrelated amongst themselves and be weakly predictive. The target is the dataset which is or should be the machine learning output.
The estimator is chosen based on the problem statement at hand. For example, if the final output is the predicted price, then the regression estimator is used. But if the final output is a classification (to maintain a long position or to maintain no position) then the classifier estimator is used.
Note : the target can be both for regression (no levels required) and classification (requires levels).
Building a Machine Learning Strategy
In this article, you will learn how to construct a long-only machine learning model using the Random Forest Classification estimator. That means, the strategy will only take long positions and the underlying machine learning estimator is the random forest classifier .
The key backtesting results which you can expect from the strategy (backtest starting from January 2015 till November 2020) you build here are as follows:
Metric | Value |
Annual Returns | 10.98% |
Cumulative Returns | 83.58% |
Annual Volatility | 9.96% |
Sharpe Ratio | 1.09 |
Maximum Drawdown | -10.60% |
Note : The backtesting results will vary slightly each time you run the model. This difference is due to the inherent randomness in the random forest algorithm used by the model to fit the data.
The strategy is constructed by following these steps:
Strategy Settings
The initial strategy block looks as follows:Make sure to select the dataset as NYSE-DAILY as you will be designing the strategy based on daily NYSE data. As you are making a long-only strategy, set the long-only option to “True” .
Define the Universe
Connect the “set universe to” and the “select assets” blocks to the “Define Universe” step. For this sample strategy, you can choose these stocks: AMZN, AAPL, WMT, MU, BAC, KO, BA, AXP.
Define the Alpha (Machine Learning Model)
The alpha of your strategy will be the prediction output from the machine learning model. So you need to define the machine learning model as your strategy alpha . To refresh your memory about alpha and variables, you can refer to the Variables And Alpha Functions video.Create a model using the “Use model” in the Machine Learning menu on the left. You can name the model as “model_1”.
Connect the “model_1” block to the “Define alpha” step.
To define the model, you need to specify these three things:
- Features
- Target
- Estimator
Features
The features available in Blueshift Visual Programming are as follows:
- Technical : crossovers, MFI, RSI, Fibonacci and more
- Statistical : kurtosis, var, mean, median and more
- Price action features : doji, engulfing, gap-up/down, and more
You can use any combination of these features and analyse the performance. It is very easy to add features to your model. Just connect the relevant block to the “Add Features” and click on select features.
A menu shows which features are available and which ones are currently selected.
The features used in this sample strategy can be seen by loading the pre-built strategy at the end of this section .
Target
The target maps price returns performance (based on close-to-close price) automatically to the estimator target (classification or regression output). The inputs required for defining a target are:
- The number of levels of encoding : In the sample classification strategy, you can either “maintain a long position” or “maintain no position”. Thus the level of encoding will be two. If the estimator is of regression type, then the number of levels will be zero.
- Lookback period : The lookback period for return calculation.
- Method of measuring return performance : To use percentage change or difference when calculating the returns. For the sample strategy, choose the “pct_change” option.
Estimator
In addition to pre-processing and feature selection, you can use the machine learning pipeline in Blueshift Visual Programming for dimensionality reduction. In the end, you add an estimator to obtain the model output.
The available options are:
- Feature Selection : to select the k-best features
- Dimensionality Reduction :
a) PCA
b) Linear kernel PCA
c) rbf kernel PCA - Estimator and the rolling estimation window :
a) Classification: Logit, ridge, random forest, xgboost, etc
b) Regression: OLS, lasso, ridge, random forest, etc
For your strategy, select the PCA dimensionality reduction to 25 components . Select the classification estimator block and the estimator as “ rf ” (random forest) with a rolling estimation window of 500 periods (approximately 2 years on daily frequency data).
Blueshift By QuantInsti and the Alpaca API: Tools for Algorithmic Trading
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.
Future Enhancements to the Strategy
The strategy you learnt here implements a random forest classifier based on several features. You can play around with a different combination of input features to see if you improve the performance. As shown earlier, the feature selection is possible from the easy to use graphical interface. You just need to connect the relevant block to the “Add Features” and select from the menu.
You can also change the strategy hyperparameters like lookback window and number of estimators. The machine learning model will perform differently for different assets in your trade universe. You can change the tradable assets and analyse the performance.
You can add complex risk management trading rules with different stop loss and take profit criteria. Feel free to tweak this strategy to suit your needs!
You can refer to the visual programming tutorial series if you need to recall how to use the interface or any of the blocks.
Leave a Reply