Skip to content

Commit

Permalink
Small notebook updates
Browse files Browse the repository at this point in the history
  • Loading branch information
erdewit committed Jun 1, 2018
1 parent 321ff53 commit a22bb8f
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 218 deletions.
88 changes: 52 additions & 36 deletions notebooks/basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"*startLoop() only works in notebooks, not in regular Python programs.*"
"*Note that startLoop() only works in notebooks, not in regular Python programs.*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Connecting\n",
"The main player of the whole package is the \"IB\" class. Let's create an IB instance and connect to a running TWS/IBG application (remember it must have an open API port):"
"The main player of the whole package is the \"IB\" class. Let's create an IB instance and connect to a running TWS/IBG application:"
]
},
{
Expand Down Expand Up @@ -91,7 +91,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"If the connection failed, then double-check the hostname and port. For IB Gateway the default port is 4002. Make sure the clientId is not already in use.\n",
"If the connection failed, then verify that the application has the API port enabled and double-check the hostname and port. For IB Gateway the default port is 4002. Make sure the clientId is not already in use.\n",
"\n",
"If the connection succeeded, then ib will be synchronized with TWS/IBG. The \"current state\" is now available via methods such as ib.positions(), ib.trades(), ib.openTrades(), ib.accountValues() or ib.tickers(). Let's list the current positions:"
]
Expand All @@ -104,17 +104,16 @@
{
"data": {
"text/plain": [
"[Position(account='DU772802', contract=Contract(secType='STK', conId=9579970, symbol='IWM', exchange='ARCA', currency='USD', localSymbol='IWM', tradingClass='IWM'), position=3.0, avgCost=154.57666665),\n",
" Position(account='DU772802', contract=Contract(secType='FUT', conId=269745169, symbol='ES', lastTradeDateOrContractMonth='20180615', multiplier='50', currency='USD', localSymbol='ESM8', tradingClass='ES'), position=3.0, avgCost=132847.88333335),\n",
"[Position(account='DU772802', contract=Contract(secType='STK', conId=9579970, symbol='IWM', exchange='ARCA', currency='USD', localSymbol='IWM', tradingClass='IWM'), position=210.0, avgCost=159.05424285),\n",
" Position(account='DU772802', contract=Contract(secType='FUT', conId=269745169, symbol='ES', lastTradeDateOrContractMonth='20180615', multiplier='50', currency='USD', localSymbol='ESM8', tradingClass='ES'), position=-5.0, avgCost=135347.95),\n",
" Position(account='DU772802', contract=Contract(secType='FOP', conId=280107184, symbol='ES', lastTradeDateOrContractMonth='20180615', strike=2700.0, right='C', multiplier='50', currency='USD', localSymbol='ESM8 C2700', tradingClass='ES'), position=3.0, avgCost=2029.19776665),\n",
" Position(account='DU772802', contract=Contract(secType='OPT', conId=300772895, symbol='VIX', lastTradeDateOrContractMonth='20180619', strike=17.0, right='C', multiplier='100', currency='USD', localSymbol='VIX 180620C00017000', tradingClass='VIX'), position=8.0, avgCost=231.2378),\n",
" Position(account='DU772802', contract=Contract(secType='FUT', conId=290048111, symbol='ZN', lastTradeDateOrContractMonth='20180620', multiplier='1000', currency='USD', localSymbol='ZN JUN 18', tradingClass='ZN'), position=-1.0, avgCost=119420.255),\n",
" Position(account='DU772802', contract=Contract(secType='OPT', conId=310595717, symbol='IWM', lastTradeDateOrContractMonth='20180720', strike=157.0, right='P', multiplier='100', currency='USD', localSymbol='IWM 180720P00157000', tradingClass='IWM'), position=5.0, avgCost=504.7878),\n",
" Position(account='DU772802', contract=Contract(secType='CASH', conId=12087792, symbol='EUR', currency='USD', localSymbol='EUR.USD', tradingClass='EUR.USD'), position=20000.0, avgCost=1.2),\n",
" Position(account='DU772802', contract=Contract(secType='CASH', conId=12087792, symbol='EUR', currency='USD', localSymbol='EUR.USD', tradingClass='EUR.USD'), position=20000.0, avgCost=1.18555),\n",
" Position(account='DU772802', contract=Contract(secType='OPT', conId=265160118, symbol='AAPL', lastTradeDateOrContractMonth='20180615', strike=170.0, right='C', multiplier='100', currency='USD', localSymbol='AAPL 180615C00170000', tradingClass='AAPL'), position=3.0, avgCost=605.9978),\n",
" Position(account='DU772802', contract=Contract(secType='STK', conId=265598, symbol='AAPL', exchange='NASDAQ', currency='USD', localSymbol='AAPL', tradingClass='NMS'), position=2.0, avgCost=163.16),\n",
" Position(account='DU772802', contract=Contract(secType='STK', conId=270639, symbol='INTC', exchange='NASDAQ', currency='USD', localSymbol='INTC', tradingClass='NMS'), position=300.0, avgCost=51.64666665),\n",
" Position(account='DU772802', contract=Contract(secType='FUT', conId=286286636, symbol='VIX', lastTradeDateOrContractMonth='20180516', multiplier='1000', currency='USD', localSymbol='VXK8', tradingClass='VX'), position=7.0, avgCost=17602.31)]"
" Position(account='DU772802', contract=Contract(secType='STK', conId=265598, symbol='AAPL', exchange='NASDAQ', currency='USD', localSymbol='AAPL', tradingClass='NMS'), position=536.0, avgCost=185.46992535),\n",
" Position(account='DU772802', contract=Contract(secType='STK', conId=270639, symbol='INTC', exchange='NASDAQ', currency='USD', localSymbol='INTC', tradingClass='NMS'), position=300.0, avgCost=51.64666665)]"
]
},
"execution_count": 4,
Expand All @@ -141,7 +140,7 @@
{
"data": {
"text/plain": [
"[AccountValue(account='DU772802', tag='NetLiquidationByCurrency', value='834794', currency='BASE', modelCode='')]"
"[AccountValue(account='DU772802', tag='NetLiquidationByCurrency', value='842526', currency='BASE', modelCode='')]"
]
},
"execution_count": 5,
Expand All @@ -167,7 +166,7 @@
"### Contracts\n",
"\n",
"Contracts can be specified in different ways:\n",
"* The ibapi way, by creating an empty Contract object and setting it's attributes one by one;\n",
"* The ibapi way, by creating an empty Contract object and setting its attributes one by one;\n",
"* By using Contract and giving the attributes as keyword argument;\n",
"* By using the specialized Stock, Option, Future, Forex, Index, CFD, Commodity,\n",
" Bond, FuturesOption, MutualFund or Warrant contracts.\n",
Expand Down Expand Up @@ -208,27 +207,28 @@
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 1.55 ms, sys: 0 ns, total: 1.55 ms\n",
"Wall time: 289 ms\n"
]
"data": {
"text/plain": [
"[ContractDetails(contract=Contract(secType='STK', conId=76792991, symbol='TSLA', exchange='SMART', primaryExchange='NASDAQ', currency='USD', localSymbol='TSLA', tradingClass='NMS'), marketName='NMS', 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,SCALE,SCALEODD,SCALERST,SMARTSTG,SNAPMID,SNAPMKT,SNAPREL,STP,STPLMT,SWEEP,TRAIL,TRAILLIT,TRAILLMT,TRAILMIT,WHATIF', validExchanges='SMART,AMEX,NYSE,CBOE,ISE,CHX,ARCA,ISLAND,DRCTEDGE,BEX,BATS,EDGEA,CSFBALGO,JEFFALGO,BYX,IEX,CVGXALGO,NYSENAT,PSX', priceMagnifier=1, longName='TESLA INC', industry='Consumer, Cyclical', category='Auto Manufacturers', subcategory='Auto-Cars/Light Trucks', timeZoneId='EST5EDT', tradingHours='20180601:0400-20180601:2000;20180602:CLOSED;20180603:CLOSED;20180604:0400-20180604:2000;20180605:0400-20180605:2000;20180606:0400-20180606:2000;20180607:0400-20180607:2000;20180608:0400-20180608:2000;20180609:CLOSED;20180610:CLOSED;20180611:0400-20180611:2000;20180612:0400-20180612:2000;20180613:0400-20180613:2000;20180614:0400-20180614:2000;20180615:0400-20180615:2000;20180616:CLOSED;20180617:CLOSED;20180618:0400-20180618:2000;20180619:0400-20180619:2000;20180620:0400-20180620:2000;20180621:0400-20180621:2000;20180622:0400-20180622:2000;20180623:CLOSED;20180624:CLOSED;20180625:0400-20180625:2000;20180626:0400-20180626:2000;20180627:0400-20180627:2000;20180628:0400-20180628:2000;20180629:0400-20180629:2000;20180630:CLOSED;20180701:CLOSED;20180702:0400-20180702:2000;20180703:0400-20180703:2000;20180704:0400-20180704:2000;20180705:0400-20180705:2000', liquidHours='20180601:0930-20180601:1600;20180602:CLOSED;20180603:CLOSED;20180604:0930-20180604:1600;20180605:0930-20180605:1600;20180606:0930-20180606:1600;20180607:0930-20180607:1600;20180608:0930-20180608:1600;20180609:CLOSED;20180610:CLOSED;20180611:0930-20180611:1600;20180612:0930-20180612:1600;20180613:0930-20180613:1600;20180614:0930-20180614:1600;20180615:0930-20180615:1600;20180616:CLOSED;20180617:CLOSED;20180618:0930-20180618:1600;20180619:0930-20180619:1600;20180620:0930-20180620:1600;20180621:0930-20180621:1600;20180622:0930-20180622:1600;20180623:CLOSED;20180624:CLOSED;20180625:0930-20180625:1600;20180626:0930-20180626:1600;20180627:0930-20180627:1600;20180628:0930-20180628:1600;20180629:0930-20180629:1600;20180630:CLOSED;20180701:CLOSED;20180702:0930-20180702:1600;20180703:0930-20180703:1600;20180704:0930-20180704:1600;20180705:0930-20180705:1600', mdSizeMultiplier=100, aggGroup=1, marketRuleIds='26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26')]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"\n",
"contract = Stock('TSLA', 'SMART', 'USD')\n",
"cds = ib.reqContractDetails(contract)"
"ib.reqContractDetails(contract)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Requesting contract details generally takes about 100 - 300 ms. Let's have a look at the actual\n",
"contract details that were sent:"
"### Current state vs request\n",
"\n",
"Doing a request involves network traffic going up and down and can take considerable time. The current state on the other hand is always immediately available. So it is preferable to use the current state methods over requests. For example, use ``ib.openOrders()`` in preference over ``ib.reqOpenOrders()``, or ``ib.positions()`` over ``ib.reqPositions()``, etc:"
]
},
{
Expand All @@ -237,18 +237,34 @@
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[ContractDetails(marketName='NMS', 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,SCALE,SCALEODD,SCALERST,SMARTSTG,SNAPMID,SNAPMKT,SNAPREL,STP,STPLMT,SWEEP,TRAIL,TRAILLIT,TRAILLMT,TRAILMIT,WHATIF', validExchanges='SMART,AMEX,NYSE,CBOE,ISE,CHX,ARCA,ISLAND,DRCTEDGE,NSX,BEX,BATS,EDGEA,CSFBALGO,JEFFALGO,BYX,IEX,CVGXALGO,PSX', priceMagnifier=1, longName='TESLA INC', industry='Consumer, Cyclical', category='Auto Manufacturers', subcategory='Auto-Cars/Light Trucks', timeZoneId='EST5EDT', tradingHours='20180508:0400-20180508:2000;20180509:0400-20180509:2000;20180510:0400-20180510:2000;20180511:0400-20180511:2000;20180512:CLOSED;20180513:CLOSED;20180514:0400-20180514:2000;20180515:0400-20180515:2000;20180516:0400-20180516:2000;20180517:0400-20180517:2000;20180518:0400-20180518:2000;20180519:CLOSED;20180520:CLOSED;20180521:0400-20180521:2000;20180522:0400-20180522:2000;20180523:0400-20180523:2000;20180524:0400-20180524:2000;20180525:0400-20180525:2000;20180526:CLOSED;20180527:CLOSED;20180528:0400-20180528:2000;20180529:0400-20180529:2000;20180530:0400-20180530:2000;20180531:0400-20180531:2000;20180601:0400-20180601:2000;20180602:CLOSED;20180603:CLOSED;20180604:0400-20180604:2000;20180605:0400-20180605:2000;20180606:0400-20180606:2000;20180607:0400-20180607:2000;20180608:0400-20180608:2000;20180609:CLOSED;20180610:CLOSED;20180611:0400-20180611:2000', liquidHours='20180508:0930-20180508:1600;20180509:0930-20180509:1600;20180510:0930-20180510:1600;20180511:0930-20180511:1600;20180512:CLOSED;20180513:CLOSED;20180514:0930-20180514:1600;20180515:0930-20180515:1600;20180516:0930-20180516:1600;20180517:0930-20180517:1600;20180518:0930-20180518:1600;20180519:CLOSED;20180520:CLOSED;20180521:0930-20180521:1600;20180522:0930-20180522:1600;20180523:0930-20180523:1600;20180524:0930-20180524:1600;20180525:0930-20180525:1600;20180526:CLOSED;20180527:CLOSED;20180528:0930-20180528:1600;20180529:0930-20180529:1600;20180530:0930-20180530:1600;20180531:0930-20180531:1600;20180601:0930-20180601:1600;20180602:CLOSED;20180603:CLOSED;20180604:0930-20180604:1600;20180605:0930-20180605:1600;20180606:0930-20180606:1600;20180607:0930-20180607:1600;20180608:0930-20180608:1600;20180609:CLOSED;20180610:CLOSED;20180611:0930-20180611:1600', mdSizeMultiplier=100, aggGroup=1, marketRuleIds='26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26', summary=Contract(secType='STK', conId=76792991, symbol='TSLA', exchange='SMART', primaryExchange='NASDAQ', currency='USD', localSymbol='TSLA', tradingClass='NMS'))]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 16 µs, sys: 2 µs, total: 18 µs\n",
"Wall time: 24.1 µs\n"
]
}
],
"source": [
"cds"
"%time l = ib.positions()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 3.23 ms, sys: 180 µs, total: 3.41 ms\n",
"Wall time: 43.8 ms\n"
]
}
],
"source": [
"%time l = ib.reqPositions()"
]
},
{
Expand All @@ -262,7 +278,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"metadata": {
"collapsed": true
},
Expand All @@ -280,7 +296,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"metadata": {
"collapsed": true
},
Expand All @@ -301,15 +317,15 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2018-05-08 14:23:38,594 ib_insync.ib INFO Disconnecting from 127.0.0.1:7497, 144 B sent in 7 messages, 21.1 kB received in 393 messages, session time 502 ms.\n",
"2018-05-08 14:23:38,597 ib_insync.client INFO Disconnected\n"
"2018-06-01 12:33:09,966 ib_insync.ib INFO Disconnecting from 127.0.0.1:7497, 153 B sent in 8 messages, 22.1 kB received in 406 messages, session time 830 ms.\n",
"2018-06-01 12:33:09,970 ib_insync.client INFO Disconnected\n"
]
}
],
Expand Down
Loading

0 comments on commit a22bb8f

Please sign in to comment.