I am using ibpy to get my porfolio information every 10 seconds (i need this information in a very frequent manner), specifically the unrealized pnl information for each contract. The way i do it is :
def updatePortfolio(self):
self._portfolio=[]
if self._updated_accounts==False:
print("requesting account updates")
self._tws.reqAccountUpdates(True,'')
sleep(3)
print("requesting account value updates")
self._tws.updateAccountValue()
sleep(3)
print("requesting portfolio updates")
self._tws.updatePortfolio()
sleep(3)
However, since I do this pretty frequently (every 10 second). It seems portfolio information is not sent back and it usually leads to empty portfolio. How do i ensure that I could request and refresh the portfolio information and not its update (meaning that i should get the complete portfolio information each time i request)? Thank you.
You don't keep calling it.
True
means keep sending updates as needed. When a position changes you'll get an update. I'm assuming you left the account blank for privacy, but you need to specify it.If you need the pnl in an ongoing manner, you should subscribe to market data and calculate based on the contract price and your entry price. You will have received your entry price in the
execDetails
message;I'm assuming you want real time updates of your pnl. Your answer seems like it's still using the update portfolio callback. What you need is to track it on your own and then you can see the pnl whenever the price changes.
First save the execution price
Then on every price tick you can see the pnl
Register these for messages
To request data
This isn't a complete program, just an idea of how it's done. Most people I know don't use the account information except as a check to make sure nothing has gone wrong. You should keep track of your positions and if IB disagrees, then you figure out what's wrong. It's a more real time way of monitoring.