Python wrapper for TA-LIB providing 175+ technical analysis indicators for financial market data
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Statistical analysis functions for correlation, regression, and statistical measures of price data. These functions provide mathematical insights into price relationships, trends, and variability patterns.
Measures the sensitivity of a security's returns relative to market returns, indicating systematic risk.
def BETA(real0, real1, timeperiod=5):
"""
Beta
Calculates beta coefficient measuring correlation and relative volatility between two price series.
Parameters:
- real0: array-like, dependent variable (typically individual security)
- real1: array-like, independent variable (typically market index)
- timeperiod: int, number of periods for calculation (default: 5)
Returns:
numpy.ndarray: Beta coefficient values
"""Measures the linear correlation between two price series, ranging from -1 to +1.
def CORREL(real0, real1, timeperiod=30):
"""
Pearson's Correlation Coefficient (r)
Measures linear relationship strength between two data series.
Parameters:
- real0: array-like, first data series
- real1: array-like, second data series
- timeperiod: int, number of periods for calculation (default: 30)
Returns:
numpy.ndarray: Correlation coefficient values (-1 to +1)
"""Calculates linear regression line values, providing trend line based on least squares method.
def LINEARREG(real, timeperiod=14):
"""
Linear Regression
Calculates linear regression line values using least squares method.
Parameters:
- real: array-like, input price data
- timeperiod: int, number of periods for regression (default: 14)
Returns:
numpy.ndarray: Linear regression line values
"""Calculates the angle of the linear regression line, indicating trend steepness.
def LINEARREG_ANGLE(real, timeperiod=14):
"""
Linear Regression Angle
Calculates the angle of the linear regression line in degrees.
Parameters:
- real: array-like, input price data
- timeperiod: int, number of periods for regression (default: 14)
Returns:
numpy.ndarray: Regression line angle values (degrees)
"""Calculates the y-intercept of the linear regression line.
def LINEARREG_INTERCEPT(real, timeperiod=14):
"""
Linear Regression Intercept
Calculates the y-intercept of the linear regression line.
Parameters:
- real: array-like, input price data
- timeperiod: int, number of periods for regression (default: 14)
Returns:
numpy.ndarray: Regression line intercept values
"""Calculates the slope of the linear regression line, indicating trend direction and strength.
def LINEARREG_SLOPE(real, timeperiod=14):
"""
Linear Regression Slope
Calculates the slope of the linear regression line.
Parameters:
- real: array-like, input price data
- timeperiod: int, number of periods for regression (default: 14)
Returns:
numpy.ndarray: Regression line slope values
"""Measures the dispersion of price data around the mean, indicating volatility.
def STDDEV(real, timeperiod=5, nbdev=1):
"""
Standard Deviation
Calculates standard deviation of price data over specified period.
Parameters:
- real: array-like, input price data
- timeperiod: int, number of periods (default: 5)
- nbdev: float, number of deviations (default: 1)
Returns:
numpy.ndarray: Standard deviation values
"""Projects future price values based on linear regression analysis.
def TSF(real, timeperiod=14):
"""
Time Series Forecast
Forecasts next period value using linear regression.
Parameters:
- real: array-like, input price data
- timeperiod: int, number of periods for forecast calculation (default: 14)
Returns:
numpy.ndarray: Forecasted values
"""Calculates the variance of price data, measuring the average of squared deviations from the mean.
def VAR(real, timeperiod=5, nbdev=1):
"""
Variance
Calculates variance of price data over specified period.
Parameters:
- real: array-like, input price data
- timeperiod: int, number of periods (default: 5)
- nbdev: float, number of deviations (default: 1)
Returns:
numpy.ndarray: Variance values
"""Calculates the average deviation from the mean, providing a measure of dispersion that is less sensitive to outliers than standard deviation.
def AVGDEV(real, timeperiod=14):
"""
Average Deviation
Calculates average deviation from the mean over specified period.
Provides a robust measure of dispersion less sensitive to outliers.
Parameters:
- real: array-like, input price data
- timeperiod: int, number of periods (default: 14)
Returns:
numpy.ndarray: Average deviation values
"""import talib
import numpy as np
# Sample data - individual stock and market index
stock_prices = np.array([100, 102, 101, 103, 105, 104, 106, 108, 107, 109])
market_prices = np.array([1000, 1020, 1010, 1025, 1040, 1035, 1050, 1065, 1055, 1070])
# Calculate statistical measures
beta = talib.BETA(stock_prices, market_prices, timeperiod=5)
correlation = talib.CORREL(stock_prices, market_prices, timeperiod=5)
linear_reg = talib.LINEARREG(stock_prices, timeperiod=5)
reg_slope = talib.LINEARREG_SLOPE(stock_prices, timeperiod=5)
reg_angle = talib.LINEARREG_ANGLE(stock_prices, timeperiod=5)
std_dev = talib.STDDEV(stock_prices, timeperiod=5)
variance = talib.VAR(stock_prices, timeperiod=5)
forecast = talib.TSF(stock_prices, timeperiod=5)
print("Statistical Analysis Results:")
print(f"Beta: {beta[-1]:.3f}")
print(f"Correlation: {correlation[-1]:.3f}")
print(f"Current Price: {stock_prices[-1]}")
print(f"Linear Regression: {linear_reg[-1]:.2f}")
print(f"Trend Slope: {reg_slope[-1]:.4f}")
print(f"Trend Angle: {reg_angle[-1]:.2f} degrees")
print(f"Standard Deviation: {std_dev[-1]:.3f}")
print(f"Variance: {variance[-1]:.3f}")
print(f"Next Period Forecast: {forecast[-1]:.2f}")
# Interpretation:
# - Beta > 1: Stock is more volatile than market
# - Correlation near 1: Strong positive relationship with market
# - Positive slope/angle: Upward trend
# - High std dev/variance: High volatility