0

I'm a fairly new programmer attempting to pull google intraday stock data for every stock in the RUSSEL3000 and place that data into CSV files. 1 file per stock. I'm able to use the following code to accomplish that task for daily data pulled from yahoo finance with pandas without any problems:

import pandas as pd
from pandas import DataFrame
import datetime, csv, pandas.io.data

def PullTicker(file='RUS3000.csv'):
    RUS3000 = []
    with open(file,'rb') as f:
                reader = csv.reader(f)
                for row in reader:
                    RUS3000.extend(row)

    for Ticker in RUS3000:
        Filename = Ticker+'.csv'
        StockData = pd.io.data.get_data_yahoo(Ticker,
                                        start=datetime.datetime(2004, 12, 5),
                                        end=datetime.datetime(2014, 12, 5))
        print Ticker,StockData.head(),'\n'
        StockData.to_csv(Filename)

PullTicker()

This is the code I'm using for GoogleFinance intraday data, with it I'm able to get the data I want to print to the terminal:

import urllib,time,datetime
import pandas as pd
from pandas import DataFrame
import pandas.io.data
import csv

class Quote(object):

  DATE_FMT = '%Y-%m-%d'
  TIME_FMT = '%H:%M:%S'

  def __init__(self):
    self.symbol = ''
    self.date,self.time,self.open_,self.high,self.low,self.close,self.volume = ([] for _ in range(7))

  def append(self,dt,open_,high,low,close,volume):
    self.date.append(dt.date())
    self.time.append(dt.time())
    self.open_.append(float(open_))
    self.high.append(float(high))
    self.low.append(float(low))
    self.close.append(float(close))
    self.volume.append(int(volume))

  def to_csv(self):
    return ''.join(["{0},{1},{2},{3:.2f},{4:.2f},{5:.2f},{6:.2f},{7}\n".format(self.symbol,
              self.date[bar].strftime('%Y-%m-%d'),self.time[bar].strftime('%H:%M:%S'),
              self.open_[bar],self.high[bar],self.low[bar],self.close[bar],self.volume[bar]) 
              for bar in xrange(len(self.close))])

  def write_csv(self,filename):
    with open(filename,'w') as f:
      f.write(self.to_csv())

  def read_csv(self,filename):
    self.symbol = ''
    self.date,self.time,self.open_,self.high,self.low,self.close,self.volume = ([] for _ in range(7))
    for line in open(filename,'r'):
      symbol,ds,ts,open_,high,low,close,volume = line.rstrip().split(',')
      self.symbol = symbol
      dt = datetime.datetime.strptime(ds+' '+ts,self.DATE_FMT+' '+self.TIME_FMT)
      self.append(dt,open_,high,low,close,volume)
    return True

  def __repr__(self):
    return self.to_csv()

class GoogleIntradayQuote(Quote):
  ''' Intraday quotes from Google. Specify interval seconds and number of days '''
  def __init__(self,symbol,interval_seconds=300,num_days=5):
    super(GoogleIntradayQuote,self).__init__()
    self.symbol = symbol.upper()
    url_string = "http://www.google.com/finance/getprices?q={0}".format(self.symbol)
    url_string += "&i={0}&p={1}d&f=d,o,h,l,c,v".format(interval_seconds,num_days)
    csv = urllib.urlopen(url_string).readlines()
    for bar in xrange(7,len(csv)):
      if csv[bar].count(',')!=5: continue
      offset,close,high,low,open_,volume = csv[bar].split(',')
      if offset[0]=='a':
        day = float(offset[1:])
        offset = 0
      else:
        offset = float(offset)
      open_,high,low,close = [float(x) for x in [open_,high,low,close]]
      dt = datetime.datetime.fromtimestamp(day+(interval_seconds*offset))
      self.append(dt,open_,high,low,close,volume)


if __name__ == '__main__':
  q = GoogleIntradayQuote
  RUS3000 = []
  with open('RUS3000.csv','rb') as f:
                reader=csv.reader(f)
                for row in reader:
                    RUS3000.extend(row)

  for Ticker in RUS3000:
    Filename = Ticker+'.csv'
    StockData = q(Ticker,60,1)
    print StockData
    StockData.to_csv(Filename)

When I attempt to save that data to a csv using pandas to_csv function I get the error:

Traceback (most recent call last):
  File "intraday.py", line 81, in <module>
    StockData.to_csv(Filename)
TypeError: to_csv() takes exactly 1 argument (2 given)

Why would the data from my class, assigned to a variable, count as two arguments while the data pulled using pandas.io.data works perfectly? This is my first question on Stackoverflow and I appreciate any help in advance! Thank you.

Austin
  • 51
  • 3
  • Which 'to_csv()' method are you executing that makes problem? the one in the `Quote` class? – Marcin Dec 08 '14 at 03:36
  • The one in the main function, I just edited the question to reflect that. I also tried renaming the to_csv function in the quote class just incase it was interfering but got the same error. – Austin Dec 08 '14 at 03:39
  • 1
    Execting `StockData.to_csv(Filename)` results in passing two arguments to `def to_csv(self):`. One self and the other is Filename. Shouldn't it be `StockData.write_csv(Filename)`? – Marcin Dec 08 '14 at 03:45
  • Haha, you are absolutely right. I shouldn't have been messing with pandas when I already had the function written out. Thank you Marcin, works perfectly. – Austin Dec 08 '14 at 03:50

0 Answers0