ComponentIntermediate

The Portfolio Engine

The portfolio engine is the component that maintains the authoritative internal record of positions, cash and P&L, updating on every fill and continuously reconciling that record against the broker's.

Quick answer: The portfolio engine is the component that maintains the authoritative internal record of positions, cash and P&L, updating on every fill and continuously reconciling that record against the broker's.

In simple words

The portfolio engine is the system's accountant. It knows exactly what you hold, how much cash you have, and whether you are up or down, updating every time an order fills. Just as importantly, it keeps checking its books against the broker's, because if the two disagree, every decision above it is built on a wrong number.

Purpose

Position sizing, risk checks and signals all need to know the current state. Centralising that state in one reconciled record means the whole system agrees on what it holds, rather than each part guessing from its own fills.

Visual explanation

The Portfolio Engine

Fills update positions, cash and P&L; a reconciliation loop compares the internal record against the broker's.

Portfolio FlowFillsPositionsCash /MarginP&LReconcilevs broker

Professional explanation

Single responsibility

The portfolio engine owns the answer to what do we currently hold, at what cost, with how much cash, and what is the P&L. It is the single source of truth for position state that the risk engine, sizing engine and signals all read. It does not decide trades or place orders; it records their consequences. Its correctness is foundational: if the recorded position is wrong, sizing over- or under-bets, the risk engine mis-measures exposure, and the signal may think it is flat when it is long. This is why reconciliation, not just bookkeeping, is central to its design.

State it maintains

Core state is a set of positions (per instrument: quantity, average entry price, and for options the strike, expiry and side), available and blocked cash or margin, and P&L split into realised (from closed trades) and unrealised (mark-to-market on open positions). It also derives exposures the risk engine needs: gross and net exposure, per-instrument concentration, and margin utilisation. Positions update on fills; unrealised P&L updates on every price tick via the data layer's marks. The engine must handle the full lifecycle of a position including averaging into it, partially closing, and flipping direction, each of which changes average price and realised P&L in specific ways.

Inputs and outputs

Inputs are fill events (from execution/OMS), price marks (from the data layer), and periodic broker snapshots of positions and funds. Outputs are read queries the rest of the system depends on: current position for an instrument, available capital for sizing, current P&L and exposure for risk. The engine should expose a consistent snapshot rather than piecemeal fields that can be read mid-update, because a reader that sees an updated position but stale cash will compute nonsense. A clean interface returns an immutable snapshot of the whole state as of a point in time.

Reconciliation with the broker

The broker is the authoritative record of truth; the internal book is a fast local mirror that can drift. Reconciliation compares the two, positions, quantities, and where possible average prices and cash, and flags any mismatch. Drift happens for real reasons: a fill event missed during a disconnect, a manual trade placed outside the system, an exchange-side adjustment, or a bug. Reconciliation should run at startup (before trading, so the system begins from truth), periodically during the session, and after any reconnect. The policy on a mismatch matters: small, explainable differences may auto-correct to the broker's number and log; a large unexplained divergence should halt trading, because acting on a wrong position is dangerous.

P&L accounting subtleties

P&L looks simple and is full of traps. Realised P&L on a partial close must use the correct cost basis (average or FIFO, chosen and applied consistently). Fees, brokerage, STT, exchange charges and GST must be included or reported P&L will be optimistic; in Indian F&O these frictions are non-trivial and STT on option selling and futures is a real drag. Marking open positions needs a sensible price (last trade, or mid) and must handle illiquid strikes where the last trade is stale. Flipping a position from long to short in one fill realises the long P&L and opens a new short at the fill price, which the accounting must split correctly.

How it fails

The engine fails by missing a fill (so its position is wrong), double-counting a fill (same problem, opposite sign), using a stale mark (so unrealised P&L and thus risk are wrong), applying an inconsistent cost basis, or omitting costs. The most dangerous failure is silent position drift: the system believes it is flat and happily opens a new position while actually already holding one, doubling exposure. Defences: idempotent fill application keyed on order/fill ids so a replayed event does not double-count, frequent reconciliation against the broker, halting on unexplained divergence, and marking against a validated price with a staleness check.

