{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Contract details\n", "-------------------" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from ib_insync import *\n", "util.startLoop()\n", "\n", "import logging\n", "# util.logToConsole(logging.DEBUG)\n", "\n", "ib = IB()\n", "ib.connect('127.0.0.1', 7497, clientId=11)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose we want to find the contract details for AMD stock.\n", "Let's create a stock object and request the details for it:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "35" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "amd = Stock('AMD')\n", "\n", "cds = ib.reqContractDetails(amd)\n", "\n", "len(cds)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We get a long list of contract details. Lets print the first one:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "ContractDetails(contract=Contract(secType='STK', conId=4391, symbol='AMD', exchange='SMART', primaryExchange='NASDAQ', currency='USD', localSymbol='AMD', tradingClass='NMS', comboLegs=[]), marketName='NMS', minTick=0.01, orderTypes='ACTIVETIM,ADJUST,ALERT,ALGO,ALLOC,AON,AVGCOST,BASKET,BENCHPX,COND,CONDORDER,DARKONLY,DARKPOLL,DAY,DEACT,DEACTDIS,DEACTEOD,DIS,GAT,GTC,GTD,GTT,HID,IBKRATS,ICE,IMB,IOC,LIT,LMT,LOC,MIDPX,MIT,MKT,MOC,MTL,NGCOMB,NODARK,NONALGO,OCA,OPG,OPGREROUT,PEGBENCH,POSTONLY,PREOPGRTH,REL,RPI,RTH,SCALE,SCALEODD,SCALERST,SNAPMID,SNAPMKT,SNAPREL,STP,STPLMT,SWEEP,TRAIL,TRAILLIT,TRAILLMT,TRAILMIT,WHATIF', validExchanges='SMART,AMEX,NYSE,CBOE,PHLX,ISE,CHX,ARCA,ISLAND,DRCTEDGE,BEX,BATS,EDGEA,CSFBALGO,JEFFALGO,BYX,IEX,EDGX,FOXRIVER,NYSENAT,PSX', priceMagnifier=1, underConId=0, longName='ADVANCED MICRO DEVICES', contractMonth='', industry='Technology', category='Semiconductors', subcategory='Electronic Compo-Semicon', timeZoneId='EST (Eastern Standard Time)', tradingHours='20191231:0400-20191231:2000;20200101:CLOSED;20200102:0400-20200102:2000;20200103:0400-20200103:2000;20200104:CLOSED;20200105:CLOSED;20200106:0400-20200106:2000;20200107:0400-20200107:2000;20200108:0400-20200108:2000;20200109:0400-20200109:2000;20200110:0400-20200110:2000;20200111:CLOSED;20200112:CLOSED;20200113:0400-20200113:2000;20200114:0400-20200114:2000;20200115:0400-20200115:2000;20200116:0400-20200116:2000;20200117:0400-20200117:2000;20200118:CLOSED;20200119:CLOSED;20200120:0400-20200120:2000;20200121:0400-20200121:2000;20200122:0400-20200122:2000;20200123:0400-20200123:2000;20200124:0400-20200124:2000;20200125:CLOSED;20200126:CLOSED;20200127:0400-20200127:2000;20200128:0400-20200128:2000;20200129:0400-20200129:2000;20200130:0400-20200130:2000;20200131:0400-20200131:2000;20200201:CLOSED;20200202:CLOSED;20200203:0400-20200203:2000', liquidHours='20191231:0930-20191231:1600;20200101:CLOSED;20200102:0930-20200102:1600;20200103:0930-20200103:1600;20200104:CLOSED;20200105:CLOSED;20200106:0930-20200106:1600;20200107:0930-20200107:1600;20200108:0930-20200108:1600;20200109:0930-20200109:1600;20200110:0930-20200110:1600;20200111:CLOSED;20200112:CLOSED;20200113:0930-20200113:1600;20200114:0930-20200114:1600;20200115:0930-20200115:1600;20200116:0930-20200116:1600;20200117:0930-20200117:1600;20200118:CLOSED;20200119:CLOSED;20200120:0930-20200120:1600;20200121:0930-20200121:1600;20200122:0930-20200122:1600;20200123:0930-20200123:1600;20200124:0930-20200124:1600;20200125:CLOSED;20200126:CLOSED;20200127:0930-20200127:1600;20200128:0930-20200128:1600;20200129:0930-20200129:1600;20200130:0930-20200130:1600;20200131:0930-20200131:1600;20200201:CLOSED;20200202:CLOSED;20200203:0930-20200203:1600', evRule='', evMultiplier=0, mdSizeMultiplier=100, aggGroup=1, underSymbol='', underSecType='', marketRuleIds='26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26', secIdList=[], realExpirationDate='', lastTradeTime='', stockType='COMMON', cusip='', ratings='', descAppend='', bondType='', couponType='', callable=False, putable=False, coupon=0, convertible=False, maturity='', issueDate='', nextOptionDate='', nextOptionType='', nextOptionPartial=False, notes='')" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cds[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The contract itself is in the 'contract' property of the contract details. Lets make a list of contracts and look at the first:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Contract(secType='STK', conId=4391, symbol='AMD', exchange='SMART', primaryExchange='NASDAQ', currency='USD', localSymbol='AMD', tradingClass='NMS', comboLegs=[])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "contracts = [cd.contract for cd in cds]\n", "\n", "contracts[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To better spot the difference between all the contracts it's handy to convert to a DataFrame. There is a utility function to do that:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
secTypeconIdsymbollastTradeDateOrContractMonthstrikerightmultiplierexchangeprimaryExchangecurrencylocalSymboltradingClassincludeExpiredsecIdTypesecIdcomboLegsDescripcomboLegsdeltaNeutralContract
0STK4391AMD0.0SMARTNASDAQUSDAMDNMSFalse[]None
1STK32596680AMD0.0SMARTIBISEURAMDXETRAFalse[]None
2STK172603743AMD0.0SMARTEBSCHFAMDAMDFalse[]None
3STK298633607AMD0.0SMARTASXAUDAMDAMDFalse[]None
4STK4391AMD0.0AMEXNASDAQUSDAMDNMSFalse[]None
5STK4391AMD0.0NYSENASDAQUSDAMDNMSFalse[]None
6STK4391AMD0.0CBOENASDAQUSDAMDNMSFalse[]None
7STK4391AMD0.0PHLXNASDAQUSDAMDNMSFalse[]None
8STK4391AMD0.0ISENASDAQUSDAMDNMSFalse[]None
9STK4391AMD0.0CHXNASDAQUSDAMDNMSFalse[]None
10STK4391AMD0.0ARCANASDAQUSDAMDNMSFalse[]None
11STK4391AMD0.0ISLANDNASDAQUSDAMDNMSFalse[]None
12STK4391AMD0.0DRCTEDGENASDAQUSDAMDNMSFalse[]None
13STK4391AMD0.0BEXNASDAQUSDAMDNMSFalse[]None
14STK4391AMD0.0BATSNASDAQUSDAMDNMSFalse[]None
15STK4391AMD0.0EDGEANASDAQUSDAMDNMSFalse[]None
16STK4391AMD0.0CSFBALGONASDAQUSDAMDNMSFalse[]None
17STK4391AMD0.0JEFFALGONASDAQUSDAMDNMSFalse[]None
18STK4391AMD0.0BYXNASDAQUSDAMDNMSFalse[]None
19STK4391AMD0.0IEXNASDAQUSDAMDNMSFalse[]None
20STK4391AMD0.0EDGXNASDAQUSDAMDNMSFalse[]None
21STK4391AMD0.0FOXRIVERNASDAQUSDAMDNMSFalse[]None
22STK4391AMD0.0NYSENATNASDAQUSDAMDNMSFalse[]None
23STK4391AMD0.0PSXNASDAQUSDAMDNMSFalse[]None
24STK32596680AMD0.0FWBIBISEURAMDUSSTARSFalse[]None
25STK32596680AMD0.0IBISIBISEURAMDXETRAFalse[]None
26STK32596680AMD0.0BVMEIBISEURAMDAMDFalse[]None
27STK32596680AMD0.0VSEIBISEURAMDAMDFalse[]None
28STK32596680AMD0.0SWBIBISEURAMDXETRAFalse[]None
29STK32596680AMD0.0TGATEIBISEURAMDAMDFalse[]None
30STK32596680AMD0.0GETTEXIBISEURAMDUSSTARSFalse[]None
31STK48818298AMD0.0MEXIMEXIMXNAMDAMDFalse[]None
32STK172603743AMD0.0EBSEBSCHFAMDAMDFalse[]None
33STK298633607AMD0.0ASXASXAUDAMDAMDFalse[]None
34STK298633607AMD0.0ASXCENASXAUDAMDAMDFalse[]None
\n", "
" ], "text/plain": [ " secType conId symbol lastTradeDateOrContractMonth strike right \\\n", "0 STK 4391 AMD 0.0 \n", "1 STK 32596680 AMD 0.0 \n", "2 STK 172603743 AMD 0.0 \n", "3 STK 298633607 AMD 0.0 \n", "4 STK 4391 AMD 0.0 \n", "5 STK 4391 AMD 0.0 \n", "6 STK 4391 AMD 0.0 \n", "7 STK 4391 AMD 0.0 \n", "8 STK 4391 AMD 0.0 \n", "9 STK 4391 AMD 0.0 \n", "10 STK 4391 AMD 0.0 \n", "11 STK 4391 AMD 0.0 \n", "12 STK 4391 AMD 0.0 \n", "13 STK 4391 AMD 0.0 \n", "14 STK 4391 AMD 0.0 \n", "15 STK 4391 AMD 0.0 \n", "16 STK 4391 AMD 0.0 \n", "17 STK 4391 AMD 0.0 \n", "18 STK 4391 AMD 0.0 \n", "19 STK 4391 AMD 0.0 \n", "20 STK 4391 AMD 0.0 \n", "21 STK 4391 AMD 0.0 \n", "22 STK 4391 AMD 0.0 \n", "23 STK 4391 AMD 0.0 \n", "24 STK 32596680 AMD 0.0 \n", "25 STK 32596680 AMD 0.0 \n", "26 STK 32596680 AMD 0.0 \n", "27 STK 32596680 AMD 0.0 \n", "28 STK 32596680 AMD 0.0 \n", "29 STK 32596680 AMD 0.0 \n", "30 STK 32596680 AMD 0.0 \n", "31 STK 48818298 AMD 0.0 \n", "32 STK 172603743 AMD 0.0 \n", "33 STK 298633607 AMD 0.0 \n", "34 STK 298633607 AMD 0.0 \n", "\n", " multiplier exchange primaryExchange currency localSymbol tradingClass \\\n", "0 SMART NASDAQ USD AMD NMS \n", "1 SMART IBIS EUR AMD XETRA \n", "2 SMART EBS CHF AMD AMD \n", "3 SMART ASX AUD AMD AMD \n", "4 AMEX NASDAQ USD AMD NMS \n", "5 NYSE NASDAQ USD AMD NMS \n", "6 CBOE NASDAQ USD AMD NMS \n", "7 PHLX NASDAQ USD AMD NMS \n", "8 ISE NASDAQ USD AMD NMS \n", "9 CHX NASDAQ USD AMD NMS \n", "10 ARCA NASDAQ USD AMD NMS \n", "11 ISLAND NASDAQ USD AMD NMS \n", "12 DRCTEDGE NASDAQ USD AMD NMS \n", "13 BEX NASDAQ USD AMD NMS \n", "14 BATS NASDAQ USD AMD NMS \n", "15 EDGEA NASDAQ USD AMD NMS \n", "16 CSFBALGO NASDAQ USD AMD NMS \n", "17 JEFFALGO NASDAQ USD AMD NMS \n", "18 BYX NASDAQ USD AMD NMS \n", "19 IEX NASDAQ USD AMD NMS \n", "20 EDGX NASDAQ USD AMD NMS \n", "21 FOXRIVER NASDAQ USD AMD NMS \n", "22 NYSENAT NASDAQ USD AMD NMS \n", "23 PSX NASDAQ USD AMD NMS \n", "24 FWB IBIS EUR AMD USSTARS \n", "25 IBIS IBIS EUR AMD XETRA \n", "26 BVME IBIS EUR AMD AMD \n", "27 VSE IBIS EUR AMD AMD \n", "28 SWB IBIS EUR AMD XETRA \n", "29 TGATE IBIS EUR AMD AMD \n", "30 GETTEX IBIS EUR AMD USSTARS \n", "31 MEXI MEXI MXN AMD AMD \n", "32 EBS EBS CHF AMD AMD \n", "33 ASX ASX AUD AMD AMD \n", "34 ASXCEN ASX AUD AMD AMD \n", "\n", " includeExpired secIdType secId comboLegsDescrip comboLegs \\\n", "0 False [] \n", "1 False [] \n", "2 False [] \n", "3 False [] \n", "4 False [] \n", "5 False [] \n", "6 False [] \n", "7 False [] \n", "8 False [] \n", "9 False [] \n", "10 False [] \n", "11 False [] \n", "12 False [] \n", "13 False [] \n", "14 False [] \n", "15 False [] \n", "16 False [] \n", "17 False [] \n", "18 False [] \n", "19 False [] \n", "20 False [] \n", "21 False [] \n", "22 False [] \n", "23 False [] \n", "24 False [] \n", "25 False [] \n", "26 False [] \n", "27 False [] \n", "28 False [] \n", "29 False [] \n", "30 False [] \n", "31 False [] \n", "32 False [] \n", "33 False [] \n", "34 False [] \n", "\n", " deltaNeutralContract \n", "0 None \n", "1 None \n", "2 None \n", "3 None \n", "4 None \n", "5 None \n", "6 None \n", "7 None \n", "8 None \n", "9 None \n", "10 None \n", "11 None \n", "12 None \n", "13 None \n", "14 None \n", "15 None \n", "16 None \n", "17 None \n", "18 None \n", "19 None \n", "20 None \n", "21 None \n", "22 None \n", "23 None \n", "24 None \n", "25 None \n", "26 None \n", "27 None \n", "28 None \n", "29 None \n", "30 None \n", "31 None \n", "32 None \n", "33 None \n", "34 None " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "util.df(contracts)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can see from this that AMD trades in different currencies on different exchanges.\n", "Suppose we want the one in USD on the SMART exchange. The AMD contract is adjusted to\n", "reflect that and becomes unique:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "amd = Stock('AMD', 'SMART', 'USD')\n", "\n", "assert len(ib.reqContractDetails(amd)) == 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lets try the same for Intel:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "intc = Stock('INTC', 'SMART', 'USD')\n", "\n", "assert len(ib.reqContractDetails(intc)) == 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's try a non-existing contract:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Error 200, reqId 6: No security definition has been found for the request, contract: Stock(symbol='XXX', exchange='SMART', currency='USD', comboLegs=[])\n" ] } ], "source": [ "xxx = Stock('XXX', 'SMART', 'USD')\n", "\n", "assert len(ib.reqContractDetails(xxx)) == 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or a Forex contract" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "eurusd = Forex('EURUSD')\n", "\n", "assert len(ib.reqContractDetails(eurusd)) == 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With the ``qualifyContracts`` method the extra information that is send back\n", "from the contract details request is used to fill in the original contracts.\n", "\n", "Lets do that with ``amd`` and compare before and aftwards:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Stock(symbol='AMD', exchange='SMART', currency='USD', comboLegs=[])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "amd" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Stock(conId=4391, symbol='AMD', exchange='SMART', primaryExchange='NASDAQ', currency='USD', localSymbol='AMD', tradingClass='NMS', comboLegs=[])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ib.qualifyContracts(amd)\n", "amd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**TIP:** When printing a contract, the output can be copy-pasted and it will be valid Python code.\n", "\n", "The ``conId`` that is returned can by itself be used to uniquely specify a contract:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "contract_4391 = Contract(conId=4391)\n", "\n", "ib.qualifyContracts(contract_4391)\n", "\n", "assert contract_4391 == amd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A whole bunch of contracts can be qualified at the same time. A list of all the successfull ones is returned:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Error 200, reqId 12: No security definition has been found for the request, contract: Stock(symbol='XXX', exchange='SMART', currency='USD', comboLegs=[])\n", "Unknown contract: Stock(symbol='XXX', exchange='SMART', currency='USD', comboLegs=[])\n" ] } ], "source": [ "qualContracts = ib.qualifyContracts(amd, intc, xxx, eurusd)\n", "\n", "assert intc in qualContracts\n", "assert xxx not in qualContracts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is also an API function to request stocks (only stocks) that match a pattern:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[ContractDescription(contract=Contract(secType='STK', conId=38709539, symbol='INTC', primaryExchange='MEXI', currency='MXN', comboLegs=[]), derivativeSecTypes=[]),\n", " ContractDescription(contract=Contract(secType='STK', conId=270639, symbol='INTC', primaryExchange='NASDAQ.NMS', currency='USD', comboLegs=[]), derivativeSecTypes=['CFD', 'OPT', 'IOPT', 'WAR', 'FUT']),\n", " ContractDescription(contract=Contract(secType='STK', conId=12178234, symbol='INTC', primaryExchange='EBS', currency='CHF', comboLegs=[]), derivativeSecTypes=[]),\n", " ContractDescription(contract=Contract(secType='STK', conId=11463493, symbol='INTCUSD', primaryExchange='EBS', currency='USD', comboLegs=[]), derivativeSecTypes=[]),\n", " ContractDescription(contract=Contract(secType='STK', conId=130861844, symbol='S', primaryExchange='MEXI', currency='MXN', comboLegs=[]), derivativeSecTypes=[]),\n", " ContractDescription(contract=Contract(secType='STK', conId=130861851, symbol='2S7', primaryExchange='FWB', currency='EUR', comboLegs=[]), derivativeSecTypes=[]),\n", " ContractDescription(contract=Contract(secType='STK', conId=130651996, symbol='S', primaryExchange='NYSE', currency='USD', comboLegs=[]), derivativeSecTypes=['CFD', 'OPT', 'FUT']),\n", " ContractDescription(contract=Contract(secType='STK', conId=14015710, symbol='4613', primaryExchange='TSEJ', currency='JPY', comboLegs=[]), derivativeSecTypes=['CFD']),\n", " ContractDescription(contract=Contract(secType='STK', conId=257765562, symbol='603737', primaryExchange='SEHKNTL', currency='CNH', comboLegs=[]), derivativeSecTypes=[]),\n", " ContractDescription(contract=Contract(secType='STK', conId=257310905, symbol='002436', primaryExchange='SEHKSZSE', currency='CNH', comboLegs=[]), derivativeSecTypes=[]),\n", " ContractDescription(contract=Contract(secType='STK', conId=234809383, symbol='FING.B', primaryExchange='SFB', currency='SEK', comboLegs=[]), derivativeSecTypes=['CFD', 'OPT', 'IOPT']),\n", " ContractDescription(contract=Contract(secType='STK', conId=234809384, symbol='FPQ1', primaryExchange='FWB2', currency='EUR', comboLegs=[]), derivativeSecTypes=['IOPT']),\n", " ContractDescription(contract=Contract(secType='STK', conId=83364054, symbol='FRP', primaryExchange='VALUE', currency='USD', comboLegs=[]), derivativeSecTypes=[]),\n", " ContractDescription(contract=Contract(secType='STK', conId=169544707, symbol='ECC', primaryExchange='NYSE', currency='USD', comboLegs=[]), derivativeSecTypes=[]),\n", " ContractDescription(contract=Contract(secType='STK', conId=96816938, symbol='E16', primaryExchange='SGX', currency='USD', comboLegs=[]), derivativeSecTypes=[]),\n", " ContractDescription(contract=Contract(secType='STK', conId=90691307, symbol='1151', primaryExchange='SEHK', currency='HKD', comboLegs=[]), derivativeSecTypes=[]),\n", " ContractDescription(contract=Contract(secType='STK', conId=173148149, symbol='JYNT', primaryExchange='NASDAQ.SCM', currency='USD', comboLegs=[]), derivativeSecTypes=['OPT']),\n", " ContractDescription(contract=Contract(secType='STK', conId=392988575, symbol='1JO', primaryExchange='FWB2', currency='EUR', comboLegs=[]), derivativeSecTypes=[]),\n", " ContractDescription(contract=Contract(secType='STK', conId=131085231, symbol='4621', primaryExchange='TSEJ', currency='JPY', comboLegs=[]), derivativeSecTypes=[])]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matches = ib.reqMatchingSymbols('intc')\n", "matchContracts = [m.contract for m in matches]\n", "\n", "matches" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "assert intc in matchContracts" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "ib.disconnect()" ] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 4 }