I became interested in quantitative trading largely to "scientifically" explore the relevance of many technical charting principles I've used and come across (something quite time-consuming to do manually), build a library of materially useful modules, then combine those modules to make profitable automated trading strategies.
To accomplish this I find it important to visualize what the code is doing to avoid errors and determine relevance of features, especially since many of the features I'm exploring--I or others--have identified visually through years of discretionary trading.
A popular charting platform, Tradingview, allows one to code scripts and algorithms in "Pinescript"--an easy to use but not very powerful "programming language". Complicated algorithms cannot be researched and coded here adequately, but Pinescript does make visualizing certain features quick and relatively straightforward.
I thought it would be far superior to use the included Jupyter research environment for exploratory feature research. Especially since the code for any relevant features could be straightforwardly converted into modules for use in production algorithms, far more programming power and data would be available, and documenting one's findings come naturally with the use of said tool. The combination of QuantConnect's data and backtester, Python, and Jupyter have far more flexibility and power than Tradingview.
To that end I conducted initial research into how to create plots in the research environment, both in the documentation and online.
The most relevant link to my quest on this discussion board, which describes the (now deprecated) matplotlib.finance module:
https://www.quantconnect.com/forum/discussion/4048/plot-candlestick-pattern-with-quantbook-historical-data/p1
A purusal of online tutorials revealed 2 methods to create candlestick charts in the research environment:
-Using the plotly library: https://plot.ly/python/candlestick-charts/
-Using mpl_finance and matplotlib.plplot libraries https://www.neuralnine.com/plot-candlestick-charts-in-python/
It appeared that the plotly library was limited to quick and easy plotting of candlestick charts, while the mpl_finance/matplotlib combination allowed the plotting of multiple series, thus allowing me to create seperate series highlighting the features on the candlestick chart I wanted to highlight, creating custom chart graphics, etc. This was shown to be incorrect when I came across this library in PiPy: https://pypi.org/project/chartgenerator/ , which uses Plotly to "Generate candlestick charts from json market data, automatically calculate support and resistance levels, and graph using Plot.ly" Plotly had the additional benefit of being interactive, as the GIF in this article shows: https://towardsdatascience.com/introduction-to-interactive-time-series-visualizations-with-plotly-in-python-d3219eb7a7af
I discover that mpl_finance nor Plotly was included when I looked at the QuantConnect supported libraries documentation, however, although Jared later created Git issues in order to request that mpl_finance and plotly be included in the supported libraries, so perhaps these will be viable options in the near future: https://www.quantconnect.com/docs/key-concepts/supported-libraries only to .
And attempting the following import as per the first link raised "ModuleNotFound" error, indicating mpl_finance was indeed depricated from matplotlib: >from matplotlib.finance import candlestick_ohlc
Of all the other libraries I found capable of straightforwardly creating candlestick charts, Bokeh was the only one supported at QuantConnect at the time of writing. I decided to move forward with Bokeh for the time being. (Jared later created Git issues in order to request that mpl_finance and plotly be included in the supported libraries, so perhaps these will be viable options in the near future.) And after using Bokeh it's clear the library is actually quite powerful. An official example clearly how candlestick charts can be created in Bokeh out of multiple "glyphs" -- using a line segment and conditional logic for the different colored bars: https://docs.bokeh.org/en/latest/docs/gallery/candlestick.html
The optimizer in me still had to do a comparison between the two interactive plotting libraries: apparently between the two, Plotly and Bokeh, Bokeh was more powerful while Plotly is easier to use. "While I have a personal preference for Bokeh by far (more flexible, capable, pythonic, and open), my experience at work has been that Plotly is a lot more effective in an enterprise setting (at least for the time being)" (as of 2 years ago--big improvements have probably been made to both, but my sense from looking at their respective webpages would be that Plotly is developing faster...although this is hardly scientific): https://www.reddit.com/r/Python/comments/5nwk9r/plotly_vs_bokeh_vs/
In fact, Bokeh is a fairly ideal platform for creating such charts--although seemingly behind Plotly in terms of ease of use. In any case, it outputs without issue into Jupyter notebooks given several modifications to the above example. How Bokeh does that is somewhat described here, although you may want to look at the code I share to get a better idea: https://docs.bokeh.org/en/latest/docs/user_guide/notebook.html . NOTE: the sample data described in the official candlestick tutorial will not download due to QuantConnect restrictions, and one must modify the example as follows to use QuantConnect data through the Quantbook.
If you decide to look for a good overview of how to use Bokeh, this may be a better introduction than the official docs: https://towardsdatascience.com/data-visualization-with-bokeh-in-python-part-one-getting-started-a11655a467d4
To get you started on your plotting journey I'm sharing the following demonstration project. It allows one to pass a symbol string or list of symbol strings to my plotting function, fetches the respective QuantConnect-provided-data, then pass it an analysis function which turns OHLC values from each candlestick into start and end prices that allow highlighting candlesticks or the area above/below them. The output is however many plots (arranged vertically) as there are symbols requested. All of these plots are exactly sized to what was my jupyter terminal width and initiated with the most useful tools (drag and mouse wheel zoom) already active. The code can of course be modified to do whatever you want, and should be an excellent starting point for those interested in candlestick chart plotting with Bokeh in the QC research environment. The important bits are the notebook_graphing_modules.py and the included sample notebook which imports a function from it. Just hit run all cells in the notebook for instant plots and Enjoy!
-Ian
P.S. I came across a financial plotting library maintained by Bloomberg called bqplot -- more than likely good for financial data! Not available on QuantConnect currently however and I suspect it's not interactive. However, someone may find its mention helpful: https://github.com/bloomberg/bqplot
Jared Broad
Great work Ian =) Fun to see it applied this way.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Rolf Brokmann
Thanks Ian, that's very helpful.
mplfinance is now available in QC Quantbook. Thank you QC Team
Â
Jared Broad
=) Pleasure
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Rolf Brokmann
After the latest refactorings in quantbook's pandas API, it is now easy to plot a candle chart with mplfinance:
#Lean Version: v8296 Hash: 59ed3f58e9 Released: 2020-05-20 01:02:10 import mplfinance as mpf qb = QuantBook() spy = qb.AddEquity("SPY") history = qb.History(qb.Securities.Keys, 360, Resolution.Daily) history.columns=['Close', 'High', 'Low', 'Open', 'Volume'] history.index = history.index.droplevel() mpf.plot(history,type='candle',volume=True,show_nontrading=True, figratio=(16,10), style='charles',mav=(200))
Â
Ian Larson
Thx for posting your code Rolf! Short and sweet!
Noticed Jared and the QC team have added Plotly to the libraries mix as well. Excited to play with this one, seems easier to use than Bokeh: https://plotly.com/python/candlestick-charts/
the real power of Plotly or Bokeh is the potential to add additional information (such as buys/sells/exits or highlighting features/patterns on the chart, in the research environment.
Ian Larson
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!