Which Is the Best Candlestick Pattern? (2)
In the previous article, I was initializing the project with several lines of code and completed several jobs:
- Imported libraries
- Downloaded financial data from Quandl
- Recognized candlestick patterns using TA-Lib
- Stored recognized candlestick patterns result
I have read through a few articles and watched several youtube videos on how to use talib to recognize candlestick patterns. But after the recognition is done, there are not many resources available on the internet on how to visualise the results so our human eyes can see. So I decide to continue exploring on this aspect and evaluating how effective are candlestick patterns.
In this second series, I am planning to visualize the financial data and display the recognized candlestick patterns. So let’s begin!
Step 1. Import mplfinance library
mplfinane focuses on visualizing financial data, it used to be the finance module within matplotlib, but now it’s an independent library. It is maintained by Daniel Goldfarb who is very actively involved in communities on GitHub and Stack Overflow, and he is of course very knowledgeable and passionate about the library. Daniel answered one of my problems posted on Stack Overflow regarding mplfinance when I just started learning. So if you’ve got any problems with the library, do not hesitate to ask on Stack Overflow, and according to my experience before, Daniel will be able to find your questions very quickly and give you a satisfactory answer as much as he can. Please remember to give him a thumb up if your question got solved!
After you installed the library with “$pip install mplfinance”, import the library as mpf.
import mplfinance as mpf
Step 2. Plot candlestick chart
As the original data is the daily data from 22nd Jan 1999 to 27th Mar 2018 which almost equals 10 years of data, if you did not slice it (use loc() function), then a “too much data warning” will pop out as below. There is nothing wrong with the function or code, it’s because the chart is so crowded that you can’t even tell those are candlesticks. For more information see: https://github.com/matplotlib/mplfinance/wiki/Plotting-Too-Much-Data
mpf.plot(df, figratio=(16,4))
Output
Step 3. Slice data to display a portion of candlesticks
The easy solution for the “too much data warning” is to take out a portion of the data to plot, here I used the loc() function to slice the data from 2017–09–01 to the end. You can slice any portion of the data as you like, as long as they can be clearly visualized.
sample = df.loc['2017-09-01':]
I used mplfinance.plot() function to display candlesticks as this method can auto-detect open, high, low, close, volume values and plot them to a series of candlesticks.
mpf.plot(sample, type='candle', figratio=(16,4))
Output
I also plotted the volume at the bottom of the candlestick chart by adding a parameter ‘volume = True’, and changing the color to red and green by specifying the style to ‘Charles’.
mpf.plot(sample, type='candle', figratio=(16,5), style='charles', volume=True, title='NVDA Candlestick Chart')
Output
Step 4. Store markers of candlestick patterns
So far we have got the data and the results of the recognized pattern stored, you cannot wait to see what those patterns really look like in the chart, right? Well, unfortunately, this is where most of the stories stopped on the internet, and you won’t be able to see the results in your own eyes! How disappointing!
That’s the reason I need to come up with a solution to visualize them. And to make the candlestick patterns easily recognizable by human eyes, I need to identify and mark them appropriately in the chart. Here I chose engulfing candles as an example to demonstrate how this should be done in general.
There are two kinds of engulfing candles: the bullish engulfing candle and the bearish engulfing candle, and Talib gives the value of 100 to the bullish engulfing bar and -100 to the bearish engulfing bar. I want to mark an up triangle 3 units away below the bottom of the bullish ones to indicate a possible uptrend, and a down triangle 3 units over the top of the bearish ones to indicate a possible downtrend. Translating this to code, I added two new columns “marker_bull_egf” and “marker_bear_egf” to store the exact position of the up and down triangles:
- value of marker_bull_egf = lowest price of the bullish engulfing candle minus 3
- value of marker_bear_egf = highest price of the bearish engulfing candle plus 3
df['marker_bull_egf'] = df[df['engulfing'] == 100]['low'] - 3
df['marker_bear_egf'] = df[df['engulfing'] == -100]['high'] + 3
df[df['engulfing'] != 0]
Output
Step 5. Plot markers on candlestick chart
mplfinance.make_addplot() function returns an object “plot”, I use one plot to store chart values of bullish engulfing bars and another plot for bearish engulfing bars. Then mplfinance.plot() function was used to plot the main candlestick chart (which is panel 0 in mplfinance), and the collection of two plots “plots_egf” was passed in as a parameter “addplot=plots_egf” to add these two series of markers on top of the main chart (panel 0).
The main chart is considered panel 0, the volume chart is usually plotted on panel 1 which is below panel 0; then move downwards are penal 2, panel 3, … until panel 9; there can only be a maximum of 10 panels. The default panel value in make_addplot() function is panel 0.
It’s time to see the real marked result! We can clearly tell there were 7 bearish engulfing candles marked with red down triangles, and 2 bullish engulfing candles marked with green up triangles. Isn’t that satisfying for your eyes and mind? I certainly felt so! And how do you think of the prediction powers of engulfing bars? Not bad! Actually not bad at all as suggested by this chart in this period for this stock, I suppose (for a person who was not expecting more than 60% winning rate for engulfing candles).
ap_bull_egf = mpf.make_addplot(sample['marker_bull_egf'], type='scatter', marker='^', markersize=100, color='green', panel=0)
ap_bear_egf = mpf.make_addplot(sample['marker_bear_egf'], type='scatter', marker='v', markersize=100, color='red', panel=0)ap0 = [ap_bull_egf, ap_bear_egf]mpf.plot(sample, type='candle', figratio=(36,17), style='yahoo', volume=True, title='NVDA Engulfing Candles', addplot=ap0)
Output
Conclusion
With another several lines of code, we certainly achieved the visualization goal that we set up at the beginning of the series. The engulfing candles were not only recognized but also clearly marked on the chart for our convenience.
I consider Step 4 to be the most important takeaway for this article as it defines the essential way of locating the positions for markers to display in the chart. I am sure you will be able to apply this principle to any patterns or charts you would like to mark and see clearly.
Do you want to further experiment with other candlestick patterns yourself? And how do your charts turn out? And what’s your opinion on the accuracies of other patterns? Please leave a comment below if you would like to share.
Please give me a clap or thumb up if you find this article useful! Thanks! See you in the next blog.