royy
royy

Reputation: 35

Interactive Brokers (IB) Python API: Unable to run the IB tutorial sample on VS Code

I am new to Python, I am using Windows 7 and have downloaded and installed the TWS API (9.76.01) (My TWS is running 972.1), and successfully installed the ibapi python as instructed:

1) python setup.py sdist
2) python setup.py bdist_wheel
3) python -m pip install --user --upgrade dist/ibapi-9.76.01-py3-none-any.whl

I loaded up VS Code and copied the tutorial sample codes from IB: https://cdcdyn.interactivebrokers.com/webinars/TA-2018-TWS-Python-Receiving-Market-Data-Study-Notes.pdf (Youtube video: https://www.youtube.com/watch?v=GmTPDzcko6k)

The sample code I am using is from Page 2 of the document 'Example of requesting streaming market data for AAPL'

Upon running the code, there is no output on the terminal. However, there's a message at the end of the 'main()' on VS Code stating the following ,screenshot:

Unable to import 'ibapi.client'pylint(import-error)
Unable to import 'ibapi.wrapper'pylint(import-error)
Unable to import 'ibapi.contract'pylint(import-error)
Unable to import 'ibapi.ticktype'pylint(import-error)
Undefined variable 'TestApp'pylint(undefined-variable)

Is there something or some steps I missed out? Thank you!

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum


class TestApp(EWrapper, EClient):
 def __init__(self):
 EClient.__init__(self, self)
 def error(self, reqId, errorCode, errorString):
 print("Error: ", reqId, " ", errorCode, " ", errorString)
 def tickPrice(self, reqId, tickType, price, attrib):
 print("Tick Price. Ticker Id:", reqId, "tickType:",
       TickTypeEnum.to_str(tickType), "Price:", price, end=' ')

 def tickSize(self, reqId, tickType, size):
 print("Tick Size. Ticker Id:", reqId, "tickType:",
       TickTypeEnum.to_str(tickType), "Size:", size)


def main():
 app = TestApp()
 app.connect("127.0.0.1", 7497, 0)
 contract = Contract()
 contract.symbol = "AAPL"
 contract.secType = "STK"
 contract.exchange = "SMART"
 contract.currency = "USD"
 contract.primaryExchange = "NASDAQ"
 # switch to delayed-frozen data if live is not available
 app.reqMarketDataType(4)
 app.reqMktData(1, contract, "", False, False, [])
 app.run()


if __name__ == "__main__":
 main()

enter image description here

Upvotes: 1

Views: 1491

Answers (1)

MatthewScarpino
MatthewScarpino

Reputation: 5936

If you look through the TWS API installation, you'll find a folder named ibapi. This contains the Python modules that define the TWS classes that you're missing. You need to set your PYTHONPATH environment variable to include this directory.

Upvotes: 1

Related Questions