【问题标题】:Set limit price based on market data - IB Python API根据市场数据设置限价 - IB Python API
【发布时间】:2021-10-31 12:01:02
【问题描述】:

我正在尝试使用交互式经纪人 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),我将非常感谢。

【问题讨论】:

    标签: python algorithmic-trading interactive-brokers


    【解决方案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()
    

    【讨论】:

    • 嗨,Brian,我在真实连接上进行了测试,它工作正常。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2020-03-05
    • 2022-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    相关资源
    最近更新 更多