I have a python dataframe df
containing Date, Open, High,Low, Close, and volume values.
I am attempting to Candlestick chart a stock. For some reason, I can't seem to get matplotlib.finance.candlestick2_ochl()
to corectly display days where the stock price closed self.df['closep']
was higher than stock price open self.df['openp']
. On these positive (green) days, matplotlib either displays a doji or nothing at all.
Not quite sure why I am getting this behavior. I suspect it has something to do with the way my program is accessing the stock data from my Pandas DataFrame self.df
.
Here is my code:
# Visualize my stock data
import pandas as pd
import datetime
import time
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.finance import candlestick2_ochl
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
class MyStock:
def __init__(self, stock):
self.stock = stock
self.pullData()
def pullData(self):
try:
print('Currently pulling', self.stock)
print(str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')))
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/' + self.stock + '/chartdata;type=quote;range=5d/csv'
self.df = pd.read_csv(urlToVisit, header=None, names=['date', 'closep', 'highp', 'lowp', 'openp', 'volume'],
skiprows=22, dtype=float)
print('Pulled', self.stock, '\n')
except Exception as e:
print('main loop', str(e))
print('Constucting DataFrame\n')
self.df['date'] = pd.to_datetime(self.df['date'], unit='s')
self.df = self.df.set_index('date').tz_localize('UTC').tz_convert('US/Eastern').reset_index()
self.df['date'] = self.df['date'].dt.strftime('%Y-%m-%d %H:%M:%S')
print(self.df.tail())
def CandleStick(self):
self.fig = plt.figure()
ax1 = plt.subplot2grid((6, 4), (1, 0), rowspan=4, colspan=4, axisbg='w')
candlestick2_ochl(ax1, opens=self.df['openp'], closes=self.df['closep'], highs=self.df['highp'], lows=self.df['lowp'], width=.75, colorup='k', colordown='r', alpha=0.75)
plt.ylabel('Stock Price')
ax1.grid(True)
for label in ax1.xaxis.get_ticklabels():
label.set_rotation(90)
plt.subplots_adjust(left=.10, bottom=.19, right=.93, top=.95, wspace=.20, hspace=0)
plt.suptitle(self.stock + ' Stock Price')
plt.show()
amd = MyStock('AMD')
amd.CandleStick()
and a screenshot of the output alongside the associated section of the DataFrame:
You can clearly see in this screen cap that positive days (green) are not being displayed correctly. I am sure I'm missing something very obvious here.
Any help is appreciated.
Please see this code that produces rightly colored candlesticks:
import pandas as pd
import datetime
import time
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.finance import candlestick2_ochl
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
class MyStock:
def __init__(self, stock):
self.stock = stock
self.pullData()
def pullData(self):
try:
print('Currently pulling', self.stock)
print(str(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')))
urlToVisit = 'http://chartapi.finance.yahoo.com/instrument/1.0/' + self.stock + '/chartdata;type=quote;range=2d/csv'
self.df = pd.read_csv(urlToVisit, header=None, names=['date', 'closep', 'highp', 'lowp', 'openp', 'volume'],
skiprows=22, dtype=float)
print('Pulled', self.stock, '\n')
except Exception as e:
print('main loop', str(e))
print('Constucting DataFrame\n')
self.df['date'] = pd.to_datetime(self.df['date'], unit='s')
self.df = self.df.set_index('date').tz_localize('UTC').tz_convert('US/Eastern').reset_index()
self.df['date'] = self.df['date'].dt.strftime('%Y-%m-%d %H:%M:%S')
print(self.df.tail())
def CandleStick(self):
self.fig = plt.figure()
ax1 = plt.subplot2grid((6, 4), (1, 0), rowspan=4, colspan=4, axisbg='w')
candlestick2_ochl(ax1,
self.df['openp'], self.df['highp'], self.df['lowp'], self.df['closep'],
width=.75, colorup='g', colordown='r', alpha=0.75)
plt.ylabel('Stock Price')
ax1.grid(True)
for label in ax1.xaxis.get_ticklabels():
label.set_rotation(90)
plt.subplots_adjust(left=.10, bottom=.19, right=.93, top=.95, wspace=.20, hspace=0)
plt.suptitle(self.stock + ' Stock Price')
plt.show()
amd = MyStock('AMD')
amd.CandleStick()