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

Python 如何存储历史股价(使用数组)?,python,runtime-error,finance,yahoo-finance,Python,Runtime Error,Finance,Yahoo Finance,我使用的是雅虎财经库,可在此处找到: 我有一个带有股票代码的文本文件。我正在浏览股票代码并打印股票价格的历史数据 我如何获取收盘价并存储它们,以便以后使用它们(计算平均值) 这是我的密码: from yahoo_finance import Share from pprint import pprint #for easy to view historical data import calendar import datetime import time cal = cale

我使用的是雅虎财经库,可在此处找到:

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

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

这是我的密码:

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

在遍历数组时,如何存储“close”值?我想创建另一个数组来存储“收盘”值,但如何使该数组只存储收盘值而不存储任何其他值?

我编写了一个示例,将所有收盘价格(每个日期)存储在一个数组中。产量为GOOG在今年前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

你有很多选择。最常见的方法是将字典列表保存为pickle、json、csv或raw_文本(这是我对不同存储格式的偏好顺序)

我想不请自来地给你一些建议,引导你走向熊猫。它将使您的生活更轻松,因为它在数据分析以及文件读写方面做得特别好。您可以通过将字典列表转换为数据帧来获得使用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

与其打印这些值,不如将它们存储在变量中。谢谢,我以前从未听说过熊猫,我将研究它!熊猫是伟大的。我把我的答案放在你的帖子的范围内,但我肯定会用熊猫之类的东西来做任何你想做的分析。
>>> print df.Close.mean()
25.470388733914213