Observability

Expose the book so it can be watched and trusted. Emit metrics for each position, current P&L (realised, unrealised, net of costs), exposure and margin used, and, critically, the reconciliation status and the size of any position or cash discrepancy. A reconciliation-difference alert should be prominent, because a growing gap between system and broker means the source of truth is being lost. Log every fill applied with the resulting position and average price so the book's evolution can be replayed, and snapshot the full state periodically so a restart can begin from a recent known-good point and then reconcile forward.

Practical example

Illustrative example (Indian market)

A bot holds short 2 lots of a Nifty call at an average of 120. A fill event reports buying back 1 lot at 90, so the portfolio engine reduces the position to short 1 lot, books realised P&L of (120 minus 90) times 75 equals 2,250 rupees on the closed lot, and leaves the remaining lot marked at the latest price for unrealised P&L. It nets brokerage, STT and charges into the realised figure, so the reported profit is what actually reaches the account, not the gross. At the next reconciliation the broker also shows short 1 lot, so the books agree. Had the broker instead shown flat, meaning the other lot had been closed by an order the system missed, the engine would flag a divergence, halt new trading, and require the mismatch to be resolved before continuing.

Indian F&O charges STT, exchange transaction charges, SEBI turnover fees, stamp duty and GST on brokerage, and STT on option sell-side and on futures is a meaningful cost; a portfolio engine that reports P&L gross of these will consistently overstate performance, so the accurate approach is to deduct the full charge stack on every fill so the book reflects realisable P&L.

Advantages

  • One reconciled source of truth for positions, cash and P&L across the system
  • Reconciliation catches missed fills, manual trades and drift before they compound
  • Cost-inclusive P&L reflects what is actually realisable, not an optimistic gross
  • A consistent state snapshot prevents readers from acting on half-updated data

Limitations

  • Only as accurate as its fill feed and marks; a missed fill silently corrupts state
  • Reconciliation depends on the broker's snapshot being timely and correct
  • Marking illiquid options is imprecise when the last trade is stale
  • Cost and tax accounting is fiddly and jurisdiction-specific, easy to get subtly wrong

Why it matters in practice

  • Every sizing and risk decision is only as trustworthy as the portfolio state
  • Silent position drift can double exposure without any component noticing

Common mistakes

  • Trusting the internal book without reconciling, so a missed fill leaves the position permanently wrong
  • Double-counting a fill on a replay because fill application is not idempotent
  • Reporting P&L gross of brokerage, STT and charges, overstating performance
  • Marking open positions with a stale last-trade price on an illiquid strike
  • Continuing to trade after an unexplained reconciliation divergence instead of halting
  • Applying an inconsistent cost basis when partially closing, corrupting realised P&L

Professional usage

Professional shops separate a fast internal position keeper from an end-of-day book reconciled against the prime broker or clearer, and they reconcile intraday too. They apply a consistent, documented cost-basis convention, mark to validated prices, fold every fee and tax into P&L, and treat any unexplained break as an incident to resolve before trading continues. The internal book is understood to be a mirror that can drift, so reconciliation against the authoritative record is a scheduled, monitored process rather than an afterthought.

Key takeaways

  • The portfolio engine is the single reconciled source of truth for positions, cash and P&L
  • The broker is authoritative; reconcile against it at startup, periodically and after reconnects
  • Apply fills idempotently and include all costs so P&L is realisable, not gross
  • Halt on unexplained divergence: acting on a wrong position is a dangerous state

Frequently asked questions

