Python API will print the first time only

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()
 
and app.cancelHistorialData() as well. I still get the same thing.
The code runs with no errors.
There is apparently one more thing that you don't know yet about IB's API. Certain methods provide information you request and then end by themselves. reqHistoricalData() is one of those. You request a certain amount of historical data. IB provides that historical data and then the task ends. Unless you change your mind halfway: for that case IB offers the method to close out early, with cancelHistoricalData(). But usually it is not necessary to use that method.
Other methods are subscription type methods. You subscribe to these and you will get updates forever, until the moment you discontinue your subscription. reqMktData() is an example of this type of method. This explains why you need to unsubscribe from reqMktData() but you don't need to use reqHistoricalData().
Running a test case with reqHistoricalData() is therefore very different from running a test case with reqMktData().
 
There is apparently one more thing that you don't know yet about IB's API. Certain methods provide information you request and then end by themselves. reqHistoricalData() is one of those. You request a certain amount of historical data. IB provides that historical data and then the task ends. Unless you change your mind halfway: for that case IB offers the method to close out early, with cancelHistoricalData(). But usually it is not necessary to use that method.
Other methods are subscription type methods. You subscribe to these and you will get updates forever, until the moment you discontinue your subscription. reqMktData() is an example of this type of method. This explains why you need to unsubscribe from reqMktData() but you don't need to use reqHistoricalData().
Running a test case with reqHistoricalData() is therefore very different from running a test case with reqMktData().

Ok I see, thank you.

It now seems to work when I change the Client ID to a random number. I was trying with just changing it from 0 to 1 before but maybe 1 is already used?

Anyway if I just put 110 or something instead it works now.

I still haven't gotten it to work with the cancelreq and disconnect() functions so I wouldn't have to change the clientID every time.

Thanks for your help, you were right the first time.
 
Good to see that you found a working solution. Good luck with this "Alice in Wonderland API". You will find surprises and should always expect the unexpected.
 
Back
Top