Closed houseofai closed 1 year ago
I think portfolio()
is more useful than positions()
.
portfolio()
also returns much more useful data and is automatically kept up to date by the library while your session is active:
To your original question, .positions()
doesn't provide any data until after a call to .reqPositions()
, then it will be updated by the library. But portfolio()
gives you more information without the extra setup, so it's better overall.
Thanks for the tip @mattsta, indeed the portfolio gives more info, but the problem remains the same: it is based on the current state.
contract = Stock("AAPL", 'SMART', 'USD')
order = MarketOrder("BUY", totalQuantity=1)
client.placeOrder(contract, order)
time.sleep(10)
print("Portfolio")
for portfolio in client.portfolio():
print(portfolio)
prints:
Portfolio
Interestingly, the example on the Ordering workbook forces the update:
contract = Stock("AMD", 'SMART', 'USD')
order = MarketOrder("BUY", totalQuantity=1)
trade = client.placeOrder(contract, order)
while not trade.isDone():
client.waitOnUpdate()
print("Positions")
for position in client.positions():
print(position)
prints the opened trade:
Position(account='DUxxxxx', contract=Stock(conId=4391, symbol='AMD', exchange='NASDAQ', currency='USD', localSymbol='AMD', tradingClass='NMS'), position=1.0, avgCost=93.3644)
time.sleep(10)
ALLWAYS USE ib.sleep(...)
NOT time.sleep(...)
!!!!!!!!!!!!!
During a session, I want to get my current positions, so I use the
ib.positions()
to do so. But I never get the positions opened by orders in the same session. Take a simple example where I connect, place a market order, and request the position on the Current State and the position through the request:client.positions()
call doesn't return any position, whileclient.reqPositions()
does return three positions. (Why three?)Why is
client.positions()
not returning the market order position? Should the current state have been updated after 10secs?