I am having trouble using and incrementing the "orderId" for each stock order using modified IB example code. I have a method that produces nextValidId within the class and prints it to stdout but I am not sure how to access the defined property (self.nextValidOrderId) that is created in my main() program body. I am able to instantiate the EWrapper and EClient Class and place orders (If I manually input the orderId). In example code I hard code it to 126. I thought I would be able to use the following in my main() program body. "orderId = app.nextValidOrderId" but it does not work.
`__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()`
答案 0 :(得分:1)
您的方法在语法上不正确。它应该有缩进,如下所示
@iswrapper
def nextValidId(self, orderId: int):
super().nextValidId(orderId)
print("setting nextValidOrderId: %d", orderId)
self.nextValidOrderId = orderId
此外,您没有导入函数装饰器iswrapper
from ibapi.utils import iswrapper
希望这有帮助