根据市场数据设置限价 - IB Python API

Set limit price based on market data - IB Python API

我正在尝试向互动经纪商下达限价订单 python api。限价以延迟的市场价格为准。代码如下:

class TestApp(EWrapper, EClient):

    def __init__(self):
        EClient.__init__(self, self)

    def tickPrice(self, reqId, tickType, price, attrib):
        if tickType == 67 and reqId == 1:
            print('The current ask price is: ', price)

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

    def start(self):
        contract = Contract()
        contract.symbol = 'DAI'
        contract.secType = 'STK'
        contract.exchange = 'SMART'
        contract.currency = "EUR"
        contract.primaryExchange = "SMART"

        self.reqMarketDataType(3)
        self.reqMktData(1, contract, '', False, False, [])
        time.sleep(1) # allows time for incoming price data

        order = Order()
        order.action = "Buy"
        order.totalQuantity = 1
        order.orderType =  "LMT"
        order.lmtPrice = (self.tickPrice(price) * 1.01)

        self.placeOrder(self.nextOrderId, contract, order)
        self.disconnect()

def main():
    app = TestApp()
    app.nextOrderId = 1
    app.connect('127.0.0.1', 7497, 12)
    app.run()

if __name__ == "__main__":
    main()

我总是收到错误消息:NameError: name 'price' is not defined for order.lmtPrice.

如何从 tickPrice 函数中获取价格属性以将其用于我的价格限制?

此外,如果有任何代码建议可以用更准确的内容替换 time.sleep(1),我将不胜感激。

我对您关于正确程序流程的一个问题发表了评论,这里有一个例子。我还没有在真实连接上测试过,所以...

from ibapi.wrapper import EWrapper
from ibapi.client import EClient
from ibapi.contract import Contract
from ibapi.order import Order


contract = Contract()
contract.symbol = 'DAI'
contract.secType = 'STK'
contract.exchange = 'SMART'
contract.currency = "EUR"
contract.primaryExchange = "SMART"

class TestApp(EWrapper, EClient):

    def __init__(self):
        EClient.__init__(self, self)
        self.data = []
        self.workingOrders = {}
        
    def error(self, reqId, errorCode, errorString):
        print("Error Id:", reqId, "Code:", errorCode, "Msg:", errorString)
        
    def nextValidId(self, orderId):
        self.nextOrderId = orderId
        self.start()

    def start(self):
        self.reqMarketDataType(3)
        self.reqMktData(1, contract, '', False, False, [])
        
    def tickPrice(self, reqId, tickType, price, attrib):
        if tickType == 67 and reqId == 1:
            print('The current ask price is: ', price)
            self.checkLogic(price)
            
    def checkLogic(self, ask):
        self.data.append(ask)
        if self.workingOrders: return
            
        if ask > 10:
            order = Order()
            order.totalQuantity = 1
            order.orderType =  "LMT"
            order.action = "Buy"
            # should lower lmt for buy and you have to round to min price increment
            order.lmtPrice = ask # * 1.01
            print('placing ord', order)
            self.placeOrder(self.nextOrderId, contract, order)
            self.workingOrders[self.nextOrderId] = order
            self.nextOrderId += 1
        elif ask < 5:
            print('too low')
            
    def orderStatus(self, orderId, status, filled, remaining, avgFillPrice, permId,
                    parentId, lastFillPrice, clientId, whyHeld, mktCapPrice):         
        print(orderId, status)
        if status == 'Filled':
            print (self.workingOrders.pop(orderId, "??"))
            self.stop()
            
    def stop(self):
        print('bye')
        self.disconnect()
        
def main():
    app = TestApp()
###### simulate connection 
    app.nextValidId(100)
    app.tickPrice(1, 67, 2, None)
    app.tickPrice(1, 67, 20, None)
    app.tickPrice(1, 67, 21, None)
    app.orderStatus(100, 'Submitted', 1,1,1,1,1,1,1,1,1)
    app.orderStatus(100, 'Filled', 1,1,1,1,1,1,1,1,1)
######    
    # app.connect('127.0.0.1', 7497, 12)
    # app.run()

if __name__ == "__main__":
    main()