These commands return the alpha along with tearsheets which are of interest:
Generating simple alpha factors using TA-lib
Now that we have a baseline understanding of what we can take from alphalens, let’s explore creating a factor for analysis using TA-lib. Technical factors from TA-lib are probably the simplest factors to generate. For this demonstration, we will create a technical indicator and then analyze it using alphalens.
The relative strength index (RSI) indicator
The RSI is an indicator used in technical analysis to quantify the degree to which an asset is being bought with respect to time. Put simply, it can allow us to see whether a certain asset is being bought or sold to abnormal degrees. We can use it to predict the momentum of an asset, making it a perfect potential candidate to better analyze how alphalens judges alpha.
Using the exact process as in the earlier example all I’ve changed is the technical indicator as follows:
TA-lib allows the user to calculate a wide variety of technical indicators with ease allowing for quick inquiry into the merits and demerits of different potential features. The RSI indicator returned these results:
Not great but at least they are positive. The essence of feature engineering is searching for potential signals that can return high alpha through patience, careful analysis, and robust testing. Let’s go through one more relevant strategy in the realm of feature engineering.
Wavelets
A wavelet is a mathematical concept that is often used in statistics, data science, and machine learning to represent signal data with respect to space or time. Since using something like a Fourier transform would not take into account the temporal aspect of time series, it is useful to use a wavelet when analyzing markets and signals to allow the representation of this data with functions and allow certain transformations such as smoothing of erratic results.
Using the PyWavelets library, we can use the wavelet translate tool to effectively smooth data, reducing its erratic nature. A simple implementation of this smoothing looks like this in code.
“pywt” is used to reference the pywavelets library. Here, after the creation of the RSI indicator data for analysis in alphalens, I created the signal “signal_apple” and transformed it into the reconstructed “recon_signal_apple” signal resulting in the following graph.
We can clearly see that the wavelet transform resulted in a much less erratic series. This allows us to make more reliable predictions that are less prone to the market’s whiplash and more considerate of the broader market context. Applying this to the set of stocks, let’s see what effect this has on our alpha.
As you can see, these values are much stronger than those obtained purely from the RSI. If we tweaked the threshold value of the wavelet transform further we might be able to get even more useful data that might yield even better data.
Hopefully from this basis, you have determined a decent baseline understanding of what feature engineering might look like, what it means to engineer features, and why it is useful to perform transformation on data to get a better understanding of whether it might be useful and in what ways.
For further reading on the subject, consider reading through chapter 4 of the GitHub repository “Machine Learning for Trading” by Stefan Jansen, from which much of the information in this article was synthesized. Additionally, consider reading the books:
- Dissecting Anomalies by Eugene Fama and Ken French
- Feature Engineering for Machine Learning: Principles and Techniques for Data Scientists by Alice Zheng and Amanda Casari
And for further reading on machine learning in finance as a broader subject:
- Machine Learning in Finance: From Theory to Practice by Matthew F. Dixon, Igor Halperin, and Paul Bilokon
Stay tuned for the next article in this series which will cover strategy evaluation and backtesting.