Question about position management

[Deleted User][Deleted User] Posts: 0
edited July 2017 in Under The Hood

Hi,

Here's a problem I encountered:

Say I have 3 open positions on AAPL i.e. I have 3 UnmatchedTrade object entries in account['AAPL'].position.inventory. As far as I can see the position are managed on a FIFO basis i.e. when I close a position it will be based on when it were entered.

What if I want to manage the positions via order_id i.e. say I want to decrease a position but I want to match the 3rd UnmatchedTrade in the inventory instead of the First In, can I do that?

I can see that it can managed via order_id for order.cancel but I cannot see a way where I can decrease positions by order_id.

Essentially I am looking for how I can link order.algo_buy or order.algo_sell to an order_id when I am decreasing a position.

The problem I have with using FIFO is that the VWAP of the inventory will change on each decrease. Hence, even if I sell above the VWAP on each order then I can end up with a loss as the VWAP for the inventory changes for each decrease.

Example 1 - current situation:

First In Pos: 1 share bought at $15
Middle In Pos: 1 share bought at $10
Last In Pos: 1 share bought at $5
WVAP = $10

Price = $10.1
Action: 1 share sold above VWAP at $10.1
New VWAP = $7.5

Price = $7.6
Action: 1 share sold above VWAP at $7.6
New VWAP = $5

Price = $5.1
Action: 1 share sold above VWAP at $5.1

Cost of position = $30 (15+10+5)
Sale of position = $22.8 (10.1+7.6+5.1)

Realized loss = $7.2 ($15+$10+$5 -$10.1-$7.6-$5.1)
Open position = None

If I could manage the matching of the inventory and orders then I can match profitable positions and manage the VWAP in a profitable way:

Example 2 - desired situation:

First In Pos: 1 share bought at $15
Middle In Pos: 1 share bought at $10
Last In Pos: 1 share bought at $5
WVAP = $10

Price = $10.1
Action: 1 share sold above VWAP at $10.1 --> matched to the Middle In Pos e.g. via entry order_id
New VWAP = $10

Price = $7.6
Action: no action; wait for price to raise above VWAP

Price = $5.1
Action: no action; wait for price to raise above VWAP

Price = $10.1
Action: 1 share sold above VWAP at $10.1 --> matched to the Last In Pos e.g. via entry order_id
New VWAP = $15

Price = $10.1
Action: no action; wait for price to raise above VWAP

Realized profit = $5.2 ($10+$5 -$10.1-$10.1)
Unrealized loss = $4.9 ($15 -$10.1)
Open position = 1 share bought at $15 (-$4.9 mtm)

Comments

  • Currently, the simulation engine would match the position on a FIFO basis. If someone has a better idea on how to implement this user-defined VWAP method, please feel free to comment. From my perspective, I would suggest setting up a variable for each instance and manage the inventory change within the script.

Sign In or Register to comment.