【发布时间】: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