如何存储历史股票价格(使用数组)?

时间:2015-12-07 01:16:47

标签: python runtime-error finance yahoo-finance

我正在使用可在此处找到的雅虎财务库: https://pypi.python.org/pypi/yahoo-finance/1.2.1

我有一个带有代码符号的文本文件。我正在浏览股票代码并打印股票价格的历史数据。

我如何获取收盘价并存储它们以便我以后可以使用它们(计算平均值)?

这是我的代码:

from yahoo_finance import Share

from pprint import pprint #for easy to view historical data

import calendar
import datetime 
import time





cal = calendar.TextCalendar(calendar.SUNDAY)
#cal.prmonth(today)

#using datetime below:

today = datetime.date.today() #todays date
todayH = str(today) # because in .get_historical I need to use a string 

yesterday = (today.toordinal()-10) #yesterdays date mathamatically

dateYes =  datetime.date.fromordinal(yesterday) #yesterdays date in format we want
dateYesH = str(dateYes) # because in .get_historical I need to use a string 


print 'today:', today
print dateYesH
print 'ordinal:', today.toordinal()


rand = Share('yhoo')
# print rand.get_price() (works)
#pprint(rand.get_historical(dateYesH, todayH))


#reading text file
file1 = open('TickerF.txt', 'r')
words = file1.read().split(' ')
length = len(words)
#print words 
#print len(words)
#print file1.read()
file1.close()


c = 0

try :
    while c < length:
        for i in words:
            symbol = str(i)
            stock = Share(symbol)
            c= c+1
            print i
        #print c
            pprint(stock.get_historical(dateYesH, todayH))

except: 
    pass

我的输出是:

today: 2015-12-06
2015-11-26
ordinal: 735938
YHOO
[{'Adj_Close': '34.91',
  'Close': '34.91',
  'Date': '2015-12-04',
  'High': '35.200001',
  'Low': '34.18',
  'Open': '34.34',
  'Symbol': 'YHOO',
  'Volume': '15502700'},
 {'Adj_Close': '34.34',
  'Close': '34.34',
  'Date': '2015-12-03',
  'High': '35.720001',
  'Low': '34.099998',
  'Open': '35.59',
  'Symbol': 'YHOO',
  'Volume': '17068000'},
 {'Adj_Close': '35.650002',
  'Close': '35.650002',
  'Date': '2015-12-02',
  'High': '36.389999',
  'Low': '34.77',
  'Open': '35.00',
  'Symbol': 'YHOO',
  'Volume': '56614000'},
 {'Adj_Close': '33.709999',
  'Close': '33.709999',
  'Date': '2015-12-01',
  'High': '33.889999',
  'Low': '33.470001',
  'Open': '33.869999',
  'Symbol': 'YHOO',
  'Volume': '10862500'},
 {'Adj_Close': '33.810001',
  'Close': '33.810001',
  'Date': '2015-11-30',
  'High': '33.830002',
  'Low': '32.849998',
  'Open': '33.029999',
  'Symbol': 'YHOO',
  'Volume': '17363600'},
 {'Adj_Close': '32.939999',
  'Close': '32.939999',
  'Date': '2015-11-27',
  'High': '33.09',
  'Low': '32.439999',
  'Open': '32.790001',
  'Symbol': 'YHOO',
  'Volume': '5313400'}]

如何在浏览阵列时存储“关闭”值?我有一个想法是创建另一个存储'close'值的数组,但是我该怎么做才能使数组只存储close值而不存储任何其他值?

2 个答案:

答案 0 :(得分:1)

我写了一个例子,将所有收盘价(每个日期)存储在一个数组中。产量是今年前7个月的封闭价格:

from yahoo_finance import Share    


stock = Share('GOOG')    
start_date = '2015-01-01'    
end_date = '2015-06-30'    

closes = [c['Close'] for c in stock.get_historical(start_date, end_date)]    

for c in closes:    
    print c

输出:

520.51001
521.52002
531.690002
535.22998
537.840027
540.47998
538.190002
536.690002
536.72998
529.26001
528.150024
527.200012
532.330017
534.609985
536.690002
526.690002
526.830017
533.330017
536.700012
540.309998
539.179993
533.98999
532.109985
539.780029
539.789978
532.320007
540.109985
542.51001
539.27002
537.359985
532.299988
533.849976
538.400024
529.619995
529.039978
535.700012
538.219971
530.700012
524.219971
530.799988
540.780029
537.900024
537.340027
549.080017
553.679993
555.369995
565.062561
547.002472
539.367458
533.972413
535.382408
524.052386
533.802391
532.532429
530.392405
539.172404
540.012477
540.782472
541.612446
537.022465
536.767432
535.532478
542.562439
548.002468
552.032502
548.342512
555.172522
558.787478
570.192597
558.81251
560.362537
557.992512
559.502513
550.842532
554.512509
547.322503
555.512505
551.182515
555.012538
568.852557
567.687558
575.332609
573.372583
573.64261
571.342601
558.402572
555.482516
543.872489
536.092424
531.912381
538.952441
542.872432
539.702422
542.842504
549.012501
542.932472
535.972405
536.942412
527.832406
531.002415
527.582391
522.762349
529.2424
528.482381
534.522445
510.662331
510.002318
518.63237
535.212448
539.952437
534.39245
518.042312
506.902294
508.082288
501.792271
500.872267
496.182251
492.552209
496.172274
502.682255
501.102268
501.962262
513.872306
524.812404

答案 1 :(得分:1)

你有很多选择。最常见的是将字典列表保存为(这取决于我对不同存储格式的偏好)pickle,json,csv或raw_text。

我想给你一些不请自来的建议,引导你走向大熊猫。它会让您的生活更轻松,因为它在数据分析以及读写文件方面做得特别好。只需将该字典列表转换为DataFrame,您就可以获得使用pandas的大部分好处,但是pandas还提供了yahoo_finance提供的一些相同的解析部分。例如:

from pandas.io import data
df = data.get_data_yahoo('YHOO')

将为您提供相同的日期/关闭/调整关闭/打开/高/低/卷回溯到2010年。如果您想将数据保存/加载到磁盘,您可以这样做

df.to_pickle('/tmp/yhoo.pkl')
df = pd.read_pickle('/tmp/yhoo.pkl')

它还可以更轻松地分析数据。例如,如果您只想要平均收盘价

>>> print df.Close.mean()
25.470388733914213