Created
April 10, 2022 08:36
-
-
Save rm-rf-etc/dde17a1ca6ea5f7894d19d5ffdfe636a to your computer and use it in GitHub Desktop.
Candlestick Charts With Matplotlib
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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