In [1]:
# Prepare environment
import os, sys
sys.path.insert(0, os.path.abspath('..'))
from IPython.display import display
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

Example starts here


In [2]:
import asyncio
from ibstract import IB
from ibstract import MarketDataBlock
from ibstract import HistDataReq
from ibstract.utils import dtest, dtcst, dtutc

Instantiate IB and connect to Gateway/TWS

When connecting to IB Gateway/TWS, an internal semaphore is acquired for up to 32 clients operating concurrently.

In [3]:
ib = IB('127.0.0.1', 4002)
ib2 = IB()
ib2.connect('127.0.0.1', 4002)
print('ib connected? ', ib.connected)
print('ib2 connected? ', ib2.connected)
ib
ib2
ib connected?  True
ib2 connected?  True
Out[3]:
<IB connected to 127.0.0.1:4002 clientId=21>
Out[3]:
<IB connected to 127.0.0.1:4002 clientId=22>

HistDataReq - A request object for historical data

HistDataReq constructor signature: HistDataReq(sectype, symbol, barsize, timedur, timeend=None, datatype='TRADES', exchange='SMART', currency='USD')

In [4]:
req_list = [
    HistDataReq('Stock', 'GS', '1 hour', '5 d', dtest(2017,9,12,12)),
    HistDataReq('Stock', 'BAC', '1 day', '10 d'),  # timeend=None means datetime.now(tz=pytz.utc)
    HistDataReq('Stock', 'AMZN', '1 hour', '3 d', dtest(2017, 9, 12)),
    HistDataReq('Stock', 'GOOG', '30 mins', '3 d', dtutc(2017, 9, 12, 23)),
    HistDataReq('Stock', 'FB', '10 mins', '5 d', dtest(2017, 9, 12, 16)),
    HistDataReq('Stock', 'TVIX', '5 mins', '5 d', dtcst(2017, 9, 12, 16)),
]

for req in req_list:
    req
Out[4]:
HistDataReq(Stock, GS, 1h, 5d, 2017-09-12 12:00:00-04:00, TRADES, SMART, USD)
Out[4]:
HistDataReq(Stock, BAC, 1d, 10d, 2017-09-30 17:06:38.626811+00:00, TRADES, SMART, USD)
Out[4]:
HistDataReq(Stock, AMZN, 1h, 3d, 2017-09-12 00:00:00-04:00, TRADES, SMART, USD)
Out[4]:
HistDataReq(Stock, GOOG, 30m, 3d, 2017-09-12 23:00:00+00:00, TRADES, SMART, USD)
Out[4]:
HistDataReq(Stock, FB, 10m, 5d, 2017-09-12 16:00:00-04:00, TRADES, SMART, USD)
Out[4]:
HistDataReq(Stock, TVIX, 5m, 5d, 2017-09-12 16:00:00-05:00, TRADES, SMART, USD)

Download data from IB requested by a HistDataReq

Request contract details for a HistDataReq by the coroutine IB.hist_data_req_contract_details().