What is a portfolio engine in a trading system?
It is the component that maintains the authoritative internal record of positions, cash and P&L, updating on every fill and reconciling continuously against the broker's record so the whole system shares one correct view of state.
Why does the portfolio engine reconcile with the broker?
Because the internal book is a fast local mirror that can drift from reality when a fill is missed, a manual trade is placed, or a bug occurs. The broker is the source of truth, so reconciliation catches and corrects divergence.
What is the difference between realised and unrealised P&L?
Realised P&L comes from closed trades and is locked in; unrealised P&L is the mark-to-market gain or loss on open positions and changes with every price tick until the position is closed.
When should reconciliation run?
At startup before any trading, so the system begins from truth; periodically during the session; and after any reconnect. Beginning a session from a wrong position is a serious risk, which is why startup reconciliation is essential.
What should happen when the internal book and broker disagree?
Small, explainable differences can be corrected to the broker's figure and logged. A large or unexplained divergence should halt new trading, because continuing to act on a wrong position can double exposure or breach limits.
How does the portfolio engine handle costs and taxes?
It deducts brokerage, STT, exchange charges, stamp duty and GST from realised P&L on each fill, so reported P&L reflects what is actually realisable. Ignoring these overstates performance, and in Indian F&O the costs are non-trivial.
How are fills applied idempotently?
By keying fill application on unique order or fill ids and ignoring a fill already applied. This prevents a replayed or duplicated event, common after a reconnect, from double-counting and corrupting the position.
How does the engine handle a position flipping from long to short?
A fill large enough to flip realises the P&L on the closed long quantity at the fill price and opens a new short at that price. The accounting must split the fill into a close and an open rather than net it into one wrong average.
How are open positions marked for unrealised P&L?
Against a sensible current price, usually the last trade or the mid, supplied by the data layer. For illiquid strikes the last trade may be stale, so a staleness check or a mid-price is used to avoid a misleading mark.
What state does the portfolio engine provide to other components?
Current positions, available capital or margin for sizing, and P&L and exposure for risk, ideally as a consistent point-in-time snapshot so readers never see a half-updated mix of new position and stale cash.
What is the most dangerous portfolio engine failure?
Silent position drift: the system believes it is flat and opens a new position while actually already holding one, doubling exposure without any component noticing. Frequent reconciliation and halting on divergence guard against it.
Does the portfolio engine place orders?
No. It records the consequences of trades, positions, cash and P&L, but it does not decide or place them. That separation keeps it a clean, trustworthy source of state for the components that do act.
How should the engine survive a restart?
By snapshotting full state periodically and, on restart, loading the recent snapshot and then reconciling forward against the broker's current positions and fills, so it resumes from a known-good, verified state.
What is the difference between the portfolio engine and the OMS?
The OMS tracks the lifecycle and state of individual orders; the portfolio engine tracks the resulting aggregate positions, cash and P&L. Orders are the events; the portfolio is the running balance those events produce.

Voice search & related questions

Natural-language questions people ask about The Portfolio Engine.

What does the portfolio engine do?
It is the system's accountant. It tracks what you hold, your cash, and your profit or loss, updating every time an order fills.
Why check my positions against the broker?
Because your own records can drift if a fill is missed or a trade is placed outside the system. The broker is the real truth, so you keep comparing to it.
What is the difference between realised and unrealised profit?
Realised profit is locked in from trades you have closed. Unrealised profit is the paper gain or loss on positions you still hold, and it moves with the price.
Why is my bot's profit lower than I calculated?
Probably because of costs. Brokerage, STT and other charges eat into every trade, and a proper portfolio engine subtracts them so the number is what you actually keep.
What should happen if my records disagree with the broker?
Stop trading and sort it out. Acting on a wrong position can accidentally double your exposure.
How does a bot know how much it holds after a restart?
It reloads its saved state and then checks it against the broker's current positions before it starts trading again.

Sources & references

    Last reviewed 11 July 2026. Educational content only — not investment advice. Markets and rules change; verify current conventions with SEBI, NSE/BSE and your broker.

    Educational content only — not investment advice. Examples use illustrative numbers and simplified models. Algorithmic trading and derivatives involve substantial risk. See our Risk Disclosure and SEBI Disclaimer.