algo交易代码中的except子句出错

问题描述 投票:-1回答: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()

得到这个错误并且不知道为什么然而我确定我正确地安装了IBpy lib但是如果我错了请纠正我:)....任何建议?:

  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
python helper algorithmic-trading ibpy
1个回答
2
投票

except子句的语法在Python 2和Python 3之间发生了变化。

Python 2:

except (socket.error, ), ex:

将在Python 3中:

except (socket.error, ) as ex:

您的代码似乎是为Python 2编写的。要么使用Python 2运行它,要么找到代码的更新版本。

© www.soinside.com 2019 - 2024. All rights reserved.