In [5]:
req = HistDataReq('Stock', 'GS', '1 hour', '5 d', dtest(2017,9,12,12))
loop = asyncio.get_event_loop()
contract_details_list = loop.run_until_complete(ib.hist_data_req_contract_details(req))
contract_details_list
Out[5]:
[ContractDetails(summary=Contract(conId=4627828, symbol='GS', secType='STK', exchange='SMART', primaryExchange='NYSE', currency='USD', localSymbol='GS', tradingClass='GS'), marketName='GS', minTick=0.01, orderTypes='ACTIVETIM,ADJUST,ALERT,ALGO,ALLOC,AON,AVGCOST,BASKET,COND,CONDORDER,DARKONLY,DARKPOLL,DAY,DEACT,DEACTDIS,DEACTEOD,DIS,GAT,GTC,GTD,GTT,HID,IBKRATS,ICE,IMB,IOC,LIT,LMT,LOC,MIT,MKT,MOC,MTL,NGCOMB,NODARK,NONALGO,OCA,OPG,OPGREROUT,PEGBENCH,POSTONLY,PREOPGRTH,REL,RPI,RTH,RTHIGNOPG,SCALE,SCALEODD,SCALERST,SMARTSTG,SNAPMID,SNAPMKT,SNAPREL,STP,STPLMT,SWEEP,TRAIL,TRAILLIT,TRAILLMT,TRAILMIT,WHATIF', validExchanges='SMART,AMEX,NYSE,CBOE,ISE,CHX,ARCA,ISLAND,VWAP,DRCTEDGE,NSX,BEX,BATS,EDGEA,CSFBALGO,JEFFALGO,BYX,IEX,CVGXALGO,TPLUS1,PSX', priceMagnifier=1, longName='GOLDMAN SACHS GROUP INC', industry='Financial', category='Banks', subcategory='Diversified Banking Inst', timeZoneId='EST5EDT', tradingHours='20170930:CLOSED;20171001:CLOSED;20171002:0400-2000;20171003:0400-2000;20171004:0400-2000;20171005:0400-2000;20171006:0400-2000;20171007:CLOSED;20171008:CLOSED;20171009:0400-2000;20171010:0400-2000;20171011:0400-2000;20171012:0400-2000;20171013:0400-2000;20171014:CLOSED;20171015:CLOSED;20171016:0400-2000;20171017:0400-2000;20171018:0400-2000;20171019:0400-2000;20171020:0400-2000;20171021:CLOSED;20171022:CLOSED;20171023:0400-2000;20171024:0400-2000;20171025:0400-2000;20171026:0400-2000;20171027:0400-2000;20171028:CLOSED;20171029:CLOSED;20171030:0400-2000;20171031:0400-2000;20171101:0400-2000;20171102:0400-2000;20171103:0400-2000', liquidHours='20170930:CLOSED;20171001:CLOSED;20171002:0930-1600;20171003:0930-1600;20171004:0930-1600;20171005:0930-1600;20171006:0930-1600;20171007:CLOSED;20171008:CLOSED;20171009:0930-1600;20171010:0930-1600;20171011:0930-1600;20171012:0930-1600;20171013:0930-1600;20171014:CLOSED;20171015:CLOSED;20171016:0930-1600;20171017:0930-1600;20171018:0930-1600;20171019:0930-1600;20171020:0930-1600;20171021:CLOSED;20171022:CLOSED;20171023:0930-1600;20171024:0930-1600;20171025:0930-1600;20171026:0930-1600;20171027:0930-1600;20171028:CLOSED;20171029:CLOSED;20171030:0930-1600;20171031:0930-1600;20171101:0930-1600;20171102:0930-1600;20171103:0930-1600', mdSizeMultiplier=100, aggGroup=1)]

The coroutine IB.hist_data_req_timezone() downloads the exchange time zone for the requested security.

In [6]:
req = HistDataReq('Stock', 'GS', '1 hour', '5 d', dtest(2017,9,12,12))
loop = asyncio.get_event_loop()
xchg_tz = loop.run_until_complete(ib.hist_data_req_timezone(req))
xchg_tz
Out[6]:
<DstTzInfo 'US/Eastern' LMT-1 day, 19:04:00 STD>

The coroutine IB.get_hist_data_async(*reqs) downloads historical data for one or multiple HistDataReqs concurrently, and output a list of MarketDataBlock instances in the order of input requests.

In [7]:
loop = asyncio.get_event_loop()
blk_list = loop.run_until_complete(ib.req_hist_data_async(*req_list[:3]))
for blk in blk_list:
    blk.df.head()
Out[7]:
opening high low closing volume barcount average
Symbol DataType BarSize TickerTime
GS TRADES 1h 2017-09-06 08:00:00-04:00 218.97 219.29 218.97 219.20 28 11 219.159
2017-09-06 09:00:00-04:00 219.10 220.78 218.67 219.58 4729 2596 219.796
2017-09-06 10:00:00-04:00 219.61 221.02 219.54 220.01 4451 2722 220.490
2017-09-06 11:00:00-04:00 219.98 220.20 217.73 218.20 4222 2500 219.020
2017-09-06 12:00:00-04:00 218.20 219.83 217.61 219.80 2680 1809 218.335
Out[7]:
opening high low closing volume barcount average
Symbol DataType BarSize TickerTime
BAC TRADES 1d 2017-09-17 20:00:00-04:00 24.42 24.77 24.41 24.70 579550 110935 24.6995
2017-09-18 20:00:00-04:00 24.70 24.99 24.62 24.86 485710 100294 24.8575
2017-09-19 20:00:00-04:00 24.88 25.24 24.67 25.06 734771 138090 24.9735
2017-09-20 20:00:00-04:00 25.03 25.28 24.92 25.16 511256 104320 25.1420
2017-09-21 20:00:00-04:00 25.01 25.07 24.85 25.02 522287 97246 24.9845
Out[7]:
opening high low closing volume barcount average
Symbol DataType BarSize TickerTime
AMZN TRADES 1h 2017-09-07 07:00:00-04:00 970.00 970.00 970.00 970.00 2 2 970.000
2017-09-07 08:00:00-04:00 970.87 975.00 970.78 975.00 49 36 972.557
2017-09-07 09:00:00-04:00 974.47 979.00 972.55 975.72 3779 2276 975.865
2017-09-07 10:00:00-04:00 975.91 979.75 973.52 979.01 3280 2159 976.938
2017-09-07 11:00:00-04:00 979.05 980.59 976.73 979.42 2238 1509 979.118

