ib_async icon indicating copy to clipboard operation
ib_async copied to clipboard

positions not updating fast enough

Open advaithhari opened this issue 1 year ago • 2 comments

I am using the ib.positions() function in order to get the positions for my account. When I send an order and it fills I call ib.positions() but it doesn't update even though I can see on the TWS that the position updated. Has anyone else faced this issue or has any work around to get the positions faster.

advaithhari avatar May 09 '24 18:05 advaithhari

@advaithhari are you trying to immediately pull ib.positions() after the order is filled? If so, ib.positions() may have a delay in updating position quantities. Some people go around this by maintaining their own register of fills and using cumulative totals to see your open positions. I personally do that, but I also use ib.positions() to compare what I have in my register to what ib.positions() shows but my bot "trusts" my tally based on fills and I only do ib.positions() comparison to double check.

vasiche avatar May 10 '24 00:05 vasiche

Yeah, this is an IBKR artifact because their API sends all data updates asynchronously.

For my own usage, I add a 1 to 5 second sleep after a trade before printing position updates (and even then sometimes the positions are empty for another couple seconds). For faster updates, I also always listen for commission events which have the actual trade details as soon as possible:

self.ib.commissionReportEvent += self.commissionHandler 

where it looks something like:

    def commissionHandler(self, trade, fill, report):
        # Only report commissions if connected (not when loading startup orders)
        if not self.connected:
            logger.warning("Ignoring commission because not connected...")
            return

        logger.warning(
            "[{} :: {} :: {}] Order {} commission: {} {} {} at ${:,.2f} (total {} of {}) (commission {} ({} each)){}",
            trade.orderStatus.orderId,
            trade.orderStatus.status,
            trade.contract.localSymbol,
            fill.execution.orderId,
            fill.execution.side,
            fill.execution.shares,
            fill.contract.localSymbol,
            fill.execution.price,
            fill.execution.cumQty,
            trade.order.totalQuantity,
            locale.currency(fill.commissionReport.commission),
            locale.currency(fill.commissionReport.commission / fill.execution.shares),
            f" (pnl {fill.commissionReport.realizedPNL:,.2f})"
            if fill.commissionReport.realizedPNL
            else "",
        )

The print format gives output like:

