Skip to main content
streamPositionUpdates(initial, priceStream?) takes a previously fetched getOpenPositions() response and emits an updated payload whenever relevant prices change.

Example

const positions = await client.getOpenPositions({ user: '0xTraderAddress' });
const stream = client.streamPositionUpdates(positions);

stream.onUpdate(next => {
  console.log(next.marginSummary.totalRawPnlUsd);
  console.log(next.pairPositions[0]?.position.unrealizedPnl);
});

stream.onError(err => console.error(err));

How it works

  • the SDK extracts the unique pairId values from initial.pairPositions
  • it subscribes only to those pairs
  • on each price tick, it recalculates price-sensitive fields and emits the full updated OpenPositionsResponse

Reusing an existing price stream

If your app already owns a price WebSocket, pass it as the second argument to avoid opening another connection:
const priceStream = client.streamPrices([0, 1]);
const positions = await client.getOpenPositions({ user: '0xTraderAddress' });
const stream = client.streamPositionUpdates(positions, priceStream);

Response schema

interface OstiumPositionUpdatesStream {
  onUpdate(handler: (positions: OpenPositionsResponse) => void): () => void;
  onOpen(handler: () => void): this;
  onError(handler: (event: Error) => void): this;
  onClose(handler: (code: number, reason: string) => void): this;
  getCurrent(): OpenPositionsResponse;
  ingestTick(tick: PriceTick): void;
  ingestSnapshot(ticks: PriceTick[]): void;
  close(): void;
}