更新停单API交互代理Python

更新停单API交互代理Python,python,api,Python,Api,我在市场上有订单,并已验证订单ID。我正在尝试使用下面的代码更新括号中的停止订单。输出没有给出任何错误,但我的stp价格没有变化?我已经通过print命令验证了变量是否被正确检索和存储。如果我将订单打印到终端,订单会显示以下内容。 3359.75 994 0,0,0:STP购买27@179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458

我在市场上有订单,并已验证订单ID。我正在尝试使用下面的代码更新括号中的停止订单。输出没有给出任何错误,但我的stp价格没有变化?我已经通过print命令验证了变量是否被正确检索和存储。如果我将订单打印到终端,订单会显示以下内容。
3359.75 994 0,0,0:STP购买27@179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000GTC


        if action == "UpdateStp":
            print("Received Update Stop Email") 
            AquaHiFloat = float(subparse[3])
            AquaLoFloat = float(subparse[4])
            AquaHi = AquaHiFloat 
            AquaLo = AquaLoFloat 
          
###################################Get Original Stop ID & Lot Size           
            print("Retrieve original Stop Id and lotsize from archive") 
            f=open("BuyStopOrder.txt", "r")
            if f.mode == 'r':
                BuyStopId =float(f.read())
                f.close()
            f=open("SellStopOrder.txt", "r")
            if f.mode == 'r':
                SellStopId =float(f.read())
                f.close()
                
            f=open("LotSize.txt", "r")
            if f.mode == 'r':
                lastlots =float(f.read())
                f.close()
            print(lastlots)
            print(SellStopId)


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

                def nextValidId(self, orderId: int):
                    super().nextValidId(orderId)
                    self.nextorderId = orderId
                    print('The next valid order id is: ', self.nextorderId)

                def orderStatus(self, orderId, status, filled, remaining, avgFullPrice, permId, parentId, lastFillPrice, clientId, whyHeld, mktCapPrice):
                    print('orderStatus - orderid:', orderId, 'status:', status, 'filled', filled, 'remaining', remaining, 'lastFillPrice', lastFillPrice)
    
                def openOrder(self, orderId, contract, order, orderState):
                    print('openOrder id:', orderId, contract.symbol, contract.secType, '@', contract.exchange, ':', order.action, order.orderType, order.totalQuantity, orderState.status)

                def execDetails(self, reqId, contract, execution):
                    print('Order Executed: ', reqId, contract.symbol, contract.secType, contract.currency, execution.execId, execution.orderId, execution.shares, execution.lastLiquidity)


            def run_loop():
                app.run()

            app = IBapi()
            app.connect('127.0.0.1', 7497, 123)
            app.nextorderId = None

                #Start the socket in a thread
            api_thread = threading.Thread(target=run_loop, daemon=True)
            api_thread.start()

                #Check if the API is connected via orderid
            while True:
                if isinstance(app.nextorderId, int):
                    print('connected')
                    break
                else:
                    print('waiting for connection')   
        
            if (OpenPos) < 0.0:
                print("Updating Short Postion Stop Order")
                mail=smtplib.SMTP('smtp.gmail.com',587)
                mail.ehlo()
                mail.starttls()
                mail.login(user,pwd)
                message = MIMEMultipart()
                message['From'] = user
                message['To'] = alertmail
                message['Subject'] = "Updated Short Position Stop Order"
                body=""
                message.attach(MIMEText(body, 'plain'))
                sms = message.as_string()
                mail.sendmail(user,alertmail,sms)
                mail.close
                stop_order = Order()
                stop_order.parentId = SellStopId
                stop_order.action = 'BUY'
                stop_order.orderType = 'STP'
                stop_order.auxPrice = AquaHi 
                stop_order.totalQuantity = lastlots
                stop_order.outsideRth = True
                stop_order.tif = 'GTC'
                app.placeOrder(SellStopId, contract, stop_order)
                print(AquaHi)
                print(SellStopId)
                
            if (OpenPos) > 0.0:
                mail=smtplib.SMTP('smtp.gmail.com',587)
                mail.ehlo()
                mail.starttls()
                mail.login(user,pwd)
                message = MIMEMultipart()
                message['From'] = user
                message['To'] = alertmail
                message['Subject'] ="Updated Long Position Stop Order"
                body=""
                message.attach(MIMEText(body, 'plain'))
                sms = message.as_string()
                mail.sendmail(user,alertmail,sms)
                mail.close
                print("Updating Long Position Stop Order")
                stop_order = Order()
                stop_order.parentId = BuyStopId
                stop_order.action = 'SELL'
                stop_order.orderType = 'STP'
                stop_order.auxPrice = AquaLo 
                stop_order.totalQuantity = lastlots
                stop_order.outsideRth = True
                stop_order.tif = 'GTC'      
                app.placeOrder(BuyStopId, contract, stop_order)
                print(AquaLo)
                print("  ")
            if (OpenPos) == 0.0:
                print("No Open Position Stop for Aqua to update")
            done=True
            app.disconnect()