2024-03-08 06:30:00.628 | WARNING  | icli.cli:commissionHandler:1410 - [37540 :: Submitted :: NVDA  240308C01000000] Order 37540 commission: BOT 1.0 NVDA  240308C01000000 at $1.00 (total 1.0 of 100.0) (commission $1.05 ($1.05 each))
2024-03-08 06:30:01.478 | WARNING  | icli.cli:commissionHandler:1410 - [37540 :: Submitted :: NVDA  240308C01000000] Order 37540 commission: BOT 1.0 NVDA  240308C01000000 at $1.00 (total 2.0 of 100.0) (commission $0.35 ($0.35 each))
2024-03-08 06:30:01.480 | WARNING  | icli.cli:commissionHandler:1410 - [37540 :: Submitted :: NVDA  240308C01000000] Order 37540 commission: BOT 1.0 NVDA  240308C01000000 at $1.00 (total 3.0 of 100.0) (commission $0.70 ($0.70 each))
2024-03-08 06:30:02.943 | WARNING  | icli.cli:commissionHandler:1410 - [37540 :: Filled :: NVDA  240308C01000000] Order 37540 commission: BOT 24.0 NVDA  240308C01000000 at $1.21 (total 27.0 of 100.0) (commission $16.72 ($0.70 each))
2024-03-08 06:30:02.984 | WARNING  | icli.cli:commissionHandler:1410 - [37540 :: Filled :: NVDA  240308C01000000] Order 37540 commission: BOT 70.0 NVDA  240308C01000000 at $1.21 (total 97.0 of 100.0) (commission $48.78 ($0.70 each))
2024-03-08 06:30:02.986 | WARNING  | icli.cli:commissionHandler:1410 - [37540 :: Filled :: NVDA  240308C01000000] Order 37540 commission: BOT 3.0 NVDA  240308C01000000 at $1.21 (total 100.0 of 100.0) (commission $2.09 ($0.70 each))
2024-03-08 06:35:45.942 | WARNING  | icli.cli:commissionHandler:1410 - [37549 :: Submitted :: NVDA  240308C01000000] Order 37549 commission: SLD 11.0 NVDA  240308C01000000 at $1.50 (total 11.0 of 100.0) (commission $7.71 ($0.70 each)) (pnl 310.56)                                                                                                                                                                                                                                     
2024-03-08 06:35:46.046 | WARNING  | icli.cli:commissionHandler:1410 - [37549 :: Filled :: NVDA  240308C01000000] Order 37549 commission: SLD 4.0 NVDA  240308C01000000 at $1.50 (total 15.0 of 100.0) (commission $2.80 ($0.70 each)) (pnl 112.93)                                                                                                                                                                                                                                         
2024-03-08 06:35:46.046 | WARNING  | icli.cli:commissionHandler:1410 - [37549 :: Filled :: NVDA  240308C01000000] Order 37549 commission: SLD 85.0 NVDA  240308C01000000 at $1.50 (total 100.0 of 100.0) (commission $59.57 ($0.70 each)) (pnl 2,399.75) 
2024-04-15 14:45:32.972 | WARNING  | icli.cli:commissionHandler:1491 - [0 :: Filled :: 28812380] Order 23311 commission: SLD 2.0 SPY   240419P00515000 at $11.06 (total 2.0 of 0.0) (commission $1.42 ($0.71 each))
2024-04-15 14:45:32.973 | WARNING  | icli.cli:commissionHandler:1491 - [0 :: Filled :: 28812380] Order 23311 commission: BOT 22.0 SPY   240419C00515000 at $0.77 (total 22.0 of 0.0) (commission $15.33 ($0.70 each))
2024-04-15 14:45:32.974 | WARNING  | icli.cli:commissionHandler:1491 - [0 :: Filled :: 28812380] Order 23311 commission: BOT 8.0 SPY   240419C00515000 at $0.76 (total 30.0 of 0.0) (commission $5.57 ($0.70 each))
2024-04-15 14:45:32.980 | WARNING  | icli.cli:commissionHandler:1491 - [0 :: Filled :: SPY   240419P00515000] Order 23314 commission: BOT 2.0 SPY   240419P00515000 at $10.55 (total 2.0 of 0.0) (commission $0.55 ($0.28 each)) (pnl 100.03)
2024-04-15 14:45:32.980 | WARNING  | icli.cli:commissionHandler:1491 - [0 :: Filled :: SPY   240419C00515000] Order 23313 commission: SLD 30.0 SPY   240419C00515000 at $0.86 (total 30.0 of 0.0) (commission $8.41 ($0.28 each)) (pnl 248.68)
2024-04-17 13:10:43.265 | WARNING  | icli.cli:commissionHandler:1491 - [23675 :: Filled :: SPY   240419P00490000] Order 23675 commission: SLD 1.0 SPY   240419P00490000 at $0.28 (total 1.0 of 1.0) (commission $0.63 ($0.63 each)) (pnl 4.74)
2024-04-18 08:15:08.223 | WARNING  | icli.cli:commissionHandler:1491 - [23752 :: Filled :: ASML] Order 23752 commission: SLD 6.0 ASML at $892.00 (total 6.0 of 100.0) (commission $0.38 ($0.06 each)) (pnl -138.40)
2024-04-18 08:15:08.225 | WARNING  | icli.cli:commissionHandler:1491 - [23752 :: Filled :: ASML] Order 23752 commission: SLD 94.0 ASML at $892.00 (total 100.0 of 100.0) (commission $0.46 ($0.00 each)) (pnl -2,162.71)

(sample extracted from https://github.com/mattsta/icli/blob/fe46fef75d7f771043113c1d3dd46683074745d9/icli/cli.py#L1409-L1469C10)

mattsta avatar May 10 '24 01:05 mattsta