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

How can I store the historical stock prices (using an array)?

我正在使用可在此处找到的雅虎金融库: 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' 值,但我如何才能使该数组仅存储接近值而不存储任何其他值?

我编写了一个示例,将所有收盘价(每个日期)存储在一个数组中。输出是今年前 7 个月的 GOOG 收盘价:

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

你有很多选择。最常见的是获取该词典列表并将其保存为(这是按照我对不同存储格式的偏好顺序)pickle、json、csv 或 raw_text.

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

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

将为您提供可追溯到 2010 年的相同日期/收盘价/调整收盘价/开盘价/高价/低价/交易量。如果您想 save/load 将数据写入磁盘,您可以执行

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

它还会使分析数据变得更加容易。例如,如果您只想要平均收盘价

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