0

I am pulling stock prices from yahoo then i would like to chart them using matplotlib. This is one line of the dataframe that has date, open, close, high, low, adj close and volume ([Timestamp('2016-08-01 00:00:00') 31.299999 31.389999 31.129999 31.25 31.25 13129900])

ERROR : Failed Main Loop time data "[Timestamp('2016-08-01" does not match format '%Y-%m-%d %H:%M:%S

Your help would be much appreciated.

from pandas_datareader import data as pdr
import fix_yahoo_finance as yf
import time
import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
from matplotlib.finance import _candlestick as candlestick
import matplotlib
matplotlib.rcParams.update({'font.size': 9})
import pylab


def graphData(stock, MA1, MA2):
    try:
        try:
            print 'Pulling data on ', stock
            yf.pdr_override()
            stockData = pdr.get_data_yahoo(
                stock, start='2016-08-01', end='2017-08-01')
            stockData.reset_index(level=0, inplace=True)
            print stockData.head()
            print stockData.values

        except Exception, e:
            print str(e), 'Failed to pull price data'

        date, openp, highp, lowp, closep, adjclose, volume = np.loadtxt(
            stockData.values, delimiter=' ', unpack=True, converters={0: mdates.strpdate2num('%Y-%m-%d %H:%M:%S')})

        x = 0
        y = len(date)
        newAr = []
        while x < y:
            appendLine = date[x], openp[x], closep[x], highp[x], lowp[x], volume[x]
            newAr.append(appendLine)
            x += 1

        Av1 = movingAverage(closep, MA1)
        Av2 = movingAverage(closep, MA2)

        SP = len(date[MA2 - 1:])

        fig = plt.figure(facecolor='#07000d')

        #ax1 = plt.subplot(2, 1, 1)
        ax1 = plt.subplot2grid((6, 4), (1, 0), rowspan=4,
                               colspan=4, axisbg='#07000d')
        candlestick(ax1, newAr[-SP:], width=.75,
                    colorup='#9eff15', colordown='#ff1717')  # colorup = '#53C156, colordown='#ff1717

        label1 = str(MA1) + ' SMA'
        label2 = str(MA2) + ' SMA'
        ax1.plot(date[-SP:], Av1[-SP:], '#5998ff',
                 label=label1, linewidth=1.5)  # eledf9
        ax1.plot(date[-SP:], Av2[-SP:], '#e1edf9',
                 label=label2, linewidth=1.5)  # 4ee6fd

        #ax1.plot(date, openp)
        #ax1.plot(date, highp)
        #ax1.plot(date, lowp)
        #ax1.plot(date, closep)

        ax1.grid(True, color='w')
        ax1.xaxis.set_major_locator(mticker.MaxNLocator(10))
        ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
        plt.gca().yaxis.set_major_locator(mticker.MaxNLocator(prune='upper'))
        ax1.yaxis.label.set_color('w')
        ax1.spines['bottom'].set_color('#5998ff')
        ax1.spines['top'].set_color('#5998ff')
        ax1.spines['left'].set_color('#5998ff')
        ax1.spines['right'].set_color('#5998ff')
        ax1.tick_params(axis='y', colors='w')
        ax1.tick_params(axis='x', colors='w')
        plt.ylabel('Stock Price and Volume')

        maLeg = plt.legend(loc=9, ncol=2, prop={'size': 7},
                           fancybox=True, borderaxespad=0.)
        maLeg.get_frame().set_alpha(0.4)
        textEd = pylab.gca().get_legend().get_texts()
        pylab.setp(textEd[0:5], color='w')

        ax0 = plt.subplot2grid((6, 4), (0, 0), sharex=ax1, rowspan=1,
                               colspan=4, axisbg='#07000d')

        rsi = rsiFunc(closep)
        rsiCol = '#1a8782'
        posCol = '#386d13'
        negCol = '#8f2020'
        ax0.plot(date[-SP:], rsi[-SP:], rsiCol, linewidth=1.5)
        ax0.axhline(70, color=negCol)
        ax0.axhline(30, color=posCol)
        ax0.fill_between(date[-SP:], rsi[-SP:], 70, where=(rsi[-SP:]
                                                           >= 70), facecolor=negCol, edgecolor=negCol)
        ax0.fill_between(date[-SP:], rsi[-SP:], 30, where=(rsi[-SP:]
                                                           <= 30), facecolor=posCol, edgecolor=posCol)
        #ax0.set_ylim(0, 100)
        ax0.spines['bottom'].set_color('#5998ff')
        ax0.spines['top'].set_color('#5998ff')
        ax0.spines['left'].set_color('#5998ff')
        ax0.spines['right'].set_color('#5998ff')
        ax0.text(0.015, 0.95, 'RSI (14)', va='top',
                 color='w', transform=ax0.transAxes)
        ax0.tick_params(axis='x', colors='w')
        ax0.tick_params(axis='y', colors='w')
        ax0.set_yticks([30, 70])
        ax0.yaxis.label.set_color('w')
        # plt.gca().yaxis.set_major_locator(mticker.MaxNLocator(prune='lower'))
        # plt.ylabel('RSI')

        volumeMin = 0

        '''
        #ax2 = plt.subplot(4, 1, 4, sharex=ax1)
        ax2 = plt.subplot2grid(
            (5, 4), (4, 0), sharex=ax1, rowspan=1, colspan=4, axisbg='#07000d')
        #ax2.bar(date, volume)
        ax2.plot(date, volume, '#00ffe8', linewidth=.8)
        ax2.fill_between(date, volumeMin, volume,
                         facecolor='#00ffe8', alpha=.5)
        ax2.axes.yaxis.set_ticklabels([])
        ax2.grid(False)
        ax2.spines['bottom'].set_color('#5998ff')
        ax2.spines['top'].set_color('#5998ff')
        ax2.spines['left'].set_color('#5998ff')
        ax2.spines['right'].set_color('#5998ff')
        ax2.tick_params(axis='x', colors='w')
        ax2.tick_params(axis='y', colors='w')

        plt.ylabel('Volume', color='w')

        for label in ax2.xaxis.get_ticklabels():
            label.set_rotation(45)
        '''

        ax1v = ax1.twinx()
        ax1v.fill_between(date[-SP:], volumeMin, volume[-SP:],
                          facecolor='#00ffe8', alpha=.5)
        ax1v.axes.yaxis.set_ticklabels([])
        ax1v.grid(False)
        ax1v.spines['bottom'].set_color('#5998ff')
        ax1v.spines['top'].set_color('#5998ff')
        ax1v.spines['left'].set_color('#5998ff')
        ax1v.spines['right'].set_color('#5998ff')
        ax1v.set_ylim(0, 2 * volume.max())
        ax1v.tick_params(axis='x', colors='w')
        ax1v.tick_params(axis='y', colors='w')

        ax2 = plt.subplot2grid((6, 4), (5, 0), sharex=ax1,
                               rowspan=1, colspan=4, axisbg='#07000d')

        fillcolor = '#00ffe8'
        nslow = 26
        nfast = 12
        nema = 9

        emaslow, emafast, macd = computeMACD(closep)
        ema9 = expMovingAverage(macd, nema)

        ax2.plot(date[-SP:], macd[-SP:], color='#4ee6fd', lw=2)
        ax2.plot(date[-SP:], ema9[-SP:], color='#e1edf9', lw=1)
        ax2.fill_between(date[-SP:], macd[-SP:] - ema9[-SP:], 0, alpha=0.5,
                         facecolor=fillcolor, edgecolor=fillcolor)

        ax2.text(0.015, 0.95, 'MACD 12,26,9', va='top',
                 color='w', transform=ax2.transAxes)
        ax2.spines['bottom'].set_color('#5998ff')
        ax2.spines['top'].set_color('#5998ff')
        ax2.spines['left'].set_color('#5998ff')
        ax2.spines['right'].set_color('#5998ff')
        ax2.tick_params(axis='x', colors='w')
        ax2.tick_params(axis='y', colors='w')
        #plt.ylabel('MACD', color='w')
        # plt.gca().yaxis.set_major_locator(mticker.MaxNLocator(prune='upper'))
        ax2.yaxis.set_major_locator(
            mticker.MaxNLocator(nbins=5, prune='upper'))

        for label in ax2.xaxis.get_ticklabels():
            label.set_rotation(45)

        # plt.xlabel('Date')
        plt.suptitle(stock.upper(), color='w')
        plt.setp(ax0.get_xticklabels(), visible=False)
        plt.setp(ax1.get_xticklabels(), visible=False)

        plt.subplots_adjust(left=.09, bottom=.14, right=.94,
                            top=.95, wspace=.20, hspace=0)

        plt.show()
        # fig.savefig('Example.png', facecolor=fig.get_facecolor())

    except Exception, e:
        print 'Failed Main Loop', str(e)


