Market Ranking

Calculates rankings across all listed companies based on revenue, assets, and growth rate. Provides both overall market rankings and within-sector rankings.

Usage

from dartlab.engines.rank import getRankOrBuild

rank = getRankOrBuild("005930")

rank.corpName              # "삼성전자"
rank.sector                # "IT"
rank.industryGroup         # "반도체"

# Revenue ranking
rank.revenueRank           # 3 (overall market)
rank.revenueTotal          # 2508 (total companies)
rank.revenueRankInSector   # 1 (within IT sector)
rank.revenueSectorTotal    # 122 (IT sector companies)

# Asset ranking
rank.assetRank             # 5
rank.assetRankInSector     # 2

# Growth ranking
rank.growthRank            # 320
rank.growthRankInSector    # 45

# Size class
rank.sizeClass             # "large"

Functions

getRankOrBuild()

getRankOrBuild(stockCode: str, *, verbose: bool = True) -> RankInfo | None

Returns immediately from cache if available; otherwise builds a full snapshot first.

ParameterTypeDescription
stockCodestrStock code
verboseboolPrint progress during build

getRank()

getRank(stockCode: str) -> RankInfo | None

Looks up from cache only. Returns None if cache does not exist.

buildSnapshot()

buildSnapshot(*, verbose: bool = True) -> dict[str, RankInfo]

Builds a full snapshot of all companies and saves to local cache. Takes approximately 2 minutes for 2,500+ companies.

Process:

  1. Load full KRX company list
  2. Classify sector + collect financial time series for each company
  3. Sort by revenue/assets/growth across all companies
  4. Calculate within-sector rankings separately
  5. Determine size class
  6. Save JSON cache ({dataDir}/_cache/rank_snapshot.json)

RankInfo

FieldTypeDescription
stockCodestrStock code
corpNamestrCompany name
sectorstrSector name (Korean)
industryGroupstrIndustry group name (Korean)
Financial Metrics
revenuefloat (optional)TTM revenue (KRW)
totalAssetsfloat (optional)Total assets (KRW)
revenueGrowth3Yfloat (optional)3-year revenue CAGR (%)
Revenue Ranking
revenueRankint (optional)Overall market rank
revenueTotalintTotal ranked companies
revenueRankInSectorint (optional)Within-sector rank
revenueSectorTotalintCompanies in sector
Asset Ranking
assetRankint (optional)Overall market rank
assetTotalintTotal ranked companies
assetRankInSectorint (optional)Within-sector rank
assetSectorTotalintCompanies in sector
Growth Ranking
growthRankint (optional)Overall market rank
growthTotalintTotal ranked companies
growthRankInSectorint (optional)Within-sector rank
growthSectorTotalintCompanies in sector
Classification
sizeClassstrSize class

Size Classes

ClassCriteria
largeTop 10% by revenue
midTop 10~30% by revenue
smallBelow top 30% by revenue

Examples

from dartlab.engines.rank import getRankOrBuild, buildSnapshot

# Single company lookup
rank = getRankOrBuild("000660")
print(f"{rank.corpName}: Revenue {rank.revenueRank}/{rank.revenueTotal}")
print(f"  Sector ({rank.sector}): {rank.revenueRankInSector}/{rank.revenueSectorTotal}")
print(f"  Size: {rank.sizeClass}")

# Build full snapshot (first time)
snapshot = buildSnapshot()
print(f"Ranked {len(snapshot)} companies")

# Top 10 in a specific sector
it_stocks = [(k, v) for k, v in snapshot.items() if v.sector == "IT" and v.revenueRankInSector]
it_stocks.sort(key=lambda x: x[1].revenueRankInSector)
for code, r in it_stocks[:10]:
    print(f"  #{r.revenueRankInSector}: {r.corpName} ({code})")

Cache

The snapshot is saved to {dataDir}/_cache/rank_snapshot.json. getRankOrBuild() loads from cache immediately if available; otherwise builds a new snapshot.

To refresh the cache, call buildSnapshot() directly.