Linking R and Python to retrieve financial data and plot a candlestick

Fabian Scheler
April 9, 2022

I am way more experienced with R than with Python and prefer to code in this language when possible. This applies, especially when it is about visualizations. Plotly and ggplot2 are fantastic packages that provide a lot of flexibility. However, every language has its limitations, and the best results stem from their efficient combination.

This week, I created the candlestick below, and I think it's an excellent case study to illustrate a few things:

  1. How to download financial data from investing.com using the investpy package in Python
  2. How to efficiently combine the capabilities of Python and R deploying the reticulate package
  3. How to construct a nicely formatted candlestick chart with ggplot2, ggthemes and two simple custom functions
  4. How to export the result in different image formats, including high-resolution Scalable Vector Graphics (SVG)

This candlestick graphic can be easily produced with data retrieval from investing.com and the ggplot2 package in R.

The Python part

Let's start with the Python code required. First, we need to install the investpy package using pip to run the simple function below. Investpy is a fantastic and very powerful wrapper around the public API of the investing.com page. It allows the retrieval of end of day price data for a wide range of financial instruments, including stocks, bonds, ETFs, mutual funds, indices, currencies, commodities and cryptocurrencies, as well as the download of selected meta-data. Detailed documentation can be found here or in pdf format under this link. Save the function defined below in a python script.

The R part

To use the previously defined Python function in R and to subsequently plot the data, we require the following four packages that can be installed easily from CRAN.

Defining a pretty theme

The ggthemes package comes with a few nice default themes for ggplot2 graphics. So you can, for instance, replicate the famous design of the Economist or the appearance of typical Stata charts. However, it is also possible to adapt these themes and create your unique default layout. I demonstrate this below for my standard formatting. The function defined here is later used in the candlestick function.

The candlestick function

Candlesticks are widely used in the visualization of price data and technical analysis. It allows viewers to quickly gauge the significance of market moves and analyze potential resistance levels or extraordinary price jumps that may be reverted in the future. To construct the daily candlestick displayed above, we require daily opening and closing prices as well as intraday highs and lows. Fortunately, this is all available on investing.com and can be retrieved as a handy data frame with our function defined above.

Plot the data and export the graphic

Last but not least, let's combine all these modules and execute them step by step. Once we have loaded our Python function employing the reticulate package, we can use it in R to retrieve the financial data from investpy. We can subsequently use our previously defined R functions to create the candlestick plot. The plot can then be exported easily as a PNG or SVG graphic utilizing ggsave.