Skip to main content

Market Data Services

Provides external market data integration for equity and bond pricing. Production code uses DSEApiService, while tests may continue relying on the legacy stub exported as DseMarketDataService.

Location: worker/services/market-data/

Production: DSEApiService

Source: worker/services/market-data/dse-api-service.ts

Dependencies

  • @shared/dse-api OpenAPI client (fetch wrapper)
  • Zod schemas in dse-api-schemas.ts
  • createLogger for structured telemetry

Public Methods

getEquities(): Promise<EquityEntity[]> : Fetches active equities from /securities, validates responses with Zod, and throws DSEApiError on network/validation failures.

getSecurityBySymbol(symbol: string): Promise<EquityEntity | null> : Retrieves a single equity; returns null on 404, throws DSEApiError otherwise.

getSecurityDetails(symbol: string): Promise<DSESecurityDetail | null> : Low-level accessor for detailed security metadata.

getLastTradedPrices(symbols: string[]): Promise<DSEPrice[]> : Batch price lookup for equities.

getBondPrices(isins: string[]): Promise<Array<{ isin: string; price: number | null }>> : Fetches bond prices when available.

getSyncStatus(): Promise<SyncStatus> : Returns API operational status.

Error Handling

  • All failures throw DSEApiError (message, endpoint, optional statusCode, and originalError).
  • Unexpected throwables are wrapped in DSEApiError with context and logged.
  • Callers must try/catch and map to domain errors (createBusinessLogicError) before surfacing to tRPC.

Example

import { DSEApiService, DSEApiError } from "@worker/services/market-data";

const dseApi = new DSEApiService();

try {
const equities = await dseApi.getEquities();
// ... use equities ...
} catch (error) {
if (error instanceof DSEApiError) {
logger.warn(
{ endpoint: error.endpoint, status: error.statusCode },
"DSE API failure",
);
throw createBusinessLogicError(`Market data unavailable: ${error.message}`);
}
throw error;
}

Legacy/Test Stub: DseMarketDataService

Source: worker/services/market-data/index.ts

A minimal stub retained for backwards compatibility in tests. Methods resolve to null, empty maps, or placeholder values. Prefer injecting mocks of DSEApiService for new tests.

Placeholder Bond Market Data

Source: worker/services/market-data/bond-index.ts

PlaceholderBondMarketDataService returns synthetic bond prices (typically face value) and accrued interest for deterministic testing.

Integration

Market data services are consumed by:

  • Equity Portfolio ServicegenerateDailySnapshot, ensureEquities
  • Bond Portfolio ServicegenerateDailySnapshot (with pricing fallbacks)
  • EOD Operations – portfolio valuation flows

Migration Notes

  • Production code should inject DSEApiService and convert DSEApiError into domain-friendly errors before reaching tRPC.
  • The legacy stub will be removed after all tests migrate to explicit mocks.

Last Validated: 2025-11-10 against commit 0342a62e