1

如果您知道特定标签,Quandl 模块可以轻松调用库存信息。例如,Apple 类似于 GOOG/NASDAQ_APPL,这意味着 Quandl 从谷歌金融获得,而苹果在纳斯达克交易。但是,当我在 python 调用函数中输入一堆股票并且我不知道它是 NYSE 还是 NASDAQ 时,它会变得很烦人。有没有更通用的方法来检索任意库存?

4

1 回答 1

1

如果您的股票代码范围仅限于几个主要证券交易所,您可以按照此处给出的 Quandl 中通过 Python API 获取股票数据的描述进行操作。Quandl 提供 secwiki_tickers.csv 的 .csv 文件,其中包含 3339 只股票的集合,您可以获取这些股票的收盘价(代码如下 .4)。执行此操作的 Python 代码可能如下所示:

import numpy as np
import pandas as pd
import Quandl

df = pd.read_csv('secwiki_tickers.csv')

dp = pd.read_csv('portfolio.lst',names=['pTicker'])
pTickers = dp.pTicker.values  # converts into a list
tmpTickers = []

for i in range(len(pTickers)):
test=df[df.Ticker==pTickers[i]]
if not(test.empty):
    tmp=test.Price.values+'.4'  # of <type 'numpy.ndarray'>
    tmp2=tmp.tolist()
    tmpTickers.append(tmp2)

print(tmpTickers)
tmp = []

for i in range(len(tmpTickers)):
    tmp2 = str(tmpTickers[i]).strip('[]')
    print(tmp)
    tmp.append(str(tmp2).strip('\'\''))

QuandlTickers = tmp
print(QuandlTickers)

data = Quandl.get(QuandlTickers, authtoken='YourAuthToken', \
                 trim_start='2014-10-01', trim_end='2014-11-04', \
                 transformation='rdiff')

d=data.values.T # return-series (frequency: daily) based on close-prices
print(d)

我们使用portfolio.lst作为纯文本文件,存储N个所需股票代码的列表,例如:

AAPL
IBM
TXN

最终,在指定时间间隔内获取每日返回系列的 NumPy 数组是,例如:

[[-0.01558313  0.00725953 -0.0028028   0.         -0.00873319  0.02075949
   0.00218254 -0.00287072 -0.00913333 -0.01062018 -0.01225316 -0.01312282
   0.01464783  0.02139859  0.0271652   0.00507466  0.01786581  0.00372031
  -0.00104543  0.01550756  0.00562114 -0.00335383  0.00953449  0.01296296
  -0.00731261]
 [-0.01401254 -0.00138911  0.0094163   0.0019611  -0.01761532  0.0196543
  -0.01552598 -0.00262847 -0.01296187  0.00152572 -0.01115343 -0.01050894
   0.0122887  -0.0711343  -0.03471319 -0.00882191  0.00241053 -0.0006166
  -0.00129566  0.01068759 -0.00085575  0.00544476  0.00030423 -0.00024331
  -0.01040399]
 [-0.01698469         nan         nan -0.00416489 -0.01319035  0.020213
  -0.01959949 -0.07127336 -0.0189518   0.00906272  0.01063578  0.01941066
   0.00183528  0.01694527  0.05314118 -0.00320718  0.00815101  0.01212766
   0.00798823  0.01147028 -0.00350515 -0.01655287  0.0448138   0.00845751
   0.00638978]]

请注意,正如评论中已经提到的,您可以获取 AAPL 价格系列跳过 Quandl 的 WIKI 数据库。

于 2015-04-22T12:34:28.463 回答