This blog we will learn to build Candlestick chart for stocks using R. First thing we need to ensure that “plotly” is installed from the GitHub library

install.packages(“plotly”)
install.packages(“quantmod”)

A Candlestick chart is frequently used in stocks, security, derivative or currency analysis to describe the price movement. Each candle indicates single day pattern with its open, high, low and close. Basically they look like box plot but they are not relevant to each other.

Syntax

plot_ly(x = date, open = …, high = …, low = …, close = …, type = “candlestick”)

library(plotly)
library(quantmod)

Lets take an example of Stock price movement of Infosys where green is increasing and red is decreasing movement in stock price.

getSymbols("INFY",src='yahoo')
## [1] "INFY"
# importing data into data frame and limiting to last 30 days
df <- data.frame(Date=index(INFY),coredata(INFY))
df <- tail(df,30)

Next step is to use plot_ly to plot the graph

p <- df %>%
plot_ly(x = ~Date, type="candlestick",
          open = ~INFY.Open, close = ~INFY.Close,
          high = ~INFY.High, low = ~INFY.Low) %>%
  layout(title="CandleStick Chart")

Create an sharable link for chart

# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
sharelink = api_create(p,filename="CandleStick")
## Found a grid already named: 'CandleStick Grid'. Since fileopt='overwrite', I'll try to update it
## Found a plot already named: 'CandleStick'. Since fileopt='overwrite', I'll try to update it
sharelink

Please feel free to ask any questions 🙂

Do subscribe to Tabvizexplorer.com to keep receiving regular updates.

In this post, we will see how to plot candlestick chart in Tableau. We will use the same dataset which we used in previous article on stock analysis – using LOD function.

Here is the final candlestick dashboard for stocks:

Below are the steps to create:

Step 1: Import the stock prices into Tableau which I have in following format.

We will create first 2 calculated fields i.e. closeopendiff and highopendiff formula given below:

Step 2: Drag Low and Open in the row tab (covert into avg) and make avg(open) into dual axis

Step 3: Edit both the axis and uncheck the include zero checkbox

Step 4: Under tab of Marks, change the automatic to Gann chart for All option. Then under avg(low) in marks tab, drag highlowdiff field into size and make it slim as shown in below image

Step 5: Similarly, drag closeopendiff into avg(open) in marks tab into size and make it thick as shown in below image.

Step 6: Drag actual date from dimension and put into column then right click to change the setting to day as shown below

Step 7: Create one more calculated field to allocate color based on open and close

Step 8: Drag color to avg(open) in marks and put onto color to allocate green for True value and red for False value

Now your candlestick graph is ready to use. you can use date filter to play around with different dates and see the candlestick graphs for different stocks.

Comments and feedback are welcomed!!!

Do subscribe to my blog to keep receiving new posts 🙂

Being an avid follower of stock markets and thought of creating an area graph of stocks and highlighting the lows and high of the stock for the duration selected something similar to what we see on moneycontrol.com.

My output dashboard here:

Here I will show how to use LOD (Level of Detail) expression in Tableau (More information available here)

Step 1: Downloaded stock price data using getbhavcopy app, which gives an option to extract data of all the stocks at NSE or BSE with their open, close, high, low and volume. I downloaded data from 1st Jan’17 till 22nd Feb’18 data from the app. It looked liked this:

I used command prompt commands to combine all files into one file.

Step 2: Import the data into Tableau and create area graph for the date range and applied company name and actual date as filters. For this example, I applied context filter to actual date field (using add to context filter option in Filters)

Then I created 3 calculated fields to identify High and Low price for selected duration of stock company using LOD expression named Fixed using following Nested Fixed formula:

 

Step 3: Create the worksheet with dual axis as shown below:

 

Then, I integrated the worksheet into dashboard to create output dashboard where user can select any stock with date range user wants to get output with high and lows of that duration.

Thanks!

Do subscribe to my blog to keep receiving updates on new posts