开云体育

ctrl + shift + ? for shortcuts
© 2025 开云体育

Determining which stock is bringing my portfolio into a loss


 

Hi all,

I am trying to write some code to determine which stock is bringing my
portfolio into a loss.

My logic is to see if the averageCost is greater than the current market
price for each stock in the portfolio.

The problem is that the current market price is nan. I am running this code
after market close. Here is the relevant snippet. What am I missing?
ib.connect('127.0.0.1', 4001, clientId=999990)
positions=ib.positions()
lc=0
for p in positions:

[ticker] = ib.reqTickers(p.contract)
print(p.contract.symbol)

print("number of shares=" + str(p.position))
print("Cost per share=",ib.portfolio()[lc].averageCost)
print("Current price per share=",ticker.marketPrice())

lc+=1
ib.sleep(1)
Pranav

Join [email protected] to automatically receive all group messages.