while True:
    stockToUse = raw_input('Stock to chart: ')
    graphData(stockToUse, 12, 26)  # 20,200
Richard
  • 1
  • 2
  • Please trim your code down to the parts we need to worry about. –  Oct 31 '17 at 22:36
  • Looks like your offending line is `np.loadtxt(stockData.values, delimiter=' ', unpack=True, converters={0: mdates.strpdate2num('%Y-%m-%d %H:%M:%S')})`. Can you post a snippet of `stockData.values`? –  Oct 31 '17 at 22:39
  • here is a snippet of what stockData.values outputs.........[Timestamp('2016-08-01 00:00:00') 31.299999 31.389999 31.129999 31.25 31.25 13129900] – Richard Oct 31 '17 at 23:02
  • Is it a pandas Dataframe? Why are you not using `stockData.to_matrix()` instead of `np.loadtxt`? –  Oct 31 '17 at 23:09
  • yes it is a pandas dataframe.... Im not using stockData.to_matrix() because i wasnt familiar with it until now. I will look into that and give it a try. But i would still like to figure out this current problem using np.loadtxt as I do not like to be stumped LOL. I can easily save this dataframe as a txt file and import the text file and it works perfectly, but this method Im trying eliminates the need to save many txt files on my computer. – Richard Oct 31 '17 at 23:43
  • i guess the issue seems to be that when i convert it from dataframe to numpy array, it puts 'Timestamp" in front of the date, and i cant seem to get rid of that string – Richard Nov 01 '17 at 00:10
  • i guess the issue seems to be that when i convert it from dataframe to numpy array, its still in the pandas Timestamp and i cant seem to convert to something numpy is understands (which i guess is datetime). – Richard Nov 01 '17 at 01:23
  • Using `np.loadtxt` is a bad idea. That call is likely converting the dataframe to a string and then trying to parse it all over again. Basically, you are self defeating. Pandas is much much better at doing the parsing than Numpy –  Nov 01 '17 at 17:13

