Technical analysis is a method of forecasting the direction of price through the study of past market data, volume and price being the market data we are interested in. Quantitative analysis and behavioral economics use many of the tools covered under technical analysis, showing that it has applications which are appreciated by a variety of market practitioners. If you are interested in the history of technical analysis along with comprehensive coverage regarding the concept behind this topic, I recommend you go through A Brief Overview of Technical Analysis, an article which is available on this blog.
Implementation of Technical Analysis
Having already discussed the history of technical analysis in the article mentioned above, we will be discussing some of the use cases of technical analysis in this one, the use case being: plotting some of the well known indicators in order to see how they can aid us in our analysis of a particular security, and making use of technical analysis indicators in a trading strategy. Having discussed the agenda, lets get to it!
Plotting Technical Indicators
Simple Moving Average
A simple moving average(SMA) calculates the average of a selected range of closing prices, by the number of periods in that range. The code and plot of the SMA indicator applied to Apple's stock is shown below.
![](https://static.wixstatic.com/media/2be87c_71953c0d867e4284b13515c1292f4f70~mv2.png/v1/fill/w_876,h_743,al_c,q_90,enc_auto/2be87c_71953c0d867e4284b13515c1292f4f70~mv2.png)
Exponential Moving Average
An exponential moving average places greater weight and significance on the most recent data points, thus making it more biased to the most recent prices of the relevant data. The code and plot are shown below.
![](https://static.wixstatic.com/media/2be87c_8ddfad40d10341289e1604d93578c612~mv2.png/v1/fill/w_902,h_749,al_c,q_90,enc_auto/2be87c_8ddfad40d10341289e1604d93578c612~mv2.png)
Bollinger Bands
Bollinger Bands are a type of statistical chart characterizing the prices and volatility of a financial instrument over a particular time period, using a formula proposed by John Bollinger, a CFA charter holder. The code and plot are shown below.
![](https://static.wixstatic.com/media/2be87c_5a88a5ba39804cfc85a382dbd3ed6372~mv2.png/v1/fill/w_980,h_712,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/2be87c_5a88a5ba39804cfc85a382dbd3ed6372~mv2.png)
Relative Strength Index
The relative strength index is a technical indicator that is intended to chart the current and historical strength of weakness of a security based on the closing prices of a recent trading period. The code and plot are shown below.
![](https://static.wixstatic.com/media/2be87c_d411db7505da405ea97c90981dbb3682~mv2.png/v1/fill/w_892,h_749,al_c,q_90,enc_auto/2be87c_d411db7505da405ea97c90981dbb3682~mv2.png)
The fact that we only discussed 4 technical indicators should not deter you as a reader from experimenting with some of the more exotic technical indicators, especially considering how simple the TA-lib makes it apply technical indicators to a security. For our purposes though, four are enough. Now, we move on to applying technical analysis to a trading strategy.
A Trading Strategy and Technical Analysis
Having coded and visualized a few technical indicators, it now time to apply that knowledge to and create a trading strategy, as we've done in posts like Exploring Quantamental Investing with Python and Quantopian, among others. An important point to mention about this strategy will not be as thorough because it was not done on Quantopian, the reason being that Quantopian no longer offers that functionality. This is a tragic loss for all quants(aspiring and flourishing) worldwide. In spite of this, we will make best of what is available.
The proposed strategy makes use of two technical indicators ,Bollinger Bands and RSI, as a voting system to decide whether to buy or sell a security when the correct parameters have been satisfied. When written out in code, this is what the voting system looks like.
![](https://static.wixstatic.com/media/2be87c_710ce09543bd4875a6b80b523b4b8c1b~mv2.png/v1/fill/w_980,h_99,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/2be87c_710ce09543bd4875a6b80b523b4b8c1b~mv2.png)
The above code shows why python is superb for algorithmic trading purposes: its concise and understandable! Also, its pretty easy to change the voting system by making use of more or less technical indicators than the ones mentioned, depending on what you would like achieve. The code used in this strategy will be uploaded to my GitHub soon after this post.
When applying the voting system to Coca-Cola stock from 2015-02-24 to 2021-02-23 , we get the following results, presented through a matplotlib plot. The plot shows the trades made, superimposed on the stock price via green and red dots. Thereafter the plots for RSI and Bollinger Bands are given.
![](https://static.wixstatic.com/media/2be87c_4213f9cc8f22469288ca19032b065680~mv2.png/v1/fill/w_907,h_804,al_c,q_90,enc_auto/2be87c_4213f9cc8f22469288ca19032b065680~mv2.png)
When applying the voting system to IBM stock from 1990-02-26 to 2021-01-25, we get the following results, presented through a matplotlib plot. The plot shows the trades made, superimposed on the stock price via green and red dots. Thereafter the plots for RSI and Bollinger Bands are given.
![](https://static.wixstatic.com/media/2be87c_1ddaae334da642d7828c7412ea5f1006~mv2.png/v1/fill/w_903,h_793,al_c,q_90,enc_auto/2be87c_1ddaae334da642d7828c7412ea5f1006~mv2.png)
Conclusion
Its safe to say that technical analysis can be used to create profitable trading strategies. Its unfortunate to not be able to backtest this strategy through Quantopian, but all in all, this blog post has achieved its intended purpose. Complement this article with the following video.
References
Trading Strategy: Technical Analysis with Python TA-Lib, towardsdatascience.com
Technical Analysis of Stocks using TA-Lib, towardsdatascience.com
Technical Analysis, wikipedia.com
Comments