Skip to content

Commit

Permalink
adding bulk fetch minute bars
Browse files Browse the repository at this point in the history
  • Loading branch information
timkpaine committed Oct 10, 2018
1 parent 2964e76 commit c3b2567
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 8 deletions.
16 changes: 8 additions & 8 deletions pyEX/marketdata/ws.py
Expand Up @@ -77,53 +77,53 @@ def tradesWS(symbols=None, on_data=None):
def systemEventWS(on_data=None):
'''https://iextrading.com/developer/docs/#system-event'''
sendinit = ({'channels': ['systemevent']},)
return _stream(_wsURL('/deep'), sendinit, on_data)
return _stream(_wsURL('deep'), sendinit, on_data)


def tradingStatusWS(symbols=None, on_data=None):
'''https://iextrading.com/developer/docs/#trading-status'''
symbols = _strToList(symbols)
sendinit = ({'symbols': symbols, 'channels': ['tradingstatus']},)
return _stream(_wsURL('/deep'), sendinit, on_data)
return _stream(_wsURL('deep'), sendinit, on_data)


def opHaltStatusWS(symbols=None, on_data=None):
'''https://iextrading.com/developer/docs/#operational-halt-status'''
symbols = _strToList(symbols)
sendinit = ({'symbols': symbols, 'channels': ['ophaltstatus']},)
return _stream(_wsURL('/deep'), sendinit, on_data)
return _stream(_wsURL('deep'), sendinit, on_data)


def ssrStatusWS(symbols=None, on_data=None):
'''https://iextrading.com/developer/docs/#short-sale-price-test-status'''
symbols = _strToList(symbols)
sendinit = ({'symbols': symbols, 'channels': ['ssr']},)
return _stream(_wsURL('/deep'), sendinit, on_data)
return _stream(_wsURL('deep'), sendinit, on_data)


def securityEventWS(symbols=None, on_data=None):
'''https://iextrading.com/developer/docs/#security-event'''
symbols = _strToList(symbols)
sendinit = ({'symbols': symbols, 'channels': ['securityevent']},)
return _stream(_wsURL('/deep'), sendinit, on_data)
return _stream(_wsURL('deep'), sendinit, on_data)


def tradeBreakWS(symbols=None, on_data=None):
'''https://iextrading.com/developer/docs/#trade-break'''
symbols = _strToList(symbols)
sendinit = ({'symbols': symbols, 'channels': ['tradebreaks']},)
return _stream(_wsURL('/deep'), sendinit, on_data)
return _stream(_wsURL('deep'), sendinit, on_data)


def auctionWS(symbols=None, on_data=None):
'''https://iextrading.com/developer/docs/#auction'''
symbols = _strToList(symbols)
sendinit = ({'symbols': symbols, 'channels': ['auction']},)
return _stream(_wsURL('/deep'), sendinit, on_data)
return _stream(_wsURL('deep'), sendinit, on_data)


def officialPriceWS(symbols=None, on_data=None):
'''https://iextrading.com/developer/docs/#official-price'''
symbols = _strToList(symbols)
sendinit = ({'symbols': symbols, 'channels': ['official-price']},)
return _stream(_wsURL('/deep'), sendinit, on_data)
return _stream(_wsURL('deep'), sendinit, on_data)
31 changes: 31 additions & 0 deletions pyEX/stocks.py
@@ -1,3 +1,4 @@
import itertools
import requests
import pandas as pd
from io import BytesIO
Expand All @@ -8,6 +9,7 @@


def batch(symbols, types=None, _range='1m', last=10):
'''fetch a large number of fields at the same time'''
types = types or _BATCH_TYPES

if not isinstance(symbols, [].__class__):
Expand All @@ -31,6 +33,7 @@ def batch(symbols, types=None, _range='1m', last=10):


def batchDF(symbols, types=None, _range='1m', last=10):
'''fetch a large number of fields at the same time'''
x = batch(symbols, types, _range, last)

ret = {}
Expand All @@ -53,6 +56,7 @@ def batchDF(symbols, types=None, _range='1m', last=10):


def bulkBatch(symbols, types=None, _range='1m', last=10):
'''fetch a large number of fields for multiple symbols all at the same time'''
types = types or _BATCH_TYPES
args = []
empty_data = []
Expand Down Expand Up @@ -86,6 +90,7 @@ def bulkBatch(symbols, types=None, _range='1m', last=10):


def bulkBatchDF(symbols, types=None, _range='1m', last=10):
'''fetch a large number of fields for multiple symbols all at the same time'''
dat = bulkBatch(symbols, types, _range, last)
ret = {}
for symbol in dat:
Expand Down Expand Up @@ -181,6 +186,32 @@ def chartDF(symbol, timeframe='1m', date=None):
return df


def batchMinuteBars(symbol, dates):
'''fetch many dates worth of minute-bars for a given symbol'''
_raiseIfNotStr(symbol)
dates = [_strOrDate(date) for date in dates]
list_orig = dates.__class__

args = []
for date in dates:
args.append((symbol, '1d', date))

pool = ThreadPool(20)
rets = pool.starmap(chart, args)
pool.close()

return list_orig(itertools.chain(*rets))


def batchMinuteBarsDF(symbol, dates):
'''fetch many dates worth of minute-bars for a given symbol'''
data = batchMinuteBars(symbol, dates)
df = pd.DataFrame(data)
_toDatetime(df)
df.set_index(['date', 'minute'], inplace=True)
return df


def company(symbol):
'''https://iextrading.com/developer/docs/#company'''
_raiseIfNotStr(symbol)
Expand Down
85 changes: 85 additions & 0 deletions ws.ipynb
@@ -0,0 +1,85 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pyEX"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c = pyEX.topsWS(symbols='SIRI', on_data=print)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c.run()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"c = pyEX.tradesWS(symbols=['AAPL', 'SNAP', 'MSFT'], on_data=print)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:socketIO-client:ws-api.iextrading.com:443/socket.io [connection error] recv disconnected ([Errno 60] Operation timed out)\n"
]
}
],
"source": [
"c.run()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit c3b2567

Please sign in to comment.