Beta & Relative Strength Indicators¶
This module provides beta calculation and market regime detection based on relative strength analysis.
Core Functions¶
beta
¶
Beta and relative strength regime analysis module.
Implements Mike McGlone's approach to regime detection: - Relative strength ratio (asset/benchmark) vs its moving average - Rolling beta calculation using covariance/variance - Regime signal: "risk-on" when ratio above MA, "risk-off" when below
Public API
compute_rolling_beta(asset_returns, benchmark_returns, window=60) -> pd.Series compute_relative_strength(asset_close, benchmark_close) -> pd.Series compute_regime_signal(ratio, ma_period=200) -> dict analyze_beta_regime(asset_df, benchmark_df, ma_period=200, beta_window=60) -> dict
compute_rolling_beta(asset_returns, benchmark_returns, window=60)
¶
Compute rolling beta of asset relative to benchmark.
Beta = Cov(asset_returns, benchmark_returns) / Var(benchmark_returns)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
asset_returns
|
Series
|
Series of asset percentage returns. |
required |
benchmark_returns
|
Series
|
Series of benchmark percentage returns. |
required |
window
|
int
|
Rolling window size for beta calculation (default: 60 periods). |
60
|
Returns:
| Type | Description |
|---|---|
Series
|
Series of rolling beta values. NaN where insufficient history. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If window < 2. |
Source code in src/stockcharts/indicators/beta.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
compute_relative_strength(asset_close, benchmark_close)
¶
Compute relative strength ratio of asset vs benchmark.
Relative Strength = Asset Price / Benchmark Price
This ratio shows whether the asset is outperforming (rising ratio) or underperforming (falling ratio) the benchmark.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
asset_close
|
Series
|
Series of asset closing prices. |
required |
benchmark_close
|
Series
|
Series of benchmark closing prices. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
Series of relative strength ratios. |
Source code in src/stockcharts/indicators/beta.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
compute_regime_signal(ratio, ma_period=200)
¶
Determine regime signal based on ratio vs its moving average.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ratio
|
Series
|
Series of relative strength ratios. |
required |
ma_period
|
int
|
Moving average period for regime detection (default: 200). |
200
|
Returns:
| Type | Description |
|---|---|
dict
|
dict with keys: - 'ratio': current ratio value - 'ma': current moving average value - 'regime': "risk-on" if ratio > ma, "risk-off" if ratio <= ma - 'pct_from_ma': percentage distance from MA - 'ratio_series': full ratio series - 'ma_series': full MA series |
Source code in src/stockcharts/indicators/beta.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
analyze_beta_regime(asset_df, benchmark_df, ma_period=200, beta_window=60)
¶
Complete beta regime analysis for an asset vs benchmark.
Combines relative strength ratio, regime signal, and rolling beta.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
asset_df
|
DataFrame
|
DataFrame with 'Close' column for the asset. |
required |
benchmark_df
|
DataFrame
|
DataFrame with 'Close' column for the benchmark. |
required |
ma_period
|
int
|
Moving average period for regime detection (default: 200). |
200
|
beta_window
|
int
|
Rolling window for beta calculation (default: 60). |
60
|
Returns:
| Type | Description |
|---|---|
dict
|
dict with keys: - 'relative_strength': current RS ratio - 'rs_ma': RS moving average value - 'regime': "risk-on" or "risk-off" - 'pct_from_ma': percentage distance from MA - 'rolling_beta': current rolling beta value - 'asset_price': current asset price - 'benchmark_price': current benchmark price - 'rs_series': full RS ratio series - 'rs_ma_series': full RS MA series - 'beta_series': full rolling beta series |
Source code in src/stockcharts/indicators/beta.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |