Python交互式代理ibapi
我在使用修改后的IB示例代码为每个库存订单使用和增加orderId时遇到问题。我有一个方法在类中生成nextValidId并将其打印到stdout,但我不确定如何访问在主程序体中创建的已定义属性self.nextValidOrderId。如果手动输入orderId,我可以实例化EWrapper和EClient类并下订单。在示例代码中,我将其硬编码为126。我想我可以在我的主要程序中使用以下内容。 orderId=app.nextValidOrderId,但它不起作用Python交互式代理ibapi,python,class,superclass,Python,Class,Superclass,我在使用修改后的IB示例代码为每个库存订单使用和增加orderId时遇到问题。我有一个方法在类中生成nextValidId并将其打印到stdout,但我不确定如何访问在主程序体中创建的已定义属性self.nextValidOrderId。如果手动输入orderId,我可以实例化EWrapper和EClient类并下订单。在示例代码中,我将其硬编码为126。我想我可以在我的主要程序中使用以下内容。 orderId=app.nextValidOrderId,但它不起作用 `__author__ =
`__author__ = 'noone'
from Testbed.OrderSamples import OrderSamples
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.common import *
from ibapi.contract import *
class OrderApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self,self)
def error(self,reqId:TickerId, errorCode:int, errorString:str):
print("Error:",reqId," ", errorCode, " ", errorString)
# This is the initial response after connection to TS from the API providing next available OrderID
@iswrapper
def nextValidId(self, orderId: int):
super().nextValidId(orderId)
print("setting nextValidOrderId: %d", orderId)
self.nextValidOrderId = orderId
def contractDetails(self, reqId:int, contractDetails:ContractDetails):
print("contractDetails: ", reqId, " ", contractDetails)
def main():
app=OrderApp()
app.connect("127.0.0.1",7497,0)
## Build the contract object to be passed to the order Method
stock_contract = Contract()
stock_contract.symbol = 'AAPL'
stock_contract.secType = 'STK'
stock_contract.exchange = 'SMART'
stock_contract.currency = 'USD'
stock_contract.primaryExchange = 'NASDAQ'
# reqID must be provided to the Order. This method gets the reqID from IB DB's
app.reqContractDetails(10, stock_contract)
try:
# Now place the order in Paper Money Account
app.placeOrder(126, stock_contract, OrderSamples.LimitOrder("BUY", 50, 12))
except:
raise
app.run()
if __name__ == "__main__":
main()`
您的方法在语法上不正确。它应该有如下缩进
@iswrapper
def nextValidId(self, orderId: int):
super().nextValidId(orderId)
print("setting nextValidOrderId: %d", orderId)
self.nextValidOrderId = orderId
另外,您没有导入函数decorator iswrapper
from ibapi.utils import iswrapper
希望这有帮助