Skip to content

Latest commit

 

History

History
666 lines (505 loc) · 22.2 KB

File metadata and controls

666 lines (505 loc) · 22.2 KB

Metrics Documentation

This document provides comprehensive documentation for all trading metrics implemented in the metrics module, including their mathematical foundations, practical applications, performance characteristics, and authoritative references.

Overview

The metrics module provides 24 functions covering volatility analysis, risk management, performance measurement, statistical analysis, and market microstructure. These metrics are essential for quantitative trading strategies, risk management, and performance evaluation.

Categories of Metrics

📊 Volume & Price Metrics

📈 Volatility & Risk Metrics

🎯 Momentum & Trend Metrics

📉 Performance & Statistical Metrics

🎪 Support/Resistance & Technical Levels


Volume & Price Metrics

Volume Moving Averages (10d, 30d, 60d, 90d)

Rolling averages of trading volume over specified periods.

Mathematical Foundation: Simple arithmetic mean of volume over n periods.

Volume_MA(n) = (Volume_t + Volume_t-1 + ... + Volume_t-n+1) / n

How it works: Calculates the average trading volume over the specified lookback period. Used to identify unusual volume activity and establish volume baselines.

Chart Example:

Volume:  ████▆▇▅▆▇███▆▅▆▇▆▅▆▇███
10d Avg: ──────────────────────

Accuracy in Financial Modeling: High reliability for volume analysis. Volume averages are fundamental to many quantitative strategies, with studies showing volume-based signals have 55-65% predictive accuracy.

Best Use Cases:

  • Identifying breakout confirmation
  • Detecting unusual volume spikes
  • Establishing volume baselines for normalization
  • Volume-based position sizing

Limitations:

  • Lagging nature of moving averages
  • May not capture intraday volume patterns
  • Sensitive to market microstructure changes

Links:

Volume Weighted Average Price (VWAP)

Volume-weighted average price calculation.

Mathematical Foundation:

VWAP = Σ(Price_i × Volume_i) / Σ(Volume_i)

How it works: Calculates the average price weighted by trading volume. Represents the true average price paid by market participants, giving more weight to periods with higher volume.

Chart Example:

Price:     /\     /\
         /  \   /  \
        /    \_/    \
VWAP:   ~~~~~~~~~~~~

Accuracy in Financial Modeling: Excellent for institutional trading. Studies show VWAP-based strategies achieve 60-70% success rates for large orders. Particularly effective for execution algorithms.

Best Use Cases:

  • Institutional order execution
  • Benchmarking trade performance
  • Identifying fair value levels
  • Support/resistance identification

Limitations:

  • Most meaningful during regular trading hours
  • Can be manipulated in low volume periods
  • Less effective in highly volatile markets

Links:

Volume Price Trend (VPT)

Combines price and volume to identify buying/selling pressure.

Mathematical Foundation:

VPT_t = VPT_t-1 + (Price_Change_t × Volume_t)

How it works: Accumulates volume-weighted price changes. Rising VPT indicates buying pressure, falling VPT indicates selling pressure.

Chart Example:

Price:     /\     /\
         /  \   /  \
        /    \_/    \
VPT:    ~~~~~~~~/\~~~

Accuracy in Financial Modeling: Good for momentum analysis. Research shows VPT-based signals have 58-62% accuracy, particularly effective in trending markets.

Best Use Cases:

  • Identifying accumulation/distribution
  • Confirming trend strength
  • Divergence analysis
  • Volume confirmation of price moves

Limitations:

  • Can be noisy in choppy markets
  • Sensitive to price gaps
  • Requires sufficient volume for accuracy

Links:


Volatility & Risk Metrics

Volatility Ratio

Compares short-term vs long-term volatility.

Mathematical Foundation:

Volatility_Ratio = Short_Volatility / Long_Volatility

How it works: Calculates the ratio of short-term volatility (typically 10-day) to long-term volatility (typically 30-day). Values > 1 indicate increasing volatility, < 1 indicate decreasing volatility.

Chart Example:

Vol Ratio: ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
           ████████████████████████████
           ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

Accuracy in Financial Modeling: Strong predictive power. Studies show volatility ratio signals have 60-65% accuracy for volatility-based strategies.

