Reputation: 309
The Quandl module makes it easy to call stock information if you know the specific label. For example, Apple would be something like GOOG/NASDAQ_APPL which means Quandl gets it from google finance, and apple is traded in NASDAQ. However, it gets annoying when I enter a bunch of stocks into the python call function and I don't know whether it is NYSE or NASDAQ. Is there a more general way to retrieve any arbitrary stock?
Upvotes: 1
Views: 1081
Reputation: 41
If your universe of stock tickers is limited to a few major stock exchanges you can follow the description of fetching stock data via Python API in Quandl given here. Quandl provides .csv file of secwiki_tickers.csv with a collection of 3339 stocks for which you can grab e.g. close prices (code .4 below). The Python code for doing that may look as follows:
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)
where we use portfolio.lst as a plain text file storing a list of N desired stock tickers, e.g.:
AAPL
IBM
TXN
Eventually, a NumPy array with fetched daily return-series for a specified time interval is, e.g.:
[[-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]]
Be aware, as already mentioned in comments that you can fetch AAPL price-series skipping WIKI database of Quandl.
Upvotes: 1