Skip to main content
This guide shows how to build a portfolio tracker that fetches live prices for a set of stock holdings and calculates current value, cost basis, and unrealized gain or loss. It’s the same core logic NGN Market uses in its own dashboard. Endpoints used:
  • GET /companies (Free plan): bulk price fetch for all holdings at once
  • GET /companies/:symbol (Hobby plan): single stock quote when needed
Recalculating on a polling interval? The WebSocket API can push price updates for your holdings instead, so you only recompute when a price actually changes.

How it works

  1. Store each holding as { symbol, quantity, avg_cost }, either in a database or client-side
  2. Fetch current prices for all held symbols in one call
  3. Calculate position value and P&L per stock
  4. Sum up for the total portfolio view

Step 1: Define your holdings

JavaScript
Python

Step 2: Fetch live prices for all holdings

Fetch the full company list and filter to your held symbols. One API call covers the whole portfolio.

Step 3: Calculate P&L for each position

Step 4: Put it all together

Sample output

React component

React

Tips

Store holdings server-side. Keeping avg_cost and quantity in your own database means you can calculate P&L for any user without exposing API keys to the client. Refresh on the same cadence as the ticker. Prices update every 20 minutes during trading hours, so a 5-minute poll interval is a good balance between freshness and quota usage. Handle missing prices gracefully. If a symbol isn’t in the response (e.g. suspended stock), fall back to avg_cost so the position still appears with a 0% change rather than crashing.

Companies list reference

All fields returned by GET /companies

Live price ticker guide

How to keep prices updating automatically