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.
JavaScript
x
54
54
1
`__author__ = 'noone'
2
3
from Testbed.OrderSamples import OrderSamples
4
from ibapi.client import EClient
5
from ibapi.wrapper import EWrapper
6
from ibapi.common import *
7
from ibapi.contract import *
8
9
class OrderApp(EWrapper, EClient):
10
def __init__(self):
11
EClient.__init__(self,self)
12
13
def error(self,reqId:TickerId, errorCode:int, errorString:str):
14
print("Error:",reqId," ", errorCode, " ", errorString)
15
16
# This is the initial response after connection to TS from the API providing next available OrderID
17
@iswrapper
18
def nextValidId(self, orderId: int):
19
super().nextValidId(orderId)
20
21
print("setting nextValidOrderId: %d", orderId)
22
self.nextValidOrderId = orderId
23
24
def contractDetails(self, reqId:int, contractDetails:ContractDetails):
25
print("contractDetails: ", reqId, " ", contractDetails)
26
27
def main():
28
app=OrderApp()
29
app.connect("127.0.0.1",7497,0)
30
31
## Build the contract object to be passed to the order Method
32
33
stock_contract = Contract()
34
stock_contract.symbol = 'AAPL'
35
stock_contract.secType = 'STK'
36
stock_contract.exchange = 'SMART'
37
stock_contract.currency = 'USD'
38
stock_contract.primaryExchange = 'NASDAQ'
39
40
# reqID must be provided to the Order. This method gets the reqID from IB DB's
41
app.reqContractDetails(10, stock_contract)
42
43
try:
44
# Now place the order in Paper Money Account
45
app.placeOrder(126, stock_contract, OrderSamples.LimitOrder("BUY", 50, 12))
46
47
except:
48
raise
49
50
app.run()
51
52
if __name__ == "__main__":
53
main()`
54
Advertisement
Answer
Your method is not syntactically correct. It should have indentation as below
JavaScript
1
7
1
@iswrapper
2
def nextValidId(self, orderId: int):
3
super().nextValidId(orderId)
4
5
print("setting nextValidOrderId: %d" % orderId)
6
self.nextValidOrderId = orderId
7
Also you are not importing the function decorator iswrapper
JavaScript
1
2
1
from ibapi.utils import iswrapper
2
Hope this helps