Design: Position Management

A trading system will have 3 core modules to manage business objects including: orders, trades, and positions. This entry covers position management.

After an investor has opened a position, the position will stay there and risk running, generating profit and loss with the price going up and down, until the position is closed.

The position module helps to manage the currently open positions, which will provide utilities to other modules, such as valuation module, risk exposure management, oms and tms. For example, for OMS to generate orders to close positions, it needs to know what position is available for the close operation via pms.

In Chinese market, a stock position created on T is not allowed to be closed on T; while in US market, there’s no such restriction. To generalize both scenarios, we will need to maintain positions upto yesterday (yposition) and positions created today (tposition).

The following are a few general scenarios that pms needs to provide functions for. Note that adding or decreasing positions are done via trades.

Scenario 1: creating a new position On 30Mar2020, BUY 002458 for 1000 unit @17.19. PMS will include the trade and reflect it to a position. Since it’s a new position, the math is very straightforward. date : 2020-03-30 stock: 002458 direction: long position : 1000 usable_pos: 0 (you can’t sell when just created) avgOpenPrice: 17.19 avgHoldPrice: 17.19 pnl: (this will be marked by mtm) realized_pnl:0

Scenario 2 adding to an existing position On 31Mar2020, BUY 002458 for 200 unit @ 17.30. PMS will calculate the average cost, date : 2020-03-31 stock: 002458 direction: long position : 1000+200=1200 usable_pos: 1000 (newly added 200 is not usable until next session) avgOpenPrice: (17.191000+17.30200)/(1000+200)=17.21 avgHoldPrice: 17.21 pnl: (this will be marked by mtm) realizedpnl:0

Scenario 3 reducing an existing position On 31Mar2020, SELL 002458 for 300 unit @ 17.50. PMS will calculate the average cost, date : 2020-03-31 stock: 002458 direction: long position : 1200-300 usable_pos: 1000-300 (newly added 200 is not usable until next session) avgOpenPrice: 17.21 avgHoldPrice: 17.21 pnl: (this will be marked by mtm) realizedpnl:0+300*(17.50-17.21)=87

Scenario 4 closing an existing position On 1Apr2020, SELL 002458 for all positions @17.01. (PMS has 900 usable, so it generates an order with quantity of 900 to sell). date : 2020-04-01 stock: 002458 direction: long position : 900-900 usable_pos: 900-900 avgOpenPrice: 17.21 avgHoldPrice: 17.21 pnl: (this will be marked by mtm) realizedpnl:87+900*(17.01-17.21)

Scenario 5 rolling positions from T to T+1 with open positions On 30Mar2020, BUY 002458 for 1000 unit @17.19. On 31Mar2020, check position. date(T): 2020-03-30 stock: 002458 direction: long position : 1000 usable_pos: 0 (you can’t sell when just created) avgOpenPrice: 17.19 avgHoldPrice: 17.19 date(T+1): 2020-03-31 stock: 002458 direction: long position : 1000 usable_pos: 1000 avgOpenPrice: 17.19 avgHoldPrice: 17.19

Scenario 6 rolling positions from T to T+1 with 0 positions On 1Apr2020, SELL 002458 for all positions @17.01. On 2Apr2020, check positions. date(T): 2020-04-01 stock: 002458 direction: long position : 900-900 usable_pos: 900-900 avgOpenPrice: 17.21 avgHoldPrice: 17.21 realizedpnl:87+900*(17.01-17.21) date(T+1): 2020-04-02 stock: 002458 (no position found).

Note that for each of the scenarios, we can validate the corresponding CASH positions as well. Stock risk allocation will affect CASH after all.

Published by

wofong

三千娑婆世界,三千难忘遗憾;回头乃是岸,此岸在何方;堪忍不能忍,万般看不穿;何时放得下,始得自在心。 I'm a programmer, a quantitative analyst, a photography hobbyist, a traveler, a runner, and a nature lover.

Leave a comment