-1
from ib.ext.Contract import Contract

from ib.ext.Order import Order

from ib.opt import Connection, message

#from ib.lib.overloading import overloaded

def make_contract(symbol, sec_type, exch, prim_exch, curr):
    contract = Contract()
    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 = qiantity
        order.m_action = action
        order.m_lmtPrice = price

    else:
        order = Order()
        order.m_orderType = 'MKT'
        order.m_totalQuantity = qiantity
        order.m_action = action

    return order

#def error_handler(msg):
    #print'Server Error: ',msg

#def server_handler(msg):
    #print'Server Msg: ',msg.typeName,'-',msg


def main():
    conn = Connection.create(port=7496, clientId=100) #clientID's given are 100 or 999 
    conn.connect()

    oid = 1
    cont = make_contract('TSLA', 'STK', 'SMART', 'SMART', 'USD')
    offer = make_order('BUY', 1) #or('BUY', 1, 200)

    conn.placeOrder(oid, cont, offer)

    conn.disconnect()

Getting this error and have no idea why however I am sure I installed the IBpy lib correctly but please correct me if I am wrong :)....any suggestions?:

  File "C:\Users\Heraaizon\Desktop\IBPy_Test.py", line 1, in <module>
    from ib.ext.Contract import Contract


  File "C:\Users\Heraaizon\AppData\Local\Programs\Python\Python37-32\lib\ib\ext\Contract.py", line 9, in <module>
    from ib.lib.overloading import overloaded


  File "C:\Users\Heraaizon\AppData\Local\Programs\Python\Python37-32\lib\ib\lib\__init__.py", line 239
    except (socket.error, ), ex:
                           ^
SyntaxError: invalid syntax
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
Millz
  • 21
  • 1
    You have a syntax error, but it look like you haven't posted your whole code. Typo in `qiantity` in the `make_order` function as well – liamhawkins Mar 09 '19 at 11:38
  • thanks didn't see the typo at all and this is all my code to see more about what I mean check out this youtube video I was referencing (alongside a book) -> https://www.youtube.com/watch?v=Bu0kpU-ozaw – Millz Mar 09 '19 at 11:46

1 Answers1

2

The syntax of the except clause has changed between Python 2 and Python 3.

Python 2:

except (socket.error, ), ex:

would be in Python 3:

except (socket.error, ) as ex:

It seems that your code was written for Python 2. Either run it with Python 2 or find an updated version of the code.

Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
  • now working better giving me a display of "Server Version: 76 TWS Time at connection:20190309 12:09:08 Greenwich Mean Time" when I type main() after running the program however the program interative broker is not picking up the order or takes me a few times before it realises something ???: – Millz Mar 09 '19 at 12:11
  • This might be a different question, then. You can consider creating a new one with all the relevant information if you don't manage to solve it! – Thierry Lathuille Mar 09 '19 at 12:19