Math Functions
TechKit provides 26 mathematical functions for price transformation, arithmetic operations, aggregation, and trigonometric calculations.
Price Transforms
AVGPRICE - Average Price
Calculates the average of open, high, low, and close prices.
Input: OHLC
Lookback: 0
MEDPRICE - Median Price
Calculates the median of high and low prices.
Input: OHLC
Lookback: 0
TYPPRICE - Typical Price
Calculates the typical price (average of high, low, and close).
Input: OHLC
Lookback: 0
WCLPRICE - Weighted Close Price
Calculates the weighted close price, giving more weight to the close.
Input: OHLC
Lookback: 0
Arithmetic Operators
ADD - Addition
Adds two input arrays element-wise.
Input: Two close price arrays
Lookback: 0
SUB - Subtraction
Subtracts second input from first input element-wise.
Input: Two close price arrays
Lookback: 0
MULT - Multiplication
Multiplies two input arrays element-wise.
Input: Two close price arrays
Lookback: 0
DIV - Division
Divides first input by second input element-wise.
Input: Two close price arrays
Lookback: 0
Note: Returns 0 if denominator is 0
Aggregation Functions
MAX - Maximum Value
Returns the maximum value over a specified period.
Parameters:
period(int, default: 30): Number of periods
Input: Close price
Lookback: period - 1
MIN - Minimum Value
Returns the minimum value over a specified period.
Parameters:
period(int, default: 30): Number of periods
Input: Close price
Lookback: period - 1
SUM - Sum
Returns the sum of values over a specified period.
Parameters:
period(int, default: 30): Number of periods
Input: Close price
Lookback: period - 1
MAXINDEX - Index of Maximum
Returns the index (bars ago) of the maximum value over a period.
Parameters:
period(int, default: 30): Number of periods
Input: Close price
Lookback: period - 1
Output: Index (0 = current bar, 1 = 1 bar ago, etc.)
MININDEX - Index of Minimum
Returns the index (bars ago) of the minimum value over a period.
Parameters:
period(int, default: 30): Number of periods
Input: Close price
Lookback: period - 1
Output: Index (0 = current bar, 1 = 1 bar ago, etc.)
MINMAX - Minimum and Maximum
Returns both minimum and maximum values over a period.
Parameters:
period(int, default: 30): Number of periods
Input: Close price
Output: Two values (min, max)
Lookback: period - 1
API Usage:
from techkit import MINMAX
minmax = MINMAX(period=30)
result = minmax.update(price)
if result.valid:
print(f"Min: {result.min}, Max: {result.max}")
MINMAXINDEX - Minimum and Maximum Indices
Returns the indices of both minimum and maximum values over a period.
Parameters:
period(int, default: 30): Number of periods
Input: Close price
Output: Two values (min_index, max_index)
Lookback: period - 1
Trigonometric Functions
SIN - Sine
Calculates the sine of input values.
Input: Close price (treated as radians)
Lookback: 0
COS - Cosine
Calculates the cosine of input values.
Input: Close price (treated as radians)
Lookback: 0
TAN - Tangent
Calculates the tangent of input values.
Input: Close price (treated as radians)
Lookback: 0
ASIN - Arc Sine
Calculates the arc sine (inverse sine) of input values.
Input: Close price (must be in range [-1, 1])
Output: Radians
Lookback: 0
ACOS - Arc Cosine
Calculates the arc cosine (inverse cosine) of input values.
Input: Close price (must be in range [-1, 1])
Output: Radians
Lookback: 0
ATAN - Arc Tangent
Calculates the arc tangent (inverse tangent) of input values.
Input: Close price
Output: Radians
Lookback: 0
Hyperbolic Functions
SINH - Hyperbolic Sine
Calculates the hyperbolic sine of input values.
Input: Close price
Lookback: 0
COSH - Hyperbolic Cosine
Calculates the hyperbolic cosine of input values.
Input: Close price
Lookback: 0
TANH - Hyperbolic Tangent
Calculates the hyperbolic tangent of input values.
Input: Close price
Output: Range [-1, 1]
Lookback: 0
Other Math Functions
CEIL - Ceiling
Rounds up to the nearest integer.
Input: Close price
Lookback: 0
FLOOR - Floor
Rounds down to the nearest integer.
Input: Close price
Lookback: 0
EXP - Exponential
Calculates e raised to the power of input.
Input: Close price
Lookback: 0
LN - Natural Logarithm
Calculates the natural logarithm (base e).
Input: Close price (must be > 0)
Lookback: 0
LOG10 - Base-10 Logarithm
Calculates the base-10 logarithm.
Input: Close price (must be > 0)
Lookback: 0
SQRT - Square Root
Calculates the square root of input values.
Input: Close price (must be >= 0)
Lookback: 0
Usage Examples
Price Transforms
from techkit import AVGPRICE, TYPPRICE
# Calculate average price
avg = AVGPRICE()
result = avg.update_ohlcv(open, high, low, close)
# Calculate typical price
typ = TYPPRICE()
result = typ.update_ohlcv(open, high, low, close)
Arithmetic Operations
from techkit import ADD, SUB, MULT, DIV
# Add two price series
add = ADD()
result = add.update(value1, value2)
# Subtract
sub = SUB()
result = sub.update(value1, value2)
Aggregation
from techkit import MAX, MIN, SUM
# Maximum over 20 periods
max_ind = MAX(period=20)
result = max_ind.update(price)
# Minimum
min_ind = MIN(period=20)
result = min_ind.update(price)
# Sum
sum_ind = SUM(period=20)
result = sum_ind.update(price)
Trigonometric
from techkit import SIN, COS, ATAN
# Sine (input in radians)
sin = SIN()
result = sin.update(angle_in_radians)
# Arc tangent (output in radians)
atan = ATAN()
result = atan.update(price)
angle_degrees = result.value * 180 / 3.14159
Common Use Cases
Price Normalization: Use AVGPRICE or TYPPRICE for normalized price analysis
Spread Analysis: Use SUB to calculate spreads between two instruments
Ratio Analysis: Use DIV to calculate price ratios
Range Analysis: Use MAX and MIN to identify support/resistance levels
Mathematical Transformations: Use trigonometric functions for cycle analysis