A blocking function IB.get_hist_data() is also available without using asyncio event loop. It drives the coroutine version internally.

In [8]:
req = HistDataReq('Stock', 'GS', '1 hour', '5 d', dtest(2017,9,12,12))
blk = ib.req_hist_data(req)[0]
blk
Out[8]:
                                                   opening    high     low  \
Symbol DataType BarSize TickerTime                                           
GS     TRADES   1h      2017-09-06 08:00:00-04:00   218.97  219.29  218.97   
                        2017-09-06 09:00:00-04:00   219.10  220.78  218.67   
                        2017-09-06 10:00:00-04:00   219.61  221.02  219.54   
                        2017-09-06 11:00:00-04:00   219.98  220.20  217.73   
                        2017-09-06 12:00:00-04:00   218.20  219.83  217.61   
                        2017-09-06 13:00:00-04:00   219.77  220.50  219.41   
                        2017-09-06 14:00:00-04:00   219.61  219.80  218.90   
                        2017-09-06 15:00:00-04:00   219.33  219.70  218.85   
                        2017-09-06 16:00:00-04:00   218.83  219.09  218.83   
                        2017-09-06 17:00:00-04:00   218.98  218.98  218.98   
                        2017-09-06 18:00:00-04:00   218.70  218.70  218.70   
                        2017-09-06 19:00:00-04:00   218.69  218.70  218.69   
                        2017-09-07 07:00:00-04:00   219.00  219.00  219.00   
                        2017-09-07 08:00:00-04:00   219.21  219.40  218.50   
                        2017-09-07 09:00:00-04:00   218.57  218.83  216.07   
                        2017-09-07 10:00:00-04:00   216.35  216.35  214.64   
                        2017-09-07 11:00:00-04:00   215.74  216.41  214.96   
                        2017-09-07 12:00:00-04:00   215.24  216.28  215.06   
                        2017-09-07 13:00:00-04:00   216.04  216.41  215.26   
                        2017-09-07 14:00:00-04:00   215.58  215.74  215.25   
                        2017-09-07 15:00:00-04:00   215.40  215.94  214.95   
                        2017-09-07 16:00:00-04:00   215.84  216.88  215.80   
                        2017-09-07 17:00:00-04:00   215.98  215.98  215.90   
                        2017-09-07 18:00:00-04:00   215.90  215.99  215.90   
                        2017-09-07 19:00:00-04:00   216.09  216.09  216.09   
                        2017-09-08 07:00:00-04:00   215.44  215.44  215.44   
                        2017-09-08 08:00:00-04:00   214.90  215.50  214.80   
                        2017-09-08 09:00:00-04:00   215.50  218.76  215.10   
                        2017-09-08 10:00:00-04:00   218.68  219.28  217.78   
                        2017-09-08 11:00:00-04:00   218.06  218.39  216.82   
                        2017-09-08 12:00:00-04:00   216.92  217.30  216.67   
                        2017-09-08 13:00:00-04:00   217.12  217.17  216.15   
                        2017-09-08 14:00:00-04:00   216.76  217.35  216.61   
                        2017-09-08 15:00:00-04:00   217.01  217.34  216.69   
                        2017-09-08 16:00:00-04:00   217.21  217.21  216.71   
                        2017-09-08 17:00:00-04:00   217.00  217.00  217.00   
                        2017-09-08 18:00:00-04:00   216.46  216.80  216.46   
                        2017-09-08 19:00:00-04:00   216.80  216.80  216.80   
                        2017-09-11 07:00:00-04:00   219.01  219.06  219.00   
                        2017-09-11 08:00:00-04:00   219.00  219.50  218.02   
                        2017-09-11 09:00:00-04:00   219.25  221.72  218.99   
                        2017-09-11 10:00:00-04:00   219.66  221.35  219.54   
                        2017-09-11 11:00:00-04:00   220.69  221.89  220.43   
                        2017-09-11 12:00:00-04:00   220.85  221.68  220.40   
                        2017-09-11 13:00:00-04:00   221.13  221.58  220.98   
                        2017-09-11 14:00:00-04:00   221.41  221.54  220.97   
                        2017-09-11 15:00:00-04:00   221.26  221.63  220.92   
                        2017-09-11 16:00:00-04:00   221.06  221.07  221.00   
                        2017-09-11 17:00:00-04:00   221.06  221.06  221.06   
                        2017-09-11 18:00:00-04:00   221.02  221.02  221.02   
                        2017-09-11 19:00:00-04:00   221.02  221.02  221.02   
                        2017-09-12 07:00:00-04:00   221.50  221.50  221.50   
                        2017-09-12 08:00:00-04:00   221.50  222.00  221.50   
                        2017-09-12 09:00:00-04:00   221.84  225.80  221.84   
                        2017-09-12 10:00:00-04:00   225.36  226.41  224.98   
                        2017-09-12 11:00:00-04:00   225.57  227.18  225.15   

                                                   closing  volume  barcount  \
