Hello!
Do you remember when we created a couple of time series charts to visualize exchange rate data? Now we’ll use candlestick charts to see historical stock price data.
What is a Candlestick Chart?
It’s a financial chart that shows how the price fluctuates on a daily basis. For example, it can show you the four price points of a stock: open, close, high and low.
Getting Our Essentials
As always, we will import our Python libraries:
import pandas as pd import numpy as np import chart_studio.plotly as py import plotly.express as px import plotly.graph_objects as go from plotly import __version__ from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
Next, we will get our stock_price
DataFrame:
Let’s Create a Candlestick Chart!
Before moving on, we need to organize our data by Company
. Here’s how we are going to do this:
apple = stock_price[stock_price['Company'] == 'AAPL']
facebook = stock_price[stock_price['Company'] == 'FB']
google = stock_price[stock_price['Company'] == 'GOOG']
microsoft = stock_price[stock_price['Company'] == 'MSFT']
nike = stock_price[stock_price['Company'] == 'NKE']
nvidia = stock_price[stock_price['Company'] == 'NVDA']
tesla = stock_price[stock_price['Company'] == 'TSLA']
Now we will use the data to create the traces:
fig = go.Figure()
fig.add_trace(go.Candlestick(x=apple['Date'],
open=apple['Open'],
close=apple['Close'],
low=apple['Low'],
high=apple['High'], name='Apple', increasing_line_color= 'rgb(163,170,174)', decreasing_line_color= 'rgb(0,0,0)'))
fig.add_trace(go.Candlestick(x=facebook['Date'],
open=facebook['Open'],
close=facebook['Close'],
low=facebook['Low'],
high=facebook['High'], name='Facebook', increasing_line_color= 'rgb(66,103,178)', decreasing_line_color= 'rgb(255,255,255)'))
fig.add_trace(go.Candlestick(x=google['Date'],
open=google['Open'],
close=google['Close'],
low=google['Low'],
high=google['High'], name='Google', increasing_line_color= 'rgb(66,133,244)', decreasing_line_color= 'rgb(244,160,0)'))
fig.add_trace(go.Candlestick(x=microsoft['Date'],
open=microsoft['Open'],
close=microsoft['Close'],
low=microsoft['Low'],
high=microsoft['High'], name='Microsoft', increasing_line_color= 'rgb(242,80,34)', decreasing_line_color= 'rgb(0,164,239)'))
fig.add_trace(go.Candlestick(x=nike['Date'],
open=nike['Open'],
close=nike['Close'],
low=nike['Low'],
high=nike['High'], name='Nike', increasing_line_color= 'rgb(0,0,0)', decreasing_line_color= 'rgb(255,255,255)'))
fig.add_trace(go.Candlestick(x=nvidia['Date'],
open=nvidia['Open'],
close=nvidia['Close'],
low=nvidia['Low'],
high=nvidia['High'], name='NVIDIA', increasing_line_color= 'rgb(122,181,71)', decreasing_line_color= 'rgb(26,25,24)'))
fig.add_trace(go.Candlestick(x=tesla['Date'],
open=tesla['Open'],
close=tesla['Close'],
low=tesla['Low'],
high=tesla['High'], name='Tesla', increasing_line_color= 'rgb(242,242,242)', decreasing_line_color= 'rgb(204,0,0)'))
Let’s set the title and get our chart:
fig.update_layout( title='Historical Stock Price Data (Last 6 Months)') fig.show()
You can interact with this chart in two ways:
1) Just like in the time series charts, you can use the slider to explore specific time frames.
2) You can filter by stock, just go to the legend and click on the company’s name to hide/show traces.
Ok, so which of these charts do you prefer: time series or candlestick?
Please feel free to reach out with your comments and/or questions. Stay safe!