想通了。我需要的是循環通過創建的子項目(無論你稱他們爲何)循環。
#Refrences
from time import *
from dateutil import rrule
from datetime import timedelta
import urllib.request as web
import pandas as pd
import os
forToday = 'http://netfonds.no/quotes/tradedump.php?csv_format=csv&paper='
dateToday = strftime("%Y-%m-%d", localtime())
#dateToday = "2014-10-07"
def pullToday(exchange,stock):
hold=[]
fileName=('data/'+exchange+'/'+stock+'/'+dateToday+'.txt')
try:
if not os.path.isdir(os.path.dirname(fileName)):
os.makedirs(os.path.dirname(fileName))
except OSError:
print("Something went very wrong. Review the dir creation section")
pageBuffer=web.urlopen(forToday+stock+'.'+exchange)
pageData=pd.read_csv(pageBuffer,usecols=['time','price','quantity'])
for i in pageData.index:
pageData['time'][i]=pd.datetime.strptime(pageData['time'][i],'%Y%m%dT%H%M%S')
print(hold)
#pageData = pageData.set_index('time',drop=False)
for minute in rrule.rrule(rrule.MINUTELY, dtstart=pd.datetime.strptime(dateToday+"T15:30","%Y-%m-%dT%H:%M"), until=pd.datetime.strptime(dateToday+"T21:58","%Y-%m-%dT%H:%M")):
temp = pageData[(pageData['time']>=minute)&(pageData['time']<minute+timedelta(seconds=60))]
volume=0
priceSum=0
low=123456
high=0
for i in temp.index:
volume+=temp['quantity'][i]
priceSum+=temp['quantity'][i]*temp['price'][i]
if temp['price'][i]>high:
high=temp['price'][i]
if temp['price'][i]<low:
low=temp['price'][i]
priceSum/=volume
hold.append([minute,volume,low,high,round(priceSum,4)])
minute=pd.datetime.strptime(dateToday+"T21:59","%Y-%m-%dT%H:%M")
temp = pageData[(pageData['time']>=minute)&(pageData['time']<minute+timedelta(seconds=180))]
volume=0
priceSum=0
low=123456
high=0
for i in temp.index:
volume+=temp['quantity'][i]
priceSum+=temp['quantity'][i]*temp['price'][i]
if temp['price'][i]>high:
high=temp['price'][i]
if temp['price'][i]<low:
low=temp['price'][i]
priceSum/=volume
hold.append([minute,volume,low,high,round(priceSum,4)])
compiledData=pd.DataFrame(hold ,columns=['TimeStamp', 'Volume', 'Low', 'High', 'Average'])
#for i in compiledData.index:
#compiledData['TimeStamp'][i]-=pd.datetime.strptime(dateToday+"TZ06","%Y-%m-%dTZ%H")
print(compiledData)
dataFile = open(fileName,'w')
dataFile.write('#Format: Timestamp;Volume;Low;High;Median\n')
dataFile.close()
pageData.to_csv(fileName,index=False,sep=';',mode='a',header=False)
def getList(fileName):
stockList = []
file = open(fileName+'.txt', 'r').read()
fileByLines = file.split('\n')
for eachLine in fileByLines:
if '#' not in eachLine:
lineByValues = eachLine.split('.')
stockList.append(lineByValues)
return stockList
start_time = time()
stockList = getList('stocks')
#for eachEntry in stockList:
# pullToday(eachEntry[0],eachEntry[1])
pullToday('O','AAPL')
delay=str(round((time()-start_time)))
print('Finished in ' + delay)
顯示示例輸入和輸出,具有完整的工作示例代碼。 – 2014-10-08 03:24:43