I keep getting the below errors while testing the below code. I've a paper trading account with IB.
Not sure what exactly these errors are. Tried searching online but could not get any hint.
from ib.opt import Connection, message
from ib.ext.Contract import Contract as C
from ib.ext.Order import Order
import time
def make_contract(symbol,sec_type,exch,prim_exch,curr):
C.m_symbol=symbol
C.m_secType=sec_type
C.m_exch=exch
C.m_primaryExch=prim_exch
C.m_currency=curr
return C
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
print(price)
else:
order=Order()
order.m_orderType = 'MKT'
order.m_totalQuantity = quantity
order.m_action = action
print('hi')
return order
def handleAll(msg):
print(msg)
cid = 103
conn = Connection.create(port=7497) #clietnID=888)
conn.connect()
conn.registerAll(handleAll)
oid = cid
cont = make_contract('AAPL','STK', 'SMART','SMART', 'USD')
offer = make_order('BUY', 1, 157)
conn.placeOrder(oid,cont,offer)
while 1:
time.sleep(1)
That's not really an error, just information saying you're connected to market data. However, the conn.disconnect() disconnects before you can do anything.
Also the primary exchange for AAPL is not SMART. You don't need to put one except in rare cases where the symbol is ambiguous and it's never SMART.
Please add if __name__ == "__main__":
in order to run order placing smoothly. By the way, the primary exchange for AAPL is not SMART, you may leave it empty as ''
for the most of US stocks.
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 = 103
def handleAll(msg):
print(msg)
if __name__ == "__main__":
conn = Connection.create(port=4002, clientId=103)
conn.connect()
conn.registerAll(handleAll)
oid = cid
cont = make_contract('AAPL', 'STK', 'SMART', 'ISLAND', 'USD')
offer = make_order('BUY', 1, 200)
conn.placeOrder(oid, cont, offer)
time.sleep(1)
conn.disconnect()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.