Not sure why a threading error is occurring using TWS API while trying to get reqcontractDetails and reqPnlSingle

14 Views Asked by At

Below is my code for the errors that is below that. I think it has to do with the fact I am calling the first request then calling the second, but the variables are not correct. The error seems the be with threading and the only thing I can think of is to use event threading to make sure I get the contract id. Am I on the right track? Any help is appreciated.

subscription_requests_postion_pnl = {}

master_sub_req = 0

class TradingApp(EWrapper, EClient):
    def __init__(self):
        EClient.__init__(self,self)

    def position(self, account, contract, position, avgCost):
        position_event.set()

        super().position(account, contract, position, avgCost)

        print("Position.", "Account:", account, "Symbol:", contract.symbol, "SecType:",
              contract.secType, "Currency:", contract.currency,
              "Position:", decimalMaxString(position), "Avg cost:", floatMaxString(avgCost))

        subscribe_pnl_for_position_symbol(contract.symbol)

    def pnlSingle(self, reqId, pos, dailyPnL, unrealizedPnL, realizedPnL, value):

        super().pnlSingle(reqId, pos, dailyPnL, unrealizedPnL, realizedPnL, value)

        print("PnL for contract ID {} = {}".format(contract_id, dailyPnL))

    def contractDetails(self, reqId, contractDetails):
         
        global contract_id

        contract_details_event.set()

        print(contractDetails)

        contract_id = int(str(contractDetails.contract).split(",")[0])

def websocket_con():
    app.run()


app = TradingApp()

app.connect()

# starting a separate daemon thread to execute the websocket connection
con_thread = threading.Thread(target=websocket_con, daemon=True)
con_thread.start()
time.sleep(1) # some latency added to ensure that the connection is established

position_event = threading.Event()

pnl_single_event = threading.Event()

contract_details_event = threading.Event()

#position_event.set(), .clear(), .wait()

app.reqPositions()

def subscribe_pnl_for_position_symbol(app, symbol):

    global master_sub_req

    contract = Contract()
    contract.symbol = symbol
    contract.secType = "STK"
    contract.currency = "USD"
    contract.exchange = 'SMART'

    master_sub_req += 1

    print('contract details')

    print(contract)

    app.reqContractDetails(master_sub_req, contract)

    contract_details_event.wait()

    print('contract details end')
    
    time.sleep(1)

    master_sub_req += 1

    app.reqPnLSingle(master_sub_req, "DU7058034", "", contract_id)
                    
    time.sleep(1)

    subscription_requests_postion_pnl.update({symbol:[master_sub_req]})

I get this error below!

Exception in thread Thread-2 (websocket_con):
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\threading.py", line 1016, in _bootstrap_inner
0

There are 0 best solutions below