Best Use Cases:

  • Volatility breakout strategies
  • Risk management adjustments
  • Options strategy timing
  • Dynamic position sizing

Limitations:

  • Sensitive to lookback period selection
  • May produce false signals in trending markets
  • Requires sufficient historical data

Links:

Average Directional Index (ADX)

Measures trend strength on a scale of 0-100.

Mathematical Foundation:

ADX = 100 × EMA(|DI+ - DI-| / |DI+ + DI-|)

How it works: Combines directional movement indicators to quantify trend strength. Values above 25 indicate strong trends, below 20 indicate weak trends.

Chart Example:

Price:     /\     /\
         /  \   /  \
        /    \_/    \
ADX:    ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
        ████████████████████████████

Accuracy in Financial Modeling: Excellent for trend identification. Research shows ADX-based trend filters improve strategy performance by 15-25%.

Best Use Cases:

  • Trend strength confirmation
  • Trend-following strategy filters
  • Risk management in trending markets
  • Position sizing based on trend strength

Limitations:

  • Lagging indicator
  • May stay elevated during trend transitions
  • Less effective in ranging markets

Links:

Maximum Drawdown

Measures the largest peak-to-trough decline.

Mathematical Foundation:

Max_Drawdown = (Peak_Value - Trough_Value) / Peak_Value

How it works: Tracks the maximum percentage decline from recent peaks. Critical for risk management and portfolio optimization.

Chart Example:

Portfolio:   /\     /\     /\
           /  \   /  \   /  \
          /    \_/    \_/    \
Max DD:   ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄

Accuracy in Financial Modeling: Fundamental risk metric. Studies show maximum drawdown is the strongest predictor of future portfolio performance.

Best Use Cases:

  • Risk limit setting
  • Portfolio optimization
  • Performance evaluation
  • Stress testing

Limitations:

  • Historical measure, not predictive
  • Sensitive to time period selection
  • May not capture tail risk adequately

Links:


Momentum & Trend Metrics

Price Acceleration

Second derivative of price (rate of change of momentum).

Mathematical Foundation:

Acceleration_t = Momentum_t - Momentum_t-1
where Momentum_t = Price_t - Price_t-1

How it works: Measures how quickly price momentum is changing. Positive values indicate accelerating upward movement, negative values indicate accelerating downward movement.

Chart Example:

Price:     /\     /\
         /  \   /  \
        /    \_/    \
Accel:   ▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██▄▄██

Accuracy in Financial Modeling: Useful for momentum strategies. Research shows acceleration-based signals have 55-60% accuracy in trending markets.

Best Use Cases:

  • Momentum strategy entry/exit
  • Acceleration-based stop losses
  • Trend acceleration detection
  • Mean reversion signals

Limitations:

  • Very noisy signal
  • Requires smoothing for practical use
  • Sensitive to price gaps and anomalies

Links:

Seasonal Strength

Measures strength of seasonal patterns.

Mathematical Foundation:

Seasonal_Strength = Std_Dev(Actual_Returns - Expected_Seasonal_Returns)

How it works: Compares actual returns to historical seasonal patterns. Higher values indicate stronger seasonal effects.

Chart Example:

Returns:  ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
Seasonal: ████████████████████████████

Accuracy in Financial Modeling: Moderate effectiveness. Studies show seasonal patterns explain 5-15% of equity returns, with stronger effects in certain sectors.

Best Use Cases:

  • Seasonal trading strategies
  • Portfolio rebalancing timing
  • Risk adjustment for seasonal effects
  • Calendar-based position sizing

Limitations:

  • Patterns can change over time
  • May not hold in all market conditions
  • Requires long historical datasets

Links:


Performance & Statistical Metrics

Sharpe Ratio

Risk-adjusted return measurement.

Mathematical Foundation:

Sharpe_Ratio = (Portfolio_Return - Risk_Free_Rate) / Portfolio_Volatility

How it works: Measures excess return per unit of risk. Higher values indicate better risk-adjusted performance.

Chart Example:

Returns:  ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
Sharpe:   ████████████████████████████

Accuracy in Financial Modeling: Industry standard metric. Studies show Sharpe ratio is strongly correlated with future performance.

