CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-ta-lib

Python wrapper for TA-LIB providing 175+ technical analysis indicators for financial market data

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

math-operations.mddocs/

Math Operations

Mathematical transformation and operator functions for array manipulation and calculations. These functions provide fundamental mathematical operations on price data arrays, enabling custom indicator development and data preprocessing.

Capabilities

Basic Arithmetic Operations

def ADD(real0, real1):
    """
    Vector Arithmetic Add
    
    Element-wise addition of two arrays.
    
    Parameters:
    - real0: array-like, first array
    - real1: array-like, second array
    
    Returns:
    numpy.ndarray: Sum of the two arrays
    """

def SUB(real0, real1):
    """
    Vector Arithmetic Subtraction
    
    Element-wise subtraction of two arrays.
    
    Parameters:
    - real0: array-like, first array (minuend)
    - real1: array-like, second array (subtrahend)
    
    Returns:
    numpy.ndarray: Difference (real0 - real1)
    """

def MULT(real0, real1):
    """
    Vector Arithmetic Multiply
    
    Element-wise multiplication of two arrays.
    
    Parameters:
    - real0: array-like, first array
    - real1: array-like, second array
    
    Returns:
    numpy.ndarray: Product of the two arrays
    """

def DIV(real0, real1):
    """
    Vector Arithmetic Divide
    
    Element-wise division of two arrays.
    
    Parameters:
    - real0: array-like, numerator array
    - real1: array-like, denominator array
    
    Returns:
    numpy.ndarray: Quotient (real0 / real1)
    """

Aggregation Functions

def SUM(real, timeperiod=30):
    """
    Summation
    
    Calculates rolling sum over specified period.
    
    Parameters:
    - real: array-like, input data
    - timeperiod: int, number of periods to sum (default: 30)
    
    Returns:
    numpy.ndarray: Rolling sum values
    """

def MAX(real, timeperiod=30):
    """
    Highest value over a specified period
    
    Parameters:
    - real: array-like, input data
    - timeperiod: int, number of periods to examine (default: 30)
    
    Returns:
    numpy.ndarray: Maximum values over rolling window
    """

def MIN(real, timeperiod=30):
    """
    Lowest value over a specified period
    
    Parameters:
    - real: array-like, input data
    - timeperiod: int, number of periods to examine (default: 30)
    
    Returns:
    numpy.ndarray: Minimum values over rolling window
    """

def MINMAX(real, timeperiod=30):
    """
    Lowest and highest values over a specified period
    
    Parameters:
    - real: array-like, input data
    - timeperiod: int, number of periods to examine (default: 30)
    
    Returns:
    tuple: (min_values, max_values) over rolling window
    """

Index Functions

def MAXINDEX(real, timeperiod=30):
    """
    Index of highest value over a specified period
    
    Parameters:
    - real: array-like, input data
    - timeperiod: int, number of periods to examine (default: 30)
    
    Returns:
    numpy.ndarray[int32]: Index positions of maximum values
    """

def MININDEX(real, timeperiod=30):
    """
    Index of lowest value over a specified period
    
    Parameters:
    - real: array-like, input data
    - timeperiod: int, number of periods to examine (default: 30)
    
    Returns:
    numpy.ndarray[int32]: Index positions of minimum values
    """

def MINMAXINDEX(real, timeperiod=30):
    """
    Indexes of lowest and highest values over a specified period
    
    Parameters:
    - real: array-like, input data
    - timeperiod: int, number of periods to examine (default: 30)
    
    Returns:
    tuple: (min_indexes, max_indexes) as int32 arrays
    """

Trigonometric Functions

def SIN(real):
    """Vector Trigonometric Sin"""

def COS(real):
    """Vector Trigonometric Cos"""

def TAN(real):
    """Vector Trigonometric Tan"""

def ASIN(real):
    """Vector Trigonometric ASin"""

def ACOS(real):
    """Vector Trigonometric ACos"""

def ATAN(real):
    """Vector Trigonometric ATan"""

def SINH(real):
    """Vector Trigonometric Sinh"""

def COSH(real):
    """Vector Trigonometric Cosh"""

def TANH(real):
    """Vector Trigonometric Tanh"""

Exponential and Logarithmic Functions

