Statistical

OpenAlgo Statistical Indicators Documentation

Statistical indicators analyze price data using mathematical and statistical methods to identify patterns, relationships, and forecast future price movements.

Import Statement

from openalgo import ta

Getting Market Data

from openalgo import api

client = api(api_key='your_api_key_here', host='http://127.0.0.1:5000')

# Fetch historical data
df = client.history(symbol="SBIN", 
                   exchange="NSE", 
                   interval="5m", 
                   start_date="2025-04-01", 
                   end_date="2025-04-08")

Available Statistical Indicators


Linear Regression (LINREG)

Linear Regression calculates the linear regression line for a given period using the least squares method to identify the underlying trend.

Usage

Parameters

  • data (array-like): Price data (typically closing prices)

  • period (int, default=14): Period for linear regression calculation

Returns

  • array: Linear regression values in the same format as input

Example


Linear Regression Slope (LRSLOPE)

Linear Regression Slope measures the rate of change of the linear regression line, indicating the strength and direction of the trend.

Usage

Parameters

  • data (array-like): Price data (typically closing prices)

  • period (int, default=100): Period for linear regression calculation

  • interval (int, default=1): Interval divisor for slope calculation

Returns

  • array: Slope values in the same format as input

Example


Pearson Correlation Coefficient (CORREL)

Correlation measures the statistical relationship between two data series, ranging from -1 (perfect negative correlation) to +1 (perfect positive correlation).

Usage

Parameters

  • data1 (array-like): First data series

  • data2 (array-like): Second data series

  • period (int, default=20): Period for correlation calculation

Returns

  • array: Correlation values in the same format as input

Example


Beta Coefficient (BETA)

Beta measures the volatility of a security relative to the market, indicating how much the security price moves relative to market movements.

Usage

Parameters

  • asset (array-like): Asset price data

  • market (array-like): Market price data (benchmark)

  • period (int, default=252): Period for beta calculation (typically 1 year = 252 trading days)

Returns

  • array: Beta values in the same format as input

Example


Variance (VAR)

Variance measures the dispersion of price data, supporting both logarithmic returns and price modes with smoothing and signal generation.

Usage

Parameters

  • data (array-like): Price data (close prices)

  • lookback (int, default=20): Variance lookback period

  • mode (str, default="PR"): Variance mode ("LR" for Logarithmic Returns, "PR" for Price)

  • ema_period (int, default=20): EMA period for variance smoothing

  • filter_lookback (int, default=20): Lookback period for variance filter

  • ema_length (int, default=14): EMA length for z-score smoothing

  • return_components (bool, default=False): If True, returns all components

Returns

  • array or tuple: Variance values or (variance, ema_variance, zscore, ema_zscore, stdev) if return_components=True

Example


Time Series Forecast (TSF)

Time Series Forecast predicts the next value using linear regression analysis.

Usage

Parameters

  • data (array-like): Price data

  • period (int, default=14): Period for forecast calculation

Returns

  • array: Time Series Forecast values in the same format as input

Example


Rolling Median (MEDIAN)

Rolling Median calculates the median value over a rolling window, which is less sensitive to outliers than mean-based indicators.

Usage

Parameters

  • data (array-like): Price data (default hl2 in Pine Script)

  • period (int, default=3): Period for median calculation

Returns

  • array: Median values in the same format as input

Example


Median Bands (MEDIAN_BANDS)

Median Bands combine median calculation with ATR-based bands and EMA smoothing for comprehensive analysis.

Usage

Parameters

  • high (array-like): High prices

  • low (array-like): Low prices

  • close (array-like): Close prices

  • source (array-like, optional): Source data for median (default: hl2)

  • median_length (int, default=3): Period for median calculation

  • atr_length (int, default=14): Period for ATR calculation

  • atr_mult (float, default=2.0): ATR multiplier for bands

Returns

  • tuple: (median, upper_band, lower_band, median_ema) arrays

Example


Rolling Mode (MODE)

Rolling Mode calculates the most frequent value over a rolling window using discretization.

Usage

Parameters

  • data (array-like): Price data

  • period (int, default=20): Period for mode calculation

  • bins (int, default=10): Number of bins for discretization

Returns

  • array: Mode values in the same format as input

Example


Complete Example: Statistical Analysis Dashboard

Advanced Statistical Analysis

Performance Tips

  1. Period Selection: Choose appropriate periods based on your analysis timeframe

  2. Data Quality: Ensure clean data for accurate statistical calculations

  3. Correlation Interpretation: Remember correlation doesn't imply causation

  4. Statistical Significance: Consider sample size when interpreting results

  5. Regime Changes: Monitor for changes in statistical relationships over time

Common Use Cases

  1. Trend Analysis: Use Linear Regression and slopes for trend identification

  2. Risk Management: Apply variance and correlation for portfolio risk assessment

  3. Anomaly Detection: Use statistical z-scores to identify unusual market behavior

  4. Forecasting: Combine TSF with other indicators for price prediction

  5. Market Relationships: Analyze correlations between different assets or timeframes

Last updated

Was this helpful?