如何使用本机交互式代理Python API创建条件订单?

2024-04-29 19:13:01 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的代码是IB提供的。我该如何扩展这一点来创建一个条件订单,其中两个不同合同的价格可以用在一个条件语句中,来执行所述合同的市场订单?在

例如:if AAPL>SPY, "BUY" AAPL, else: "BUY" SPY...

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum
from ibapi.utils import iswrapper #just for decorator
from ibapi.order import Order
from threading import Timer
import time

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

    def error(self, reqId, errorCode, errorString):
        print("Error: ", reqId, " ", errorCode, " ", errorString)

    def nextValidId(self, orderId):
        self.nextOrderId = orderId
        self.start()

    def nextOrderId(self):
        oid = self.nextValidOrderId
        self.nextValidOrderId +=1
        return oid

    def orderStatus(self, orderId , status:str, filled:float,
                    remaining:float, avgFillPrice:float, permId:int,
                    parentId:int, lastFillPrice:float, clientId:int,
                    whyHeld:str, mktCapPrice: float):
        print(orderId, status, filled, remaining, lastFillPrice)

    def openOrder(self, orderId, contract: Contract, order: Order,
                  orderState):
        print("OpenOrder. ID:", orderId, "Symbol:", contract.symbol, "SecType:", contract.secType,
              "Exchange:", contract.exchange, "Action:", order.action, "OrderType:", order.orderType,
              "TotalQuantity:", order.totalQuantity, "Status:", orderState.status)
    def execDetails(self, reqId: int, contract: Contract, execution):
        print("ExecDetails. ReqId:", reqId, "Symbol:", contract.symbol, "SecType:", contract.secType, "Currency:", contract.currency, execution)

    def tickPrice(self, reqId, tickType, price, attrib):
        print(price)




def main():

    app = TestApp()
    app.connect("port", 7497, id)

    app.run()


if __name__ == "__main__":
    main()

Tags: fromimportselfdefstatusorderfloatint
1条回答
网友
1楼 · 发布于 2024-04-29 19:13:01

本机条件订单(即在IBKR服务器上执行触发下单逻辑的订单)不支持直接比较两种不同工具的价格,例如AAPL和SPY。例如,证券的价格必须与固定的硬编码值进行比较

“如果最后价格是AAPL>;200,请提交购买间谍的订单”

为了熟悉允许的条件语句类型,建议在从twsapi创建订单之前先在Trader工作站中创建订单。在

你可以在API程序中请求多个仪器的流式市场数据,然后在那里实现逻辑来比较不同仪器的价格,并在满足某些条件时提交订单。在

相关问题 更多 >