Bobmandude
Bobmandude

Reputation: 1

Disconnection problems with IB api

I have this code for the IBapi where I just want to buy a contract for a given stock, and then disconnect, but it seems like it stays connected after I send the first contract. I tried using disconnect and even cancelOrder, but neither seem to actually disconnect from TWS

Every time I want to send a new order I have to close and reopen TWS.

from ibapi.client import *
from ibapi.wrapper import *

import time
import threading
class TestApp(EClient, EWrapper):
    def __init__(self):
        EClient.__init__(self,self)
    def nextValidId(self,orderId: int, stock: str):
        mycontract = Contract()
        mycontract.symbol = stock
        mycontract.secType = "STK"
        mycontract.exchange = "SMART"
        mycontract.currency = "USD"
        
        self.reqContractDetails(orderId, mycontract)
        return mycontract
        
    def contractDetails(self, reqId: int, contract: Contract):

        myorder = Order()
        myorder.orderId = reqId
        myorder.action = "BUY"
        
        myorder.orderType = "MKT"
        myorder.totalQuantity = 30
        
        self.placeOrder(reqId,contract, myorder)
    def openOrder(self, orderId: OrderId, contract: Contract, order: Order, orderState: OrderState):
        print(f"orderId: {orderId}, contract: {contract}, order: {order}")
              

app = TestApp()
app.connect("127.0.0.1", 7497, 999)

stock = "MARA"

mycontract = Contract()
mycontract.symbol = stock
mycontract.secType = "STK"
mycontract.exchange = "SMART"
mycontract.currency = "USD"

orderid = 1578

myorder = Order()
myorder.orderId = orderid
myorder.action = "BUY"
        
myorder.orderType = "MKT"
myorder.totalQuantity = 120


app.placeOrder(orderid,mycontract, myorder)



time.sleep(3)


app.done = True

app.disconnect()

I tried disconnect and cancelOrder. Neither disconnected properly

Upvotes: 0

Views: 17

Answers (0)

Related Questions