How to plot candlestick chart using plotly in R?

This recipe helps you plot candlestick chart using plotly in R

Recipe Objective

A Candlestick plot is a type of price chart that originated in Japan over 100 years before bar charts were developed. It is used to analyse the low, high, close and opening prices of a security for a particular period. They are mainly used by traders to analyse price variation based on historic patterns. ​

In this recipe we are going to use Plotly package to plot the required candlestick plot. Plotly package provides an interface to the plotly javascript library allowing us to create interactive web-based graphics entrirely in R. Plots created by plotly works in multiple format such as: ​

  1. R Markdown Documents
  2. Shiny apps - deploying on the web
  3. Windows viewer

Plotly has been actively developed and supported by it's community. ​

This recipe demonstrates how to plot a candlestick plot in R using plotly package. ​

STEP 1: Loading required library and dataset

Dataset description: Stock prices of Amazon from Yahoo

# Data manipulation package library(tidyverse) # plotly package for data visualisation install.packages("plotly") library(plotly) install.packages("quantmod") library(quantmod) # reading amazon's stock prices from yahoo and stroing it in a dataframe getSymbols("AMZN", src='yahoo') df = data.frame(Date=index(AMZN),coredata(AMZN)) df = tail(df, 20) glimpse(df)

'AMZN'
Observations: 20
Variables: 7
$ Date           2020-12-07, 2020-12-08, 2020-12-09, 2020-12-10, 2020-1…
$ AMZN.Open      3156.48, 3158.90, 3167.89, 3088.99, 3096.66, 3143.00, 3…
$ AMZN.High      3180.76, 3184.13, 3174.43, 3142.10, 3118.67, 3190.47, 3…
$ AMZN.Low       3141.69, 3120.02, 3088.00, 3076.00, 3072.82, 3126.00, 3…
$ AMZN.Close     3158.00, 3177.29, 3104.20, 3101.49, 3116.42, 3156.97, 3…
$ AMZN.Volume    2751300, 3286300, 4100800, 3030200, 3064700, 4155800, 3…
$ AMZN.Adjusted  3158.00, 3177.29, 3104.20, 3101.49, 3116.42, 3156.97, 3…

STEP 2: Plotting a Candlestick plot using Plotly

We use the plot_ly() function to plot a candlestick plot.

Syntax: plot_ly( data = , x = , type = "candlestick", open = , close = , high = , low = )

Where:

  1. x = variable to be plotted in x axis
  2. data = dataframe to be used
  3. type = type of the chart
  4. open = opening price
  5. close = closing price
  6. high = maximum price
  7. low price = lowest price

Note:

  1. The %>% sign in the syntax earlier makes the code more readable and enables R to read further code without breaking it.
  2. We also use layout() function to give a title to the graph

fig <- plot_ly(x = ~Date, data = df, type = "candlestick", open = ~AMZN.Open, close = ~AMZN.Close, high = ~AMZN.High, low = ~AMZN.Low) %>% layout(title = 'Candlestick Plot using Plotly') embed_notebook(fig)

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Learn to Build an End-to-End Machine Learning Pipeline - Part 2
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, incorporating Hopsworks' feature store and Weights and Biases for model experimentation.

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.