我是一个相当新的程序员,试图为RUSSEL3000中的每个股票提取谷歌日内股票数据,并将该数据放入CSV文件中。每个库存1个文件。我可以使用以下代码完成该任务,从而使用大熊猫从雅虎财务中提取的日常数据没有任何问题:
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()
这是我用于GoogleFinance日内数据的代码,通过它我可以将我要打印的数据打印到终端:
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)
当我尝试使用pandas to_csv函数将数据保存到csv时,我收到错误:
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)
为什么我的类中的数据(分配给变量)计为两个参数,而使用pandas.io.data提取的数据效果很好?这是我在Stackoverflow上的第一个问题,我提前感谢任何帮助!谢谢。