top of page

Visualizing Stock Data in R: Candlestick Charts with quantmod

a person typing on a keyboard

What is a Candlestick Chart?

A candlestick chart is a type of financial chart used to represent the price movements of a security, currency, or derivative over a specific time-period. Each candlestick shows four key price points: the opening price, closing price, highest price, and lowest price within that period. The chart visually captures market sentiment and price trends, helping traders and investors make informed decisions.

Example of a Candlestick Chart

sample candlestick chart

Key components of a candlestick include:

  • The "real body," the rectangular section that shows the range between the opening and closing prices. A long body indicates strong buying or selling pressure, while a short body suggests indecision.

  • Shadows or wicks extending above and below the body represent the highest and lowest prices during the time frame.

  • The color of the candlestick indicates price direction: typically, green or white for bullish (closing price higher than opening), and red or black for bearish (closing price lower than opening).

Step-by-Step Guide

Load the required libraries

library(tidyverse)
library(quantmod)
library(xts)

Case 1: Using Local Data.

Load the data

stock<-read.csv("https://raw.githubusercontent.com/bernardkilonzo-rigor/dataviz/main/data/Stock%20Exchange%20Sample%20Data.csv")

Convert the date field to a date object.

stock$date <- as.Date(stock$date, format = "%d/%m/%Y")

Selecting the required columns.

new_stock<-stock%>%select(-symbol)

Converting the sample data into xts object, suitable for handling time-series data efficiently.

new_stock_xts<-xts(new_stock[,-1], order.by = new_stock$date)

Creating a candlestick chart using the code below.

chartSeries(new_stock_xts, type = "candlesticks",
theme = chartTheme("white"),up.col = "#46bd71",
dn.col = "#35366e",name = "WLTW Stock")

See the resulting viz.

example of a candlestick chart created with quantmod library

Case 2: Using Real Data

Fetch stock prices data for Google using the getSymbols() function. Period of interest -July 2024 to June 2025.

getSymbols("GOOG", src = "yahoo", from = "2024-07-01", to = "2025-06-30")

Using chartSeries() function just like in the previous example, create a candlestick chart as shown below.

chartSeries(GOOG, type = "candlesticks",
theme = chartTheme("white"),up.col = "#46bd71",
dn.col = "#35366e",name = "Google Stock")

See the resulting viz.

example of a candlestick chart created with quantmod library

Conclusion

Creating candlestick charts in R with the quantmod package offers a powerful and flexible way to visualize financial time series data. Whether you're a seasoned quantitative analyst or a curious beginner exploring market trends, quantmod provides an intuitive framework for retrieving, manipulating, and charting stock data with minimal code.

By leveraging functions like getSymbols() to import historical prices and chartSeries() to generate candlestick plots, users can quickly gain insights into market behavior, identify patterns, and support technical analysis. The ability to customize chart elements - such as overlays, indicators, and time ranges - makes quantmod a versatile tool for both exploratory data analysis and professional reporting.

In a world increasingly driven by data, mastering tools like quantmod empowers you to transform raw market information into actionable intelligence. With just a few lines of R code, you can bring clarity to complex price movements and make informed decisions grounded in visual evidence.

So, whether you're tracking your favourite stock, back testing a strategy, or building a dashboard, candlestick charts in R are a valuable addition to your analytical toolkit. Keep experimenting, keep refining, and let your charts tell the story behind the numbers.

If you like the work we do and would like to work with us, drop us an email on our contacts page and we’ll reach out!

Thank you for reading!

Tags:

 
 
Original.png

We Support You Deliver Business-Focused Solutions That Enable Data-Driven Decision Making.

  • Tableau profile
  • YouTube
  • White LinkedIn Icon
  • Facebook
  • X

QUICK LINKS

CONTACT US

WhatsApp: +254 738 307 495

East Gate Mall, Donholm

3rd Floor Suite No. 3i

Nairobi, Kenya

Join our mailing list

bottom of page