0

I want to receive the average cost of a single position.

I am using the IB-insync API and using reqPositions(). The ouput is:

[Position(account='DU1675421', contract=Stock(conId=29622888, symbol='HEIA', exchange='AEB', currency='EUR', localSymbol='HEIA', tradingClass='HEIA'), position=100.0, avgCost=90.97088),
 Position(account='DU1675421', contract=Future(conId=176791153, symbol='N225M', lastTradeDateOrContractMonth='20191212', multiplier='100', currency='JPY', localSymbol='164120019', tradingClass='NK225M'), position=1.0, avgCost=2284540.0)]

I would like to have the avgcost of 1 position. How would I do this?

b = ib.reqPositions()
while ib.sleep(0.5):
    plb = b
    print (plb)

b.avgCost() doesn't work.

1 Answers1

0

It looks like reqPositions is returning a list of namedtuples.
To access an element in the namedtuple you would need to iterate over the list, e.g.

for position in b:
    print(position.avgCost)
Josh
  • 706
  • 3
  • 8
  • Note that the avgCost includes the multiplier, i.e. it's the average of the prices paid (or received for short positions) including the commission and times the multiplier, but not times the number positions. – Gascoyne Feb 01 '21 at 01:08