def EXP(real):
    """
    Vector Arithmetic Exp
    
    Calculates e^x for each element.
    
    Parameters:
    - real: array-like, input data
    
    Returns:
    numpy.ndarray: Exponential values
    """

def LN(real):
    """
    Vector Log Natural
    
    Calculates natural logarithm for each element.
    
    Parameters:
    - real: array-like, input data (must be positive)
    
    Returns:
    numpy.ndarray: Natural logarithm values
    """

def LOG10(real):
    """
    Vector Log10
    
    Calculates base-10 logarithm for each element.
    
    Parameters:
    - real: array-like, input data (must be positive)
    
    Returns:
    numpy.ndarray: Base-10 logarithm values
    """

def SQRT(real):
    """
    Vector Square Root
    
    Calculates square root for each element.
    
    Parameters:
    - real: array-like, input data (must be non-negative)
    
    Returns:
    numpy.ndarray: Square root values
    """

Rounding Functions

def CEIL(real):
    """
    Vector Ceil
    
    Rounds each element up to nearest integer.
    
    Parameters:
    - real: array-like, input data
    
    Returns:
    numpy.ndarray: Ceiling values
    """

def FLOOR(real):
    """
    Vector Floor
    
    Rounds each element down to nearest integer.
    
    Parameters:
    - real: array-like, input data
    
    Returns:
    numpy.ndarray: Floor values
    """

Usage Examples

import talib
import numpy as np

# Sample price data
prices1 = np.array([100, 102, 101, 103, 105, 104, 106, 108, 107, 109])
prices2 = np.array([50, 51, 50.5, 52, 53, 52.5, 54, 55, 54.5, 56])
high = np.array([101, 103, 102, 104, 106, 105, 107, 109, 108, 110])
low = np.array([99, 101, 100, 102, 104, 103, 105, 107, 106, 108])

# Basic arithmetic operations
price_sum = talib.ADD(prices1, prices2)
price_diff = talib.SUB(prices1, prices2)
price_ratio = talib.DIV(prices1, prices2)

# Aggregation functions
rolling_sum = talib.SUM(prices1, timeperiod=5)
highest = talib.MAX(prices1, timeperiod=5)
lowest = talib.MIN(prices1, timeperiod=5)
min_vals, max_vals = talib.MINMAX(prices1, timeperiod=5)

# Mathematical transformations
log_prices = talib.LN(prices1)
sqrt_prices = talib.SQRT(prices1)
sin_prices = talib.SIN(prices1 / 100)  # Normalize for reasonable sine values

print("Mathematical Operations Results:")
print(f"Latest Price Sum: {price_sum[-1]:.2f}")
print(f"Latest Price Difference: {price_diff[-1]:.2f}")
print(f"Latest Price Ratio: {price_ratio[-1]:.3f}")
print(f"5-Period Rolling Sum: {rolling_sum[-1]:.2f}")
print(f"5-Period High: {highest[-1]:.2f}")
print(f"5-Period Low: {lowest[-1]:.2f}")

# Custom indicator example: Price momentum using math functions
momentum = talib.SUB(prices1, talib.MAX(prices1, timeperiod=3))
momentum_normalized = talib.DIV(momentum, talib.MAX(momentum, timeperiod=5))
print(f"Custom Momentum Indicator: {momentum_normalized[-1]:.3f}")

Custom Indicator Development

These math functions enable creation of custom indicators:

# Example: Custom price spread calculation
def custom_price_spread(high_prices, low_prices, period=10):
    # Calculate price range
    price_range = talib.SUB(high_prices, low_prices)
    
    # Calculate average range over period
    avg_range = talib.SUM(price_range, timeperiod=period)
    normalized_range = talib.DIV(avg_range, period)
    
    return normalized_range

# Example: Composite price index
def composite_price_index(high, low, close, open_prices):
    # Weighted average of OHLC
    hlc_avg = talib.DIV(talib.ADD(talib.ADD(high, low), close), 3)
    ohlc_avg = talib.DIV(talib.ADD(hlc_avg, open_prices), 2)
    
    return ohlc_avg

Install with Tessl CLI

npx tessl i tessl/pypi-ta-lib

docs

abstract-streaming.md

cycle-indicators.md

index.md

math-operations.md

momentum-indicators.md

overlap-studies.md

pattern-recognition.md

price-transform.md

statistical-functions.md

volatility-indicators.md

volume-indicators.md

tile.json