Symbol DataType BarSize TickerTime                                             
GS     TRADES   1h      2017-09-06 08:00:00-04:00   219.20      28        11   
                        2017-09-06 09:00:00-04:00   219.58    4729      2596   
                        2017-09-06 10:00:00-04:00   220.01    4451      2722   
                        2017-09-06 11:00:00-04:00   218.20    4222      2500   
                        2017-09-06 12:00:00-04:00   219.80    2680      1809   
                        2017-09-06 13:00:00-04:00   219.57    2470      1492   
                        2017-09-06 14:00:00-04:00   219.33    2127      1447   
                        2017-09-06 15:00:00-04:00   219.05    5587      3451   
                        2017-09-06 16:00:00-04:00   218.99    3634         6   
                        2017-09-06 17:00:00-04:00   218.98       2         1   
                        2017-09-06 18:00:00-04:00   218.70       1         1   
                        2017-09-06 19:00:00-04:00   218.70       8         2   
                        2017-09-07 07:00:00-04:00   219.00       1         1   
                        2017-09-07 08:00:00-04:00   218.50      31        16   
                        2017-09-07 09:00:00-04:00   216.31    3338      1726   
                        2017-09-07 10:00:00-04:00   215.77    7048      4299   
                        2017-09-07 11:00:00-04:00   215.28    4571      3190   
                        2017-09-07 12:00:00-04:00   216.07    2191      1541   
                        2017-09-07 13:00:00-04:00   215.60    2058      1495   
                        2017-09-07 14:00:00-04:00   215.37    2206      1509   
                        2017-09-07 15:00:00-04:00   215.83    6582      4149   
                        2017-09-07 16:00:00-04:00   216.02    1869         9   
                        2017-09-07 17:00:00-04:00   215.90       9         6   
                        2017-09-07 18:00:00-04:00   215.99      14         3   
                        2017-09-07 19:00:00-04:00   216.09       1         1   
                        2017-09-08 07:00:00-04:00   215.44       1         1   
                        2017-09-08 08:00:00-04:00   215.50      22         8   
                        2017-09-08 09:00:00-04:00   218.67    5788      3250   
                        2017-09-08 10:00:00-04:00   218.04    4696      3283   
                        2017-09-08 11:00:00-04:00   216.89    2574      1880   
                        2017-09-08 12:00:00-04:00   217.14    2049      1433   
                        2017-09-08 13:00:00-04:00   216.76    2254      1565   
                        2017-09-08 14:00:00-04:00   217.01    1921      1373   
                        2017-09-08 15:00:00-04:00   217.24    3980      2789   
                        2017-09-08 16:00:00-04:00   216.71    2222         6   
                        2017-09-08 17:00:00-04:00   217.00       2         1   
                        2017-09-08 18:00:00-04:00   216.80       3         2   
                        2017-09-08 19:00:00-04:00   216.80       0         0   
                        2017-09-11 07:00:00-04:00   219.00      19        15   
                        2017-09-11 08:00:00-04:00   219.25      86        52   
                        2017-09-11 09:00:00-04:00   219.66    4403      2345   
                        2017-09-11 10:00:00-04:00   220.66    4182      2712   
                        2017-09-11 11:00:00-04:00   220.78    2952      2094   
                        2017-09-11 12:00:00-04:00   221.11    1833      1314   
                        2017-09-11 13:00:00-04:00   221.40    2417      1639   
                        2017-09-11 14:00:00-04:00   221.27    2008      1576   
                        2017-09-11 15:00:00-04:00   221.09    4115      3177   
                        2017-09-11 16:00:00-04:00   221.06    1969         5   
                        2017-09-11 17:00:00-04:00   221.06     206         1   
                        2017-09-11 18:00:00-04:00   221.02       2         2   
                        2017-09-11 19:00:00-04:00   221.02       0         0   
                        2017-09-12 07:00:00-04:00   221.50       3         3   
                        2017-09-12 08:00:00-04:00   221.80      41        34   
                        2017-09-12 09:00:00-04:00   225.35    6492      3768   
                        2017-09-12 10:00:00-04:00   225.69    4994      3229   
                        2017-09-12 11:00:00-04:00   226.93    4580      2769   

                                                   average  
