【问题标题】:request the number of shares in your portfolio via ib_insync通过 ib_insync 请求您投资组合中的股票数量
【发布时间】:2022-12-30 13:10:41
【问题描述】:

我想知道使用 ib_insync 我在特定头寸中有多少股。仅使用 ib.positions() 函数给我一个 [...] 更多信息,而不仅仅是股票数量。

我在网上找到了一些代码:

from random import choice

pos = ib.positions()
p = choice(pos)
return p.position`

这段代码对我有用,但我认为这只是因为我的投资组合中只有一只股票。感谢您分享我可以使用哪些代码来获取我的投资组合中的股票数量。

【问题讨论】:

    标签: python ib-insync


    【解决方案1】:
    from ib_insync import *
    
    ib = IB()
    ib.connect('127.0.0.1', 4002, clientId=1)
    
    positions = ib.positions()
    
    for position in positions:
        print(f'You have {position.position} shares of {position.contract.symbol}')
    

    ib.positions() 会给你一个列表,这样你就可以使用for 来显示你的每个位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多