Linking R and Python to retrieve financial data and plot a candlestick (2024)

[This article was first published on R Programming – DataScience+, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Category

Tags

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:

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

    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.

    #pip install investpydef get_fx_cross_investpy(currency_cross,st_date,ed_date): import investpy data = investpy.get_currency_cross_historical_data(currency_cross=currency_cross, from_date=st_date, to_date=ed_date) return(data)

    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.

    install.packages("reticulate")install.packages("ggplot2")install.packages("ggthemes")install.packages("scales")

    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.

    theme_aq_black_default_font<- function (base_size = 12, base_family = "") { library(ggplot2) library(ggthemes) library(scales) col_aq2<-as.character(c("#04103b","#dd0400","#3b5171","#5777a7","#969696","#BDBDBD","#D9D9D9","#F0F0F0")) theme_hc(base_size = base_size, base_family = base_family) %+replace% 

    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.

    ggplot_candlestick<-function(df,width=0.9,chart_title,chart_subtitle){library(ggplot2) df$Date<-row.names(df) df$Date<-as.Date(df$Date,"%Y-%m-%d") df$chg df$Open, "dn", "up") cols<-as.character(c("#04103b","#dd0400","#3b5171","#5777a7","#969696","#BDBDBD","#D9D9D9","#F0F0F0")) p<- ggplot(data=df,aes(x=as.Date(Date), y=High))+ geom_linerange(aes(ymin=Low, ymax=High)) + geom_rect(aes(xmin = Date - width/2 * 0.9, xmax = Date + width/2 * 0.9, ymin = pmin(Open, Close), ymax = pmax(Open, Close), fill = df$chg)) + scale_fill_manual(values = c("up" = "darkred", "dn" = "darkgreen"))+ scale_colour_manual(values = cols)+ theme_aq_black_default_font(base_size=18)+ labs(color='')+ labs(title=chart_title,subtitle=chart_subtitle,x ="")+ labs(caption = paste0('Source: DataScience+, Investing.com ', Sys.Date()))+ guides(colour = guide_legend(nrow = 1))+ scale_x_date(labels = date_format("%y/%m"))+ theme(legend.position = "none",legend.margin=margin(-20,-20,-20,-20),legend.box.margin=margin(0,0,30,0))+ ylab("")+ theme(plot.margin=margin(l=5,r=20,b=5,t=5)) return(p)}

    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.

    # Load the python function and retrieve the financial datalibrary(reticulate)source_python("C:/Users/Fabian/Desktop/get_rates_investing.com.py")df<-get_fx_cross_investpy("USD/RUB",'01/01/2022','01/05/2022') # Use the R functions and plot the datap<-ggplot_candlestick(df,chart_title="Following its crash, the Russian Ruble rebounded sharply",chart_subtitle="USD/RUB exchange rate")p# Save the plottarget_folder<-"C:/Users/Fabian/Desktop/"ggsave(file=paste0(target_folder,"candlestick_usd_rub.svg"), plot=p, width=9, height=5)ggsave(file=paste0(target_folder,"candlestick_usd_rub.png"), plot=p, width=9, height=5)

    Related Post

    Related

    To leave a comment for the author, please follow the link and comment on their blog: R Programming – DataScience+.

    R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

    Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

    Linking R and Python to retrieve financial data and plot a candlestick (2024)

    FAQs

    What is the python library for candlestick plotting? ›

    Python provides various libraries, such as Plotly, Seaborn, and Matplotlib, that allow users to create and customize candlestick charts.

    What is the simplest way to create an interactive candlestick chart in python? ›

    In python there are 2 main ways to build a candlestick chart. The mplfinance package is built on top of matplotlib and is great to create static versions. Plotly has a specific function to create interactive candlestick charts. This page provides several examples of candlestick charts using those 2 libraries.

    How to get candlestick data? ›

    A candle has four points of data:
    1. Open – the first trade during the period specified by the candle.
    2. High – the highest traded price.
    3. Low – the lowest traded price.
    4. Close – the last trade during the period specified by the candle.
    Feb 25, 2024

    What is the candlestick code? ›

    The Candle Code indicator is used to assign a numerical value to each candle based on five candlestick characteristics, including Body Color, Body Size, Upper Shadow, Lower Shadow, and Gap. Positive candle code values represent bullish candles while negative candle code values represent bearish candles.

    What is the best Python visualization library for finance? ›

    1. Matplotlib. Matplotlib is one of the best Python visualization library for generating powerful yet simple visualization. ...
    2. Plotly. The most popular data visualization library in Python is Plotly, which delivers an interactive plot and is easily readable to beginners. ...
    3. Seaborn. ...
    4. GGplot. ...
    5. Altair. ...
    6. Bokeh. ...
    7. Pygal. ...
    8. Geoplotlib.
    Apr 24, 2024

    How do you learn candle graphs? ›

    A short upper wick on a red candle suggests the stock opened near its daily high. Conversely, a short upper wick on a green candle suggests the stock closed near its daily high. In summary, a candlestick graph presents the relationship between a stock's high, low, opening, and closing prices.

    What is the 3 candle rule? ›

    It consists of three successive candlesticks – the first is long and bearish and is followed by a smaller bullish bar that is completely engulfed by the first one. The third candle is bullish and closes above the second candle's high, suggesting a potential shift from a downtrend to an uptrend.

    What is the data for candlestick chart? ›

    Each candlestick represents one day's worth of price data about a stock through four pieces of information: the opening price, the closing price, the high price, and the low price. The color of the central rectangle (called the real body) tells investors whether the opening price or the closing price was higher.

    Which candlestick pattern is most reliable? ›

    Which Candlestick Pattern is Most Reliable? Many patterns are preferred and deemed the most reliable by different traders. Some of the most popular are: bullish/bearish engulfing lines; bullish/bearish long-legged doji; and bullish/bearish abandoned baby top and bottom.

    What is the difference between a candlestick and a box plot? ›

    While a candlestick chart has a narrowly defined range of four components (open price, close price, highest price, and lowest price), a box chart, or box and whisker plot, shows outliers, upper and lower bounds, median, and first and third quartile marks.

    Is it worth learning candlestick patterns? ›

    There are many dozens of candlestick patterns out there, but we highly discourage you from trying to remember all of them – it won't make you a better trader. Instead, learn to read price and what the way price moves tells you about what is going on in the markets.

    Do candlestick patterns really work? ›

    Yes, candlesticks work. We test 23 different candlestick patterns quantitatively with strict buy and sell signals. Perhaps surprisingly, some of the candlestick patterns work pretty well. Some of the patterns can highly likely be improved by adding one more variable.

    Is Plotly better than Matplotlib? ›

    A: Matplotlib and Plotly are Python libraries used for data visualization. Matplotlib is a popular library that is great for creating static visualizations, while Plotly is a more sophisticated tool that is better suited for creating elaborate plots more efficiently.

    What are Python cufflinks? ›

    Cufflink is also a python library that connects plotly with pandas so that we can create charts directly on data frames. It basically acts as a plugin.

    Top Articles
    Latest Posts
    Article information

    Author: Kimberely Baumbach CPA

    Last Updated:

    Views: 5826

    Rating: 4 / 5 (41 voted)

    Reviews: 88% of readers found this page helpful

    Author information

    Name: Kimberely Baumbach CPA

    Birthday: 1996-01-14

    Address: 8381 Boyce Course, Imeldachester, ND 74681

    Phone: +3571286597580

    Job: Product Banking Analyst

    Hobby: Cosplaying, Inline skating, Amateur radio, Baton twirling, Mountaineering, Flying, Archery

    Introduction: My name is Kimberely Baumbach CPA, I am a gorgeous, bright, charming, encouraging, zealous, lively, good person who loves writing and wants to share my knowledge and understanding with you.