使用python ibPy库获取您的投资组合的位置

时间:2015-04-14 16:22:55

标签: python interactive-brokers ibpy

我正在使用ibpy获取我的投资组合的头寸。我明白我能做到:

from ib.opt import ibConnection
tws = ibConnection( host = 'localhost',port= 7496, clientId = 123)
tws.reqAccountUpdates(True,'accountnumber')

然后我应该以某种方式使用updatePortfolio(),但我不知道如何。

由于

2 个答案:

答案 0 :(得分:6)

tws.reqAccountUpdates(True,'accountnumber')当你可能认为它是一个变量时,会发送字符串“acctnumber”。请注意我发送的字符串是我的实际(假)帐号。

然后,您需要为您感兴趣的消息注册回调。

from ib.opt import ibConnection, message

def acct_update(msg):
    print(msg)


con = ibConnection(clientId=1)
con.register(acct_update,
             message.updateAccountValue,
             message.updateAccountTime,
             message.updatePortfolio)
con.connect()
con.reqAccountUpdates(True,'DU000000')

#don't forget to disconnect somehow when done
#con.disconnect()

答案 1 :(得分:2)

还有reqPositions()顾名思义给你的位置(只有你的位置,而不是给你很多其他信息)。它们将在message.position条消息中返回。