Skip to content

Instantly share code, notes, and snippets.

@rm-rf-etc
Created April 10, 2022 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rm-rf-etc/dde17a1ca6ea5f7894d19d5ffdfe636a to your computer and use it in GitHub Desktop.
Save rm-rf-etc/dde17a1ca6ea5f7894d19d5ffdfe636a to your computer and use it in GitHub Desktop.
Candlestick Charts With Matplotlib
# https://www.statology.org/matplotlib-python-candlestick-chart/
import matplotlib.pyplot as plt
#create figure
plt.figure()
#define width of candlestick elements
width = .4
width2 = .05
#define up and down prices
up = prices[prices.close>=prices.open]
down = prices[prices.close<prices.open]
#define colors to use
col1 = 'green'
col2 = 'red'
#plot up prices
plt.bar(up.index,up.close-up.open,width,bottom=up.open,color=col1)
plt.bar(up.index,up.high-up.close,width2,bottom=up.close,color=col1)
plt.bar(up.index,up.low-up.open,width2,bottom=up.open,color=col1)
#plot down prices
plt.bar(down.index,down.close-down.open,width,bottom=down.open,color=col2)
plt.bar(down.index,down.high-down.open,width2,bottom=down.open,color=col2)
plt.bar(down.index,down.low-down.close,width2,bottom=down.close,color=col2)
#rotate x-axis tick labels
plt.xticks(rotation=45, ha='right')
#display candlestick chart
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment