Calculations › Market Discovery

Show a sector overview across the market (count, average price, average YTD change)

The query

-- table market-wide: sector overview (count, avg price, avg YTD change)
WITH latest AS (
  SELECT DISTINCT ON (tp.ticketnameid) tp.ticketnameid, tp.price AS price_now
  FROM ticketprice tp JOIN ticketname tn ON tn.id = tp.ticketnameid
  WHERE tn.tickettype = 'stock'
  ORDER BY tp.ticketnameid, tp.datetime DESC
),
year_start AS (
  SELECT DISTINCT ON (tp.ticketnameid) tp.ticketnameid, tp.price AS price_start
  FROM ticketprice tp JOIN ticketname tn ON tn.id = tp.ticketnameid
  WHERE tn.tickettype = 'stock'
    AND tp.datetime >= EXTRACT(EPOCH FROM DATE_TRUNC('year', NOW()))::bigint
  ORDER BY tp.ticketnameid, tp.datetime ASC
)
SELECT sm.sector, COUNT(*) AS n, AVG(l.price_now) AS avg_price,
       AVG((l.price_now - y.price_start) / NULLIF(y.price_start,0) * 100) AS avg_ytd_pct_change
FROM latest l
JOIN year_start y ON y.ticketnameid = l.ticketnameid
JOIN symbolmetadata sm ON sm.ticketnameid = l.ticketnameid
WHERE sm.sector IS NOT NULL AND sm.sector != '' AND y.price_start >= 0.5
GROUP BY sm.sector
ORDER BY avg_ytd_pct_change DESC;

Runs against Cryptobi's market tables — ticketname (symbols), ticketprice (daily prices and volume) and symbolmetadata (sector, exchange, market cap and other reference fields).

Current output

SectorCountAvg priceAvg YTD % change
Energy 17 127.67 25.40
Consumer Defensive 28 108.92 19.34
Communication Services 31 116.07 10.06
Industrials 129 158.36 4.55
Financial Services 72 177.59 3.63
Healthcare 76 139.46 0.02
Technology 78 109.44 -3.65
Utilities 6 13.72 -4.27
Consumer Cyclical 58 144.47 -8.26
Basic Materials 30 114.92 -9.39
Real Estate 39 76.74 -11.34

Live output, first 11 rows, refreshed periodically. Model estimates are trend projections from recent prices, not financial advice.

Run this on your own holdings, change it, or write your own in SQL or Python — then put it on a dashboard or have it emailed on a schedule. Free, no credit card.

Sign Up Free

Related calculations