I created a thread in the main code. Inside the thread I call a procedure outside the thread. The called procedure runs fine except a line that's supposed to run an ib order, which throws the error "There is no current event loop in thread 'Thread-6(--)'"
I've tried ensuring the ib global variable is writable in the called procedure but it doesn't help. See below code.
#Global variables
ib = ""
class StartRunning():
def __init__(self):
print("Starting up...")
def myMain()
global ib
ib = IB()
ib.connect("127.0.0.1", port, clientId)
def SaleProcess():
try:
...................
StartRunning.ProcessSS(x)
except Exception as ex:
print("SaleProcess: ", ex)
pass
def ProcessSS(x):
try:
StartRunning.RunOrder()
except Exception as ex:
print("Error in ProcessSS: ", ex)
return False
def RunOrder():
global ib
try:
theOrder = Order()
theOrder.orderId = ib.client.getReqId()
theOrder.action = theAction
thePrice = StartRunning.GetPriceVolGap(theSymbol)
theOrder.auxPrice = getauxPrice() #trailing amount
theOrder.orderType = "TRAIL"
theOrder.totalQuantity = theQuantity
theOrder.transmit = True
theOrder.eTradeOnly = None
theOrder.firmQuoteOnly = None
theOrder.nbboPriceCap = None
contract = Stock(theSymbol, 'SMART', 'USD')
ib.placeOrder(contract, theOrder)
return True
except Exception as ex:
print("Error in RunOrder: ", ex)
return False
#MAIN STRUCTURE
def main():
try:
while True:
thread1 = Thread(target=StartRunning.SaleProcess)
thread1.start()
StartRunning.MyMain()
except Exception as ex:
print("Error in main: ", ex)
pass
if __name__ == "__main__":
main() `