request the number of shares in your portfolio via ib_insync

581 views Asked by At

I would like to know how many shares I have of a specific position using ib_insync. Using solely the ib.positions() function gives me a [...] with much more information that just the number of shares.

I found some code on the internet:

from random import choice

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

this code works for me, but I presume this is only because I have just one stock in my portfolio. Thank you for sharing what code I can use to get the number of shares in my portfolio.

1

There are 1 answers

0
Kamille On
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() will give you a list so you can use for to show every position of yours.