erdewit / ib_insync

Python sync/async framework for Interactive Brokers API
BSD 2-Clause "Simplified" License
2.84k stars 769 forks source link

How to show the pnl of the account? #157

Closed greg2paris closed 5 years ago

greg2paris commented 5 years ago

I tried this but I cant do it.

ib = IB()
ib.connect('127.0.0.1', 7497, clientId=16)
print(ib.pnl)
greg2paris commented 5 years ago

I just found out how to do, if you have at least opened position: print(ib.portfolio()) will display all currently opened position details. If you have no position opened it will print a empty array: []

If you want to know the unrealized pnl of the 1st position you have to use this code: unrealized_pnl = ib.portfolio()[0][5]

If you want the total realized pnl for the security, use this: realized_pnl = ib.portfolio()[0][6]

print("unrealized pnl: ", unrealized_pnl, "realized pnl: ", realized_pnl) # show portofolio