如果操作==“UpdateStp”:
打印(“收到更新停止电子邮件”)
AquaHiFloat=浮球(子面[3])
AquaLoFloat=浮球(子面[4])
AquaHi=AquaHiFloat
AquaLo=AquaLoFloat
###################################获取原始站点ID和批次大小
打印(“从存档中检索原始站点Id和批量”)
f=打开(“BuyStopOrder.txt”、“r”)
如果f.mode='r':
BuyStopId=float(f.read())
f、 关闭()
f=打开(“SellStopOrder.txt”,“r”)
如果f.mode='r':
SellStopId=float(f.read())
f、 关闭()
f=打开(“LotSize.txt”、“r”)
如果f.mode='r':
lastlots=float(f.read())
f、 关闭()
印刷品(最后一批)
打印(SellStopId)
IBapi类(EWrapper,EClient):
定义初始化(自):
EClient.\uuuuu init\uuuuu(self,self)
def nextValidId(self,orderId:int):
super().nextValidId(orderId)
self.nextorderId=orderId
打印('下一个有效订单id为:',self.nextorderId)
def orderStatus(self、orderId、status、filled、remaining、avgFullPrice、permitd、parentId、lastfullprice、clientId、whyHeld、mktCapPrice):
打印('orderStatus-orderid:',orderid,'status:',status,'filled',filled,'remaining',remaining,'lastFillPrice',lastFillPrice)
def openOrder(self、orderId、contract、order、orderState):
打印('openOrder id:',orderId,contract.symbol,contract.secType,'@',contract.exchange,':',order.action,order.orderType,order.totalQuantity,orderState.status)
def执行详情(自我、需求ID、合同、执行):
打印('已执行订单:',需求ID,contract.symbol,contract.secType,contract.currency,execution.execId,execution.orderId,execution.shares,execution.lastLiquidity)
def run_loop():
app.run()
app=IBapi()
app.connect('127.0.0.1',7497123)
app.nextorderId=无
#在线程中启动套接字
api_thread=threading.thread(目标=run_循环,守护进程=True)
api_thread.start()
#检查API是否通过orderid连接
尽管如此:
如果isinstance(app.nextorderId,int):
打印('已连接')
打破
其他:
打印('等待连接')
如果(OpenPos)<0.0:
打印(“更新短位置停止订单”)
mail=smtplib.SMTP('SMTP.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login(用户,pwd)
message=MIMEMultipart()
消息['From']=用户
消息['To']=alertmail
消息['Subject']=“更新的空头仓位止损单”
body=“”
message.attach(MIMEText(正文“普通”))
sms=message.as_string()
mail.sendmail(用户、alertmail、sms)
邮件关闭
停止命令=命令()
stop\u order.parentId=SellStopId
stop_order.action='BUY'
stop_order.orderType='STP'
停止订单。auxPrice=AquaHi
停止订单。总数量=最后批次
stop_order.th=真
stop_order.tif='GTC'
应用程序placeOrder(SellStopId、合同、停止订单)
印刷品(AquaHi)
打印(SellStopId)
如果(OpenPos)>0.0:
mail=smtplib.SMTP('SMTP.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login(用户,pwd)
message=MIMEMultipart()
消息['From']=用户
消息['To']=alertmail
消息['Subject']=“已更新的多头仓位止损单”
body=“”
message.attach(MIMEText(正文“普通”))
sms=message.as_string()
mail.sendmail(用户、alertmail、sms)
邮件关闭
打印(“更新多头仓位停止单”)
停止命令=命令()
stop\u order.parentId=BuyStopId
stop_order.action='SELL'
stop_order.orderType='STP'
停止订单。auxPrice=AquaLo
停止订单。总数量=最后批次
stop_order.th=真
stop_order.tif='GTC'
应用程序placeOrder(购买停止ID、合同、停止订单)
印刷品(AquaLo)
打印(“”)
如果(OpenPos)==0.0:
打印(“Aqua无打开位置停止更新”)
完成=正确
app.disconnect()

代码工作得很好,但OrderID必须是INT而不是Float

        if f.mode == 'r':
            BuyStopId =int(f.read())
            f.close()
        f=open("SellStopOrder.txt", "r")
        if f.mode == 'r':
            SellStopId =int(f.read())
            f.close()

那么,仅仅发送一个新的STP订单就可以更新现有的订单?