Last active
July 28, 2019 18:29
-
-
Save ranaroussi/8e2b5526f2efc85214bfeb46fe33e1a4 to your computer and use it in GitHub Desktop.
plot_candlestick
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import matplotlib.pyplot as plt | |
# future-compatibility with Panda 0.25 | |
from pandas.plotting import register_matplotlib_converters | |
register_matplotlib_converters() | |
import numpy as np | |
from matplotlib.ticker import FormatStrFormatter | |
try: | |
from mpl_finance import candlestick_ohlc | |
except ImportError: | |
from matplotlib.finance import candlestick_ohlc | |
def plot_candlestick(df, keys=('open', 'high', 'low', 'close'), | |
colorup='#00EE00', colordown='#FF0000', | |
colorwick='#808080', linewidth=1.3, alpha=1, | |
ax=None, figsize=None, savefig=None, show=True): | |
up_open = np.where(df[keys[0]] < df[keys[3]], df[keys[0]], np.nan) | |
up_close = np.where(df[keys[0]] < df[keys[3]], df[keys[3]], np.nan) | |
down_open = np.where(df[keys[0]] > df[keys[3]], df[keys[0]], np.nan) | |
down_close = np.where(df[keys[0]] > df[keys[3]], df[keys[3]], np.nan) | |
same_open = np.where(df[keys[0]] == df[keys[3]], df[keys[0]], np.nan) | |
same_close = np.where(df[keys[0]] == df[keys[3]], | |
df[keys[3]]-0.0005, np.nan) | |
samecolor = colorwick if colorwick is not None else '#808080' | |
fig = None | |
if ax is None: | |
fig, ax = plt.subplots(figsize=figsize) | |
# plot candles | |
if colorwick is not None: | |
ax.vlines(df.index.values, df[keys[2]], df[keys[1]], | |
color=colorwick, linewidth=linewidth/4, alpha=alpha) | |
ax.vlines(df.index.values, up_open, up_close, | |
linewidth=linewidth, color=colorup, alpha=alpha) | |
ax.vlines(df.index.values, down_open, down_close, | |
linewidth=linewidth, color=colordown, alpha=alpha) | |
ax.vlines(df.index.values, same_open, same_close, linewidth=linewidth * | |
1.4, color=samecolor, alpha=min([1, alpha*2])) | |
ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f')) | |
if fig: | |
fig.autofmt_xdate() | |
try: | |
plt.subplots_adjust(hspace=0, bottom=0, top=1) | |
except Exception: | |
pass | |
try: | |
fig.tight_layout() | |
except Exception: | |
pass | |
if savefig: | |
if isinstance(savefig, dict): | |
plt.savefig(**savefig) | |
else: | |
plt.savefig(savefig) | |
if show and fig: | |
plt.show(fig) |
Added the missing imports :)
Ok. works great! Thanks so much!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Ran! I just tried it and I got a different error, stating that:
in plot_candlestick(df, keys, colorup, colordown, colorwick, linewidth, alpha, ax, figsize, savefig, show)
25 fig = None
26 if ax is None:
---> 27 fig, ax = plt.subplots(figsize=figsize)
28
29 # plot candles
NameError: name 'plt' is not defined
.....Got any ideas?