indicator
indicator
Stateless market measurements for research, rules, and model inputs.
Native indicators are flat under q.indicator. Provider catalogs remain explicit as q.indicator.talib and q.indicator.pandas_ta and are imported only when accessed.
Functions
| Name | Description |
|---|---|
| bipower_variation | Calculate finite-sample-adjusted realized bipower variation. |
| ema | Calculate an exponential moving average. |
| log_returns | Calculate consecutive logarithmic returns from positive prices. |
| madev | Calculate rolling mean absolute deviation from a rolling SMA. |
| med_rv | Calculate the median realized-volatility estimator. |
| min_rv | Calculate the minimum realized-volatility estimator. |
| price_spikes | Classify close-to-close price spikes by volume confirmation. |
| realized_quarticity | Calculate realized quarticity. |
| realized_variance | Calculate realized variance as the sum of squared returns. |
| realized_volatility | Calculate realized-volatility measures for each intraday session. |
| relative_strength | Calculate asset return minus benchmark return over a lookback. |
| rs_days | Count recent positive relative-strength periods during a correction. |
| rs_phase | Flag and count consecutive periods above a relative-strength average. |
| rsma | Smooth a relative-strength Series. |
| rsnhbp | Flag a relative-strength high that precedes a price high. |
| sma | Calculate a simple moving average. |
| volume_spike_ratio | Classify volume spikes as trend continuations or reversals. |
bipower_variation
indicator.bipower_variation(returns)Calculate finite-sample-adjusted realized bipower variation.
ema
indicator.ema(series, window)Calculate an exponential moving average.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| series | pd.Series | Input series. | required |
| window | int | Exponential span, in periods. | required |
Returns
| Name | Type | Description |
|---|---|---|
| pd.Series | Exponential moving average with the input index preserved. |
log_returns
indicator.log_returns(prices)Calculate consecutive logarithmic returns from positive prices.
madev
indicator.madev(prices, window=20)Calculate rolling mean absolute deviation from a rolling SMA.
The absolute deviation from the window-period simple moving average is itself averaged over window periods. The first defined result therefore requires 2 * window - 1 observations.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| prices | pd.Series | Prices for one instrument. | required |
| window | int | Window used for both rolling calculations. | 20 |
Returns
| Name | Type | Description |
|---|---|---|
| pd.Series | MADEV Series preserving the input index. |
med_rv
indicator.med_rv(returns)Calculate the median realized-volatility estimator.
min_rv
indicator.min_rv(returns)Calculate the minimum realized-volatility estimator.
price_spikes
indicator.price_spikes(
high,
low,
close,
volume,
*,
atr_window=20,
volume_window=20,
atr_multiplier=1.7,
volume_threshold=1.5,
)Classify close-to-close price spikes by volume confirmation.
A price spike is an absolute close-to-close move greater than a multiple of simple rolling average true range. Event code 1 is a spike with high volume, event code 2 is a spike without high volume, and 0 means no spike.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| high | pd.Series | High prices for one instrument. | required |
| low | pd.Series | Low prices aligned exactly to high. |
required |
| close | pd.Series | Close prices aligned exactly to high. |
required |
| volume | pd.Series | Volume observations aligned exactly to high. |
required |
| atr_window | int | Window for simple rolling average true range. | 20 |
| volume_window | int | Window for average volume. | 20 |
| atr_multiplier | float | ATR multiple required for a price spike. | 1.7 |
| volume_threshold | float | Relative-volume boundary for confirmation. | 1.5 |
Returns
| Name | Type | Description |
|---|---|---|
| pd.DataFrame | DataFrame containing price_spike_event and relative_volume. |
realized_quarticity
indicator.realized_quarticity(returns)Calculate realized quarticity.
realized_variance
indicator.realized_variance(returns)Calculate realized variance as the sum of squared returns.
realized_volatility
indicator.realized_volatility(
data,
*,
time_col='time',
price_col='price',
session_col='session',
)Calculate realized-volatility measures for each intraday session.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| data | pd.DataFrame | Intraday observations for one instrument. | required |
| time_col | str | Observation timestamp column. | 'time' |
| price_col | str | Positive trade or sampled-price column. | 'price' |
| session_col | str | Explicit session label column. | 'session' |
Returns
| Name | Type | Description |
|---|---|---|
| pd.DataFrame | DataFrame indexed by session with one column per estimator. |
Raises
| Name | Type | Description |
|---|---|---|
| ValueError | If required data is missing or a session has fewer than four prices (three returns). |
relative_strength
indicator.relative_strength(prices, benchmark, lookback=21)Calculate asset return minus benchmark return over a lookback.
The benchmark is aligned to the asset index without filling missing dates.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| prices | pd.Series | Prices for one asset. | required |
| benchmark | pd.Series | Benchmark prices. | required |
| lookback | int | Return lookback, in periods. | 21 |
Returns
| Name | Type | Description |
|---|---|---|
| pd.Series | Relative-strength Series preserving the asset index. |
rs_days
indicator.rs_days(
relative_strength,
benchmark,
window=60,
correction_threshold=0.95,
)Count recent positive relative-strength periods during a correction.
A correction is a benchmark price below correction_threshold times its rolling high.
rs_phase
indicator.rs_phase(relative_strength, moving_average)Flag and count consecutive periods above a relative-strength average.
rsma
indicator.rsma(relative_strength, window=21, method='exponential')Smooth a relative-strength Series.
rsnhbp
indicator.rsnhbp(prices, relative_strength, window=60)Flag a relative-strength high that precedes a price high.
sma
indicator.sma(series, window)Calculate a simple moving average.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| series | pd.Series | Input series. | required |
| window | int | Rolling window size, in periods. | required |
Returns
| Name | Type | Description |
|---|---|---|
| pd.Series | Rolling mean with the input index preserved. |
Examples
>>> q.indicator.sma(prices["close"], 20)volume_spike_ratio
indicator.volume_spike_ratio(
prices,
volume,
*,
vol_window=20,
price_window=20,
spike_threshold=1.5,
reversal_threshold=2.5,
trend_slope_threshold=0.1,
consecutive_days=3,
)Classify volume spikes as trend continuations or reversals.
A continuation is moderate relative volume in the established trend direction. A reversal is extreme relative volume with price moving against the established trend.
Event codes in vsr_spike_type are 0 for no spike, 1 for continuation, and 2 for reversal. Trend codes in vsr_trending are 0 for no trend, 1 for uptrend, and 2 for downtrend.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| prices | pd.Series | Close prices for one instrument. | required |
| volume | pd.Series | Volume observations aligned exactly to prices. |
required |
| vol_window | int | Window used for average volume. | 20 |
| price_window | int | Window used for the price SMA. | 20 |
| spike_threshold | float | Lower exclusive relative-volume continuation bound. | 1.5 |
| reversal_threshold | float | Lower inclusive relative-volume reversal bound. | 2.5 |
| trend_slope_threshold | float | Minimum absolute SMA percentage change. | 0.1 |
| consecutive_days | int | Consecutive slope observations required for a trend. | 3 |
Returns
| Name | Type | Description |
|---|---|---|
| pd.DataFrame | DataFrame containing vsr, vsr_spike_type, and vsr_trending. |