Ranking Functions

How assets are scored and selected for portfolio construction.

Momentum Ranking

Ranks assets by price momentum — the difference between the current price and the price t periods ago. Higher momentum = higher rank.

Formula
Score(i) = P(i, today) - P(i, today - t)

Assets with the largest positive price change over t periods are ranked highest. The simulator handles the sorting (descending).

Parameters

NameTypeDefaultDescription
pricesDataFrameDataFrame of historical prices (datetime index, symbol columns)
tint10Lookback period in trading days

How It Works

Computes simple price difference over the lookback window for each asset. Returns a Series of momentum values. The TPS simulator sorts descending to select top assets.

Live Demo — 10 Stocks Ranked

Sample data: 252 trading days, seeded random walk

RankSymbolNameScoreSelected
#1TSLATesla Inc.22.7286Top 5
#2MSFTMicrosoft Corp.9.1736Top 5
#3GOOGLAlphabet Inc.4.1126Top 5
#4METAMeta Platforms Inc.2.0901Top 5
#5NVDANVIDIA Corp.0.8989Top 5
#6JPMJPMorgan Chase & Co.-0.1532Excluded
#7AMZNAmazon.com Inc.-2.0967Excluded
#8AAPLApple Inc.-2.1140Excluded
#9JNJJohnson & Johnson-2.1368Excluded
#10VVisa Inc.-5.6223Excluded

When to Use

Use when you believe recent winners will continue to outperform. Standard in cross-sectional momentum strategies.

Code Example

from tradepilot.ranking import momentum_ranking

scores = momentum_ranking(prices, t=10)
# Returns pd.Series with momentum scores per asset
# Simulator selects top N by descending score

Random Ranking

Assigns random scores to each asset — useful for testing, benchmarking, and Monte Carlo simulations.

Formula
Score(i) = random()  ∈ [0, 1)

Each asset gets a uniform random value. Optional seed parameter for reproducibility.

Parameters

NameTypeDefaultDescription
pricesDataFrameDataFrame of historical prices (only used for column names)
seedint | NoneNoneRandom seed for reproducibility

How It Works

Uses numpy random to generate a random score for each asset column. With a fixed seed, produces reproducible rankings for testing.

Live Demo — 10 Stocks Ranked

Sample data: 252 trading days, seeded random walk

RankSymbolNameScoreSelected
#1METAMeta Platforms Inc.0.9759Top 5
#2GOOGLAlphabet Inc.0.7354Top 5
#3VVisa Inc.0.5304Top 5
#4MSFTMicrosoft Corp.0.5246Top 5
#5JPMJPMorgan Chase & Co.0.5123Top 5
#6TSLATesla Inc.0.3762Excluded
#7AMZNAmazon.com Inc.0.2633Excluded
#8JNJJohnson & Johnson0.2571Excluded
#9NVDANVIDIA Corp.0.1963Excluded
#10AAPLApple Inc.0.0003Excluded

When to Use

Use as a baseline benchmark to verify that your real strategy outperforms random selection. Essential for statistical significance testing.

Code Example

from tradepilot.ranking import random_ranking

scores = random_ranking(prices, seed=42)
# Returns pd.Series with random scores [0, 1)
# Use seed for reproducible results

VaR Ranking

Ranks assets by historic Value at Risk — lower VaR means lower risk. Selects the safest assets first.

Formula
Score(i) = VaR_historic(i, t, level)

VaR is the loss threshold such that only `level`% of returns fall below it. Lower VaR = less risky = ranked first.

Parameters

NameTypeDefaultDescription
pricesDataFrameDataFrame of historical prices
tint100Number of past periods to consider (0 = all)
levelint5Percentile level for VaR calculation

How It Works

Converts prices to returns, then computes the historic VaR at the given percentile for each asset. Assets with lower VaR (less risk) are preferred. The simulator sorts ascending to select lowest-risk assets.

Live Demo — 10 Stocks Ranked

Sample data: 252 trading days, seeded random walk

RankSymbolNameScoreSelected
#1VVisa Inc.0.0140Top 5
#2JPMJPMorgan Chase & Co.0.0141Top 5
#3METAMeta Platforms Inc.0.0156Top 5
#4GOOGLAlphabet Inc.0.0179Top 5
#5JNJJohnson & Johnson0.0180Top 5
#6NVDANVIDIA Corp.0.0184Excluded
#7AAPLApple Inc.0.0186Excluded
#8MSFTMicrosoft Corp.0.0192Excluded
#9AMZNAmazon.com Inc.0.0200Excluded
#10TSLATesla Inc.0.0353Excluded

When to Use

Use for risk-averse portfolio construction. Prioritizes capital preservation by selecting assets with the smallest tail risk.

Code Example

from tradepilot.ranking import var_ranking

scores = var_ranking(prices, t=100, level=5)
# Returns pd.Series with VaR values per asset
# Lower VaR = less risky = ranked first