How to plot candlestick chart using plotly in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to plot candlestick chart using plotly in R?

How to plot candlestick chart using plotly in R?

This recipe helps you plot candlestick chart using plotly in R

0

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)

Relevant Projects

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.