Symbol DataType BarSize TickerTime                          
GS     TRADES   1h      2017-09-06 08:00:00-04:00  219.159  
                        2017-09-06 09:00:00-04:00  219.796  
                        2017-09-06 10:00:00-04:00  220.490  
                        2017-09-06 11:00:00-04:00  219.020  
                        2017-09-06 12:00:00-04:00  218.335  
                        2017-09-06 13:00:00-04:00  219.954  
                        2017-09-06 14:00:00-04:00  219.428  
                        2017-09-06 15:00:00-04:00  219.363  
                        2017-09-06 16:00:00-04:00  218.830  
                        2017-09-06 17:00:00-04:00  218.980  
                        2017-09-06 18:00:00-04:00  218.700  
                        2017-09-06 19:00:00-04:00  218.696  
                        2017-09-07 07:00:00-04:00  219.000  
                        2017-09-07 08:00:00-04:00  219.015  
                        2017-09-07 09:00:00-04:00  217.503  
                        2017-09-07 10:00:00-04:00  215.392  
                        2017-09-07 11:00:00-04:00  215.666  
                        2017-09-07 12:00:00-04:00  215.518  
                        2017-09-07 13:00:00-04:00  215.708  
                        2017-09-07 14:00:00-04:00  215.428  
                        2017-09-07 15:00:00-04:00  215.313  
                        2017-09-07 16:00:00-04:00  215.846  
                        2017-09-07 17:00:00-04:00  215.927  
                        2017-09-07 18:00:00-04:00  215.909  
                        2017-09-07 19:00:00-04:00  216.090  
                        2017-09-08 07:00:00-04:00  215.440  
                        2017-09-08 08:00:00-04:00  215.050  
                        2017-09-08 09:00:00-04:00  217.577  
                        2017-09-08 10:00:00-04:00  218.707  
                        2017-09-08 11:00:00-04:00  217.345  
                        2017-09-08 12:00:00-04:00  217.015  
                        2017-09-08 13:00:00-04:00  216.591  
                        2017-09-08 14:00:00-04:00  217.117  
                        2017-09-08 15:00:00-04:00  217.075  
                        2017-09-08 16:00:00-04:00  217.209  
                        2017-09-08 17:00:00-04:00  217.000  
                        2017-09-08 18:00:00-04:00  216.698  
                        2017-09-08 19:00:00-04:00  216.800  
                        2017-09-11 07:00:00-04:00  219.004  
                        2017-09-11 08:00:00-04:00  218.883  
                        2017-09-11 09:00:00-04:00  219.964  
                        2017-09-11 10:00:00-04:00  220.819  
                        2017-09-11 11:00:00-04:00  221.137  
                        2017-09-11 12:00:00-04:00  221.104  
                        2017-09-11 13:00:00-04:00  221.304  
                        2017-09-11 14:00:00-04:00  221.248  
                        2017-09-11 15:00:00-04:00  221.246  
                        2017-09-11 16:00:00-04:00  221.060  
                        2017-09-11 17:00:00-04:00  221.060  
                        2017-09-11 18:00:00-04:00  221.020  
                        2017-09-11 19:00:00-04:00  221.020  
                        2017-09-12 07:00:00-04:00  221.500  
                        2017-09-12 08:00:00-04:00  221.791  
                        2017-09-12 09:00:00-04:00  224.636  
                        2017-09-12 10:00:00-04:00  225.686  
                        2017-09-12 11:00:00-04:00  226.220