app.run()
starts a thread to read from the socket. Your program never stops running so is always connected to TWS. Try clicking on the data menu button in TWS. It shows all your connections. You will see client 0 stays connected. Obviously closing pyCharm kills the program.
You also request the contract details before the program is ready. You should wait for nextValidId and then send the request. When the data has been recieved, then you can stop your program and it will free up the clientId for future connections. Here is how I would change your program.
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import *
from ibapi.contract import *
from ContractSamples import ContractSamples
class TestApp(EClient, EWrapper):
def __init__(self):
EClient.__init__(self, self)
def nextValidId(self, orderId:int):
print("id", orderId)
contract = Contract()
contract.symbol = "IBKR"
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
contract.primaryExchange = "NASDAQ"
self.reqContractDetails(10, contract)
def error(self, reqId:TickerId, errorCode:int, errorString:str):
print("Error: ", reqId, "", errorCode, "", errorString)
def contractDetails(self, reqId:int, contractDetails:ContractDetails):
print("contractDetail: ", reqId, " ", contractDetails)
def contractDetailsEnd(self, reqId:int):
print("end, disconnecting")
self.disconnect()
def main():
app = TestApp()
app.connect("127.0.0.1", 7496, 0)
app.run()
if __name__ == "__main__":
main()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…