4

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)
Joe
  • 61
  • 2
  • 7
  • You will get better answers (and maybe the one you are searching for has already been answered) in the TWSAPI group in the groups.io website. Search for it in Google. – Ezarate11 Dec 30 '18 at 12:53
  • could not find out what is causing this error despite visiting that group though – Joe Dec 31 '18 at 12:05
  • This question is more likely to be answered in the group, not in SO, primarily because the experienced users are present there. – Ezarate11 Dec 31 '18 at 14:34

2 Answers2

0

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.

brian
  • 10,619
  • 4
  • 21
  • 79
  • Thanks for the suggestion. It took me for a while to understand the information/warning vs. error. Sounds like it is just an informal message. In IB Broker, SMART is the routing exchange, primary exchange we use instead of AMEX, NASDAQ etc.Somehow the code is working now but I still can't see the order in either the trade log or order history in TWS... – Joe Jan 01 '19 at 04:39
  • Are you still calling conn.disconnect? Once you do that the api connection is broken. Also, I would suggest using the official python API (version>9.73.01) from IB now. http://interactivebrokers.github.io/# – brian Jan 01 '19 at 12:00
  • Hi Brian - I'm actually using their latest API version. I did remove the conn.disconnect() method from the code. However, it is still not showing any order in the order book. I've posted the updated code above. – Joe Jan 05 '19 at 09:22
  • @Joe No, you're using IBPy. https://github.com/blampe/IbPy Only use that if you have to use python 2. – brian Jan 30 '19 at 19:37
0

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()
Aqueous Carlos
  • 445
  • 7
  • 20
  • Hi Carlos - Though it worked but I still can't see the orders in the order screen of IB. – Joe Jan 29 '19 at 14:47