Your main() should do four tasks:
(1) connect to TWS, using app.connect()
(2) subscribe to market data and print it on screen, using app.reqMktData()
using some sleep command you let this run for a while.
(3) end the subscription to the market data, using app.cancelMktData()
(4) disconnect from TWS, using app.disconnect()
only then can you end your main() in a decent manner.
In case you haven't found it yet, here is the available documentation as provided by IB: https://interactivebrokers.github.io/tws-api/index.html
Yes and I'm following the IB tutorial to the letter from youtube here:
I've updated my code with the app.disconnect() and app.cancelHistorialData() as well. I still get the same thing.
The code runs with no errors.
In TWS shows no connections prior to running the script, then it shows "disconnected" from client ID 0.
And that's it. Doesn't print anything.
Code:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.common import *
class TestApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self,self)
def error(self, reqId: TickerId, errorCode: int, errorString: str):
super().error(reqId, errorCode, errorString)
print("Error. Id:", reqId, "Code:", errorCode, "Msg:", errorString)
def hisotricalData(self, reqId, bar):
print("HistoricalData:", reqId, "Date:", bar.date,
"Open:", bar.open, "High:", bar.high, "Low:", bar.low,
"Close", bar.close, "Volume:", bar.volume, "Count:", bar.count)
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'
app.reqHistoricalData(1, contract, "", "1 D", "1 min", "MIDPOINT", 0, 1, False, [])
app.cancelHistoricalData(1)
app.run()
app.disconnect()
if __name__ == "__main__":
main()