1 Answers1

0

Judging from the error, the offending line seems to be,

np.loadtxt(stockData.values, delimiter=' ', unpack=True, converters={0: mdates.strpdate2num('%Y-%m-%d %H:%M:%S')})

given that stockData is a Pandas DataFrame, what is actually happening here can be see by,

>>> import pandas as pd
>>> stockData = pd.DataFrame([[pd.Timestamp('2016-08-01 00:00:00'), 31.299999, 31.389999, 31.129999, 31.25, 31.25, 13129900]])
>>> stockData.values
array([[Timestamp('2016-08-01 00:00:00'), 31.299999, 31.389999, 31.129999,
        31.25, 31.25, 13129900]], dtype=object)
>>> str(stockData.values)
"[[Timestamp('2016-08-01 00:00:00') 31.299999 31.389999 31.129999 31.25\n  31.25 13129900]]"

So basically, you are implicitly casting your dataframe to a string and then trying to reparse.

Much better to just use stockData.values or stockData.as_matrix()

>>> stockData.values
array([[Timestamp('2016-08-01 00:00:00'), 31.299999, 31.389999, 31.129999,
        31.25, 31.25, 13129900]], dtype=object)
>>> stockData.as_matrix()
array([[Timestamp('2016-08-01 00:00:00'), 31.299999, 31.389999, 31.129999,
        31.25, 31.25, 13129900]], dtype=object)

if you need to figure out a more appropriate way to handle the timestamp, you could try something like,

>>> stockData = pd.DataFrame([[pd.Timestamp('2016-08-01 00:00:00'), 31.299999, 31.389999, 31.129999, 31.25, 31.25, 13129900]],
...                          columns=['date', 'open', 'close', 'high', 'low', 'adj close', 'volume'])
>>> stockData['date'] = stockData['date'].map(lambda d: d.day)
>>> stockData['date']
0    1
Name: date, dtype: int64
>>> stockData.values
array([[  1.00000000e+00,   3.12999990e+01,   3.13899990e+01,
          3.11299990e+01,   3.12500000e+01,   3.12500000e+01,
          1.31299000e+07]])