Candlestick chart is chart to display the price, including opening price, closing price, highest and lowest price, of a security or derivatives in a given period. In this folder, we will go over how to create candlestick charts with Python and Plotly.
The following scripts are used in this chapter:
- AAPLprice.py
This chapter requires the following packages for the scripts used:
- Pandas
- Plotly
- yfinance (Only used for AAPLprice.py to obtain data, does not require if you have any data set from other sources)
This chapter may obtain Apple stock price via API call using yfinance. In this
Data is a list of go.Candlestick(). Although you are expected to only have one go.Candlestick() in the list, you should put go.Candlestick() in a list for Plotly.
go.Candlestick() has the following parameters:
- x: Date
- open: Opening price of the security/derivative
- high: Highest price of the security/derivative
- low: Lowest price of the security/derivative
- close: Closing price of the security/derivative
Genetic Layout parameters suggested to use:
- title (Dictionary): Chart title and fonts
- text: Chart title to be displayed
- x: text location on x-dimension, from 0-1
- y: text location on y-dimension, from 0-1
- yaxis (Dictionary): Y-axis setting
- title: Y-axis title
- tickmode: Setting of ticks
- xaxis (Dictionary): X-axis setting
- title: X-axis title
- tickmode: Setting of ticks
- rangeslider (Dicionary): Whether to include rangeslider for user to shrink or expand the range, default to True and display the same candlestick chart in the rangeslider
Candlestick Chart Exclusive parameters:
- Data
- open: Opening price of the security/derivative
- high: Highest price of the security/derivative
- low: Lowest price of the security/derivative
- close: Closing price of the security/derivative
- Layout
- xaxis (Dictionary): y-axis setting
- rangeslider (Dicionary): Whether to include rangeslider for user to shrink or expand the range
- xaxis (Dictionary): y-axis setting
# Prepare data
data.append(go.Candlestick(x=df['Date'], open=df['Open'],
high=df['High'], low=df['Low'],
close=df['Close']))
layout = {'title':{'text':'Year-to-Date Apple Stock Price','x':0.5},
'xaxis':{'title':'Date','rangeslider':{'visible':False}},
'yaxis':{'title':'Price ($)'},
'hovermode':False}
By default, Plotly set rangeslider to be visible. You must turn off manually.
Plotly Documentation Candlestick Chart
