Skip to content

Business Logic Portfolio and Risk Management

tien.nguyen edited this page Jul 30, 2026 · 1 revision

Business Logic: Portfolio, Positions & Risk Management

This document details position accounting, weighted average cost basis calculations, Realized & Unrealized Profit & Loss (P&L) formulas, and pre-trade risk management controls in Emporia Trading Platform's portfolio-service.


💼 Position Accounting & Life Cycle

portfolio-service maintains real-time position state records for each user account and instrument symbol.

  • Long Position: Position quantity $> 0$. Benefits when market price increases.
  • Short Position: Position quantity $< 0$. Benefits when market price decreases.
  • Flat Position: Position quantity $= 0$. No open market exposure.
flowchart TD
    FillEvent["Execution Fill Event Received"] --> CheckDir{"Existing Position Side?"}
    CheckDir -->|Same Side| AddPos["Increase Position Size & Update Average Cost"]
    CheckDir -->|Opposite Side| ReducePos["Reduce Position Size & Calculate Realized P&L"]
    ReducePos --> CheckZero{"New Quantity == 0?"}
    CheckZero -- Yes --> Flat["Position Closed (FLAT)"]
    CheckZero -- No --> Partial["Position Partially Closed / Flipped"]
Loading

🧮 Cost Basis & P&L Calculation Formulas

1. Weighted Average Cost Basis Formula

When adding to an existing position (e.g., buying more shares for an existing long position):

$$\text{CostBasis}_{\text{new}} = \frac{(\text{CostBasis}_{\text{prev}} \times Q_{\text{prev}}) + (P_{\text{fill}} \times Q_{\text{fill}})}{Q_{\text{prev}} + Q_{\text{fill}}}$$

2. Realized Profit and Loss (Realized PnL)

Realized PnL is locked in when a position is reduced or closed out:

  • Closing a Long Position (Selling shares at price $P_{\text{exit}}$): $$\text{Realized PnL} = Q_{\text{closed}} \times (P_{\text{exit}} - \text{CostBasis})$$

  • Closing a Short Position (Buying back shares at price $P_{\text{exit}}$): $$\text{Realized PnL} = Q_{\text{closed}} \times (\text{CostBasis} - P_{\text{exit}})$$

3. Unrealized Profit and Loss (Unrealized / Mark-to-Market PnL)

Unrealized PnL reflects the paper profit or loss of open positions marked against current market quotes ($P_{\text{mark}}$):

  • For Long Positions: $$\text{Unrealized PnL} = Q_{\text{position}} \times (P_{\text{mark}} - \text{CostBasis})$$

  • For Short Positions: $$\text{Unrealized PnL} = |Q_{\text{position}}| \times (\text{CostBasis} - P_{\text{mark}})$$

Mark Price Selection:

  • Long positions mark against current Best Bid ($P_{\text{bid}}$).
  • Short positions mark against current Best Ask ($P_{\text{ask}}$).

🛡️ Pre-Trade Risk Management Controls

Before an order is submitted to execution venues, portfolio-service and authorisation-service enforce pre-trade risk constraints:

flowchart TD
    Order["Inbound Order Command"] --> Risk1{"Order Value <= Max Order Limit?"}
    Risk1 -- No --> Block1["Reject: Exceeds Max Single Order Value ($100k)"]
    Risk1 -- Yes --> Risk2{"Position + Order <= Max Symbol Position Limit?"}
    Risk2 -- No --> Block2["Reject: Exceeds Symbol Position Exposure Cap"]
    Risk2 -- Yes --> Risk3{"Required Margin <= Available Buying Power?"}
    Risk3 -- No --> Block3["Reject: Insufficient Account Buying Power"]
    Risk3 -- Yes --> Pass["Pass Pre-Trade Risk Checks"]
Loading

Risk Limits Inventory

  1. Max Order Value Limit: Restricts single order size (e.g. max $100,000 per order) to prevent catastrophic fat-finger entries.
  2. Symbol Concentration Limit: Restricts max net position per symbol (e.g. max 10,000 shares of any single ticker).
  3. Buying Power & Margin Verification: Verifies that cash balance plus available margin covers initial margin requirements before order routing.

Clone this wiki locally