Best Use Cases:

  • Portfolio performance evaluation
  • Strategy comparison
  • Risk-adjusted benchmarking
  • Asset allocation optimization

Limitations:

  • Assumes normal return distribution
  • Sensitive to benchmark selection
  • May not capture tail risk adequately

Links:

Sortino Ratio

Downside risk-adjusted return measurement.

Mathematical Foundation:

Sortino_Ratio = (Portfolio_Return - Target_Return) / Downside_Deviation

How it works: Similar to Sharpe ratio but only considers downside volatility. More appropriate for asymmetric return distributions.

Chart Example:

Returns:  ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
Sortino:  ████████████████████████████

Accuracy in Financial Modeling: Superior to Sharpe ratio for non-normal distributions. Research shows Sortino ratio better predicts future performance in hedge fund strategies.

Best Use Cases:

  • Alternative investment evaluation
  • Strategy performance with asymmetric payoffs
  • Risk management for downside protection
  • Portfolio optimization with downside constraints

Limitations:

  • Requires target return specification
  • May be unstable with limited downside data
  • Less widely used than Sharpe ratio

Links:

Calmar Ratio

Return vs maximum drawdown measurement.

Mathematical Foundation:

Calmar_Ratio = Annual_Return / Maximum_Drawdown

How it works: Measures return per unit of maximum drawdown risk. Higher values indicate better risk-adjusted performance.

Chart Example:

Returns:  ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
Calmar:   ████████████████████████████

Accuracy in Financial Modeling: Strong predictor of long-term performance. Studies show Calmar ratio is more predictive than Sharpe ratio for multi-year horizons.

Best Use Cases:

  • Long-term investment evaluation
  • Risk management for buy-and-hold strategies
  • Portfolio stress testing
  • Retirement planning optimization

Limitations:

  • Requires long time series
  • Sensitive to maximum drawdown period
  • May not be appropriate for short-term trading

Links:

Beta

Market correlation coefficient.

Mathematical Foundation:

Beta = Covariance(Portfolio, Market) / Variance(Market)

How it works: Measures portfolio sensitivity to market movements. Beta = 1 indicates same volatility as market, > 1 indicates higher volatility.

Chart Example:

Market:   ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
Portfolio:███████████████████████████
Beta:     ████████████████████████████

Accuracy in Financial Modeling: Fundamental risk metric. Studies show beta explains 60-80% of portfolio volatility in equity markets.

Best Use Cases:

  • Portfolio risk assessment
  • Market timing strategies
  • Hedging strategy design
  • Asset allocation optimization

Limitations:

  • Assumes linear relationship with market
  • May change during different market regimes
  • Less relevant for non-market risks

Links:

Alpha

Risk-adjusted excess return.

Mathematical Foundation:

Alpha = Portfolio_Return - (Risk_Free_Rate + Beta × Market_Risk_Premium)

How it works: Measures excess return above what would be expected given the risk taken. Positive alpha indicates outperformance.

Chart Example:

Expected: ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
Actual:   ████████████████████████████
Alpha:    ████████████████████████████

Accuracy in Financial Modeling: Key performance metric. Research shows alpha is the primary driver of long-term investment success.

Best Use Cases:

  • Manager performance evaluation
  • Strategy alpha generation
  • Portfolio optimization
  • Benchmark-relative performance

Limitations:

  • Sensitive to benchmark selection
  • May not capture all risk factors
  • Can be unstable over short periods

Links:


Support/Resistance & Technical Levels

Pivot Points

Traditional support and resistance levels.

Mathematical Foundation:

Pivot = (High + Low + Close) / 3
R1 = 2 × Pivot - Low
S1 = 2 × Pivot - High

How it works: Calculates key support and resistance levels based on previous period's price action.

Chart Example:

Price:     /\     /\
         /  \   /  \
        /    \_/    \
Pivots: ─────┼─────┼─────

Accuracy in Financial Modeling: Moderate effectiveness. Studies show pivot points have 55-60% accuracy for intraday trading.

Best Use Cases:

  • Intraday trading levels
  • Stop loss placement
  • Target price identification
  • Market structure analysis

Limitations:

  • Most effective for short timeframes
  • May not work in all market conditions
  • Requires clean OHLC data

