Reputation: 479
I have following sample code, when I try to run it for the first time, it worked:
from ib.opt import Connection, message
from ib.ext.Contract import Contract
from ib.ext.Order import Order
def make_contract(symbol, sec_type, exch, prim_exch, curr):
Contract.m_symbol = symbol
Contract.m_secType = sec_type
Contract.m_exchange = exch
Contract.m_primaryExch = prim_exch
Contract.m_currency = curr
return Contract
def make_order(action,quantity, price = None):
if price is not None:
order = Order()
order.m_orderType = 'LMT'
order.m_totalQuantity = quantity
order.m_action = action
order.m_lmtPrice = price
else:
order = Order()
order.m_orderType = 'MKT'
order.m_totalQuantity = quantity
order.m_action = action
return order
cid = 100
while __name__ == "__main__":
conn = Connection.create(port=7496, clientId=999)
conn.connect()
oid = cid
cont = make_contract('AAPL', 'STK', 'SMART', 'SMART', 'USD')
offer = make_order('BUY', 1, 200)
conn.placeOrder(oid, cont, offer)
conn.disconnect()
x = raw_input('enter to resend')
cid += 1
as i run the script for the first time, the interface of IB pops up a window and says configuration information for paper trading from API. However the second, third time I run it, the pop up information never shows up again which confuse me. Is there anything wrong here?
Upvotes: 1
Views: 1993
Reputation: 735
As already mentioned it should be if name == "main":
What you are doing there is running an infinite loop that connects to the IB API, places an order, disconnects and repeats the same process.
The popup is probably one of the API warnings that once accepted doesn't show up again so that explains why you don't see it again.
Your order is quite likely placed and should show up in TWS unless it's resulting in an error which you won't see in TWS.
As others have alluded to what you need to do is firstly not use iPython notebook as it's not going to give you a good view of what's going on. Change your code to look like this and you'll be able to see what's going on:
from ib.opt import Connection, message
from ib.ext.Contract import Contract
from ib.ext.Order import Order
import time
def make_contract(symbol, sec_type, exch, prim_exch, curr):
Contract.m_symbol = symbol
Contract.m_secType = sec_type
Contract.m_exchange = exch
Contract.m_primaryExch = prim_exch
Contract.m_currency = curr
return Contract
def make_order(action,quantity, price = None):
if price is not None:
order = Order()
order.m_orderType = 'LMT'
order.m_totalQuantity = quantity
order.m_action = action
order.m_lmtPrice = price
else:
order = Order()
order.m_orderType = 'MKT'
order.m_totalQuantity = quantity
order.m_action = action
return order
cid = 100
def handleAll(msg):
print msg
if __name__ == "__main__":
conn = Connection.create(port=7496, clientId=999)
conn.connect()
conn.registerAll(handleAll)
oid = cid
cont = make_contract('AAPL', 'STK', 'SMART', 'SMART', 'USD')
offer = make_order('BUY', 1, 200)
conn.placeOrder(oid, cont, offer)
while 1:
time.sleep(1)
Upvotes: 1