Source code for

.. Copyright (c) 2014- Marshall Farrier

Data - remote retrieval (:mod:``)

.. currentmodule::

Wraps `Pandas Remote Data Access 

from ftplib import FTP
from functools import partial
import io

import pandas as pd
import as web

[docs]def get(equity, *args, **kwargs): """get(equity, start=None, end=None) Get DataFrame for an individual equity from Yahoo! .. versionchanged:: 0.5.0 Default `start` (2010-01-01) and `end` (current date). Examples -------- >>> import pynance as pn >>> aapl ='aapl', '2014-03-01', '2015-03-01') >>> goog ='goog', '2014') """ return web.DataReader(equity, 'yahoo', *args, **kwargs)
[docs]def equities(country='US'): """ Return a DataFrame of current US equities. .. versionadded:: 0.4.0 .. versionchanged:: 0.5.0 Return a DataFrame Parameters ---------- country : str, optional Country code for equities to return, defaults to 'US'. Returns ------- eqs : :class:`pandas.DataFrame` DataFrame whose index is a list of all current ticker symbols. Columns are 'Security Name' (e.g. 'Zynerba Pharmaceuticals, Inc. - Common Stock') and 'Exchange' ('NASDAQ', 'NYSE', 'NYSE MKT', etc.) Examples -------- >>> eqs ='US') Notes ----- Currently only US markets are supported. """ nasdaqblob, otherblob = _getrawdata() eq_triples = [] eq_triples.extend(_get_nas_triples(nasdaqblob)) eq_triples.extend(_get_other_triples(otherblob)) eq_triples.sort() index = [triple[0] for triple in eq_triples] data = [triple[1:] for triple in eq_triples] return pd.DataFrame(data, index, columns=['Security Name', 'Exchange'], dtype=str)
def _get_nas_triples(blob): return _get_triples(blob, {}, 'NASDAQ') def _get_other_triples(blob): # exchanges = { 'A': 'NYSE MKT', 'N': 'NYSE', 'P': 'NYSE ARCA', 'Z': 'BATS'} return _get_triples(blob, exchanges, 'unknown') def _get_triples(blob, exchanges, default): lines = blob.splitlines() fields = [line.split('|') for line in lines[1:]] # last line is generally file info if not fields[-1][1].isalpha() or not fields[-1][2].isalpha(): return [(field[0], field[1], exchanges.get(field[2], default)) for field in fields[:-1]] return [(field[0], field[1], exchanges.get(field[2], default)) for field in fields] def _getrawdata(): # _nasdaqio = io.BytesIO() _otherio = io.BytesIO() _host = '' _directory = 'symboldirectory' _nasdaqfile = 'nasdaqlisted.txt' _otherfile = 'otherlisted.txt' _ftp = FTP(_host) _ftp.login() _ftp.cwd(_directory) _ftp.retrbinary('RETR ' + _nasdaqfile, partial(_handle_binary, _nasdaqio)) _ftp.retrbinary('RETR ' + _otherfile, partial(_handle_binary, _otherio)) _ftp.quit() _nasdaq = _nasdaqio.getvalue() _nasdaqio.close() _other = _otherio.getvalue() _otherio.close() return _nasdaq.decode("utf-8"), _other.decode("utf-8") def _handle_binary(sio, more_data): # sio.write(more_data)