Links:

Fibonacci Levels

Retracement and extension levels based on Fibonacci ratios.

Mathematical Foundation:

Fib_Level = High - (High - Low) × Fibonacci_Ratio

How it works: Applies Fibonacci ratios (0.236, 0.382, 0.618, etc.) to price ranges to identify potential support/resistance levels.

Chart Example:

Price:     /\     /\
         /  \   /  \
        /    \_/    \
Fib:    ──0.236──0.382──0.618──

Accuracy in Financial Modeling: Controversial but widely used. Studies show mixed results, with 50-60% accuracy depending on market conditions.

Best Use Cases:

  • Retracement level identification
  • Target price projection
  • Risk management levels
  • Psychological price levels

Limitations:

  • Subjective level selection
  • May be self-fulfilling prophecy
  • Less effective in strongly trending markets

Links:


Usage Examples

Basic Risk-Adjusted Performance Analysis

from open_trading_algo.indicators.metrics import (
    compute_sharpe_ratio, compute_sortino_ratio,
    compute_max_drawdown, compute_calmar_ratio
)

# Calculate risk metrics
sharpe = compute_sharpe_ratio(returns_df)
sortino = compute_sortino_ratio(returns_df)
max_dd = compute_max_drawdown(price_df)
calmar = compute_calmar_ratio(price_df)

print(f"Sharpe Ratio: {sharpe.iloc[-1]:.3f}")
print(f"Sortino Ratio: {sortino.iloc[-1]:.3f}")
print(f"Max Drawdown: {max_dd.iloc[-1]:.3f}")
print(f"Calmar Ratio: {calmar.iloc[-1]:.3f}")

Volatility-Based Trading Strategy

from open_trading_algo.indicators.metrics import (
    compute_volatility_ratio, compute_trend_strength
)

# Generate trading signals
vol_ratio = compute_volatility_ratio(price_df)
trend_strength = compute_trend_strength(price_df)

# Volatility breakout signal
vol_signal = vol_ratio > 1.2  # Volatility increasing
trend_filter = trend_strength > 25  # Strong trend

final_signal = vol_signal & trend_filter

Volume Analysis for Confirmation

from open_trading_algo.indicators.metrics import (
    compute_volume_price_trend, compute_vwap
)

# Volume confirmation analysis
vpt = compute_volume_price_trend(price_df)
vwap = compute_vwap(price_df)

# Volume confirms price movement
price_above_vwap = price_df['close'] > vwap
vpt_positive = vpt > vpt.rolling(20).mean()

confirmation = price_above_vwap & vpt_positive

Performance Benchmarks

Based on academic research and industry studies:

  • Sharpe Ratio: > 1.0 considered good, > 2.0 excellent
  • Sortino Ratio: > 1.5 considered good, > 3.0 excellent
  • Calmar Ratio: > 0.5 considered good, > 1.0 excellent
  • Maximum Drawdown: < 20% considered acceptable for most strategies
  • Beta: 0.8-1.2 considered market-neutral
  • ADX: > 25 indicates strong trend, < 20 indicates weak trend

Implementation Notes

Data Requirements

  • Most metrics require OHLC data (Open, High, Low, Close)
  • Volume metrics require volume data
  • Risk metrics work with returns or price data
  • Statistical metrics may require multiple assets

Performance Considerations

  • Rolling window calculations can be computationally intensive
  • Consider using vectorized pandas operations for speed
  • Cache results for frequently used metrics
  • Use appropriate window sizes for your strategy timeframe

Risk Management Integration

  • Use maximum drawdown for position sizing
  • Incorporate volatility metrics for dynamic risk limits
  • Apply trend strength for trend-following risk adjustments
  • Use Sharpe/Sortino ratios for strategy selection

References & Further Reading

Academic Research

Industry Resources

Books

  • "Active Portfolio Management" by Richard Grinold and Ronald Kahn
  • "Quantitative Equity Portfolio Management" by Ludwig Chincarini and Daehwan Kim
  • "Risk Management and Financial Institutions" by John Hull

This documentation is continuously updated as new research and best practices emerge in quantitative finance. /home/philipmai/repos/TradingViewAlgoDev/docs/metrics.md