[英]Update stop order API Interactive Brokers Python
我在市場上有訂單並驗證了訂單 ID。 我正在嘗試使用下面的代碼更新括號中的停止訂單。 輸出沒有給出任何錯誤,但我的 stp 價格沒有變化? 我已經通過打印命令驗證了正在檢索和正確存儲變量。 如果我將其打印到終端,這是訂單顯示的內容。
3359.75 994.0 0,0,0:STP買入27@179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000 GTC
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()
代碼運行良好,但 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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.