Skip to content

API Reference


API Reference / PositionLedger

Class: PositionLedger

Defined in: src/execution/PositionLedger.ts:133

Derives positions, fill history, realized P&L, and exposure from fills — the state layer PositionMonitor sits on top of. PositionMonitor decides when to exit a position it's told about; PositionLedger figures out what's actually open from what has actually filled.

This does not listen to the order-update WebSocket itself — Dhan's raw payload only exposes orderId/status/tradedQty/averageTradedPrice/ securityId in typed form (see OrderState), not the exchangeSegment or transactionType a ledger needs, and tradedQty is cumulative for the order, not a per-event delta. Feed it explicitly instead, once per order, from the side that already knows those fields — typically an OrderTracker filled handler, since your own place-order request already carried exchangeSegment/transactionType:

ts
tracker.on("filled", (outcome) => {
  ledger.recordFill({
    exchangeSegment: request.exchangeSegment,
    securityId: request.securityId,
    transactionType: request.transactionType,
    quantity: outcome.filledQuantity,
    price: outcome.averagePrice!,
    timestamp: new Date(),
    orderId: outcome.orderId,
    correlationId: outcome.correlationId,
  });
});

If you want per-partial-fill granularity from partial events instead of one fill per order, diff each event's filledQuantity against the previous value yourself first — calling recordFill() with the raw cumulative quantity on every partial event double-counts.

No persistence: state lives in memory and resets on restart or reset().

Constructors

Constructor

new PositionLedger(): PositionLedger

Returns

PositionLedger

Methods

recordFill()

recordFill(fill): LedgerPosition

Defined in: src/execution/PositionLedger.ts:138

Parameters

fill

Fill

Returns

LedgerPosition


onTick()

onTick(event): void

Defined in: src/execution/PositionLedger.ts:147

Feeds one market tick in. Wire to client.ws.market.on("tick", …).

Parameters

event

MarketFeedEvent

Returns

void


position()

position(exchangeSegment, securityId): LedgerPosition | undefined

Defined in: src/execution/PositionLedger.ts:155

Parameters

exchangeSegment

string

securityId

string

Returns

LedgerPosition | undefined


openPositions()

openPositions(): LedgerPosition[]

Defined in: src/execution/PositionLedger.ts:160

Positions with open quantity — excludes symbols that have gone flat.

Returns

LedgerPosition[]


allPositions()

allPositions(): LedgerPosition[]

Defined in: src/execution/PositionLedger.ts:165

Every symbol ever filled, open or flat, so closed-trade realizedPnl stays visible.

Returns

LedgerPosition[]


fills()

fills(): readonly Fill[]

Defined in: src/execution/PositionLedger.ts:170

Full fill history across every symbol, in the order recordFill() was called.

Returns

readonly Fill[]


totalRealizedPnl()

totalRealizedPnl(): number

Defined in: src/execution/PositionLedger.ts:175

Sum of realizedPnl across every symbol, open or flat.

Returns

number


exposure()

exposure(): object

Defined in: src/execution/PositionLedger.ts:188

Mark-to-market exposure per open symbol and in aggregate. Falls back to a symbol's averagePrice for any position onTick() hasn't seen a price for yet.

Returns

object

bySymbol

bySymbol: PositionExposure[]

total

total: number


reset()

reset(): void

Defined in: src/execution/PositionLedger.ts:212

Drops every position, fill, and mark price.

Returns

void

Community project — not affiliated with Dhan. MIT License.