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.

\[AVGPRICE = \frac{open + high + low + close}{4}\]

Input: OHLC
Lookback: 0


MEDPRICE - Median Price

Calculates the median of high and low prices.

\[MEDPRICE = \frac{high + low}{2}\]

Input: OHLC
Lookback: 0


TYPPRICE - Typical Price

Calculates the typical price (average of high, low, and close).

\[TYPPRICE = \frac{high + low + close}{3}\]

Input: OHLC
Lookback: 0


WCLPRICE - Weighted Close Price

Calculates the weighted close price, giving more weight to the close.

\[WCLPRICE = \frac{high + low + 2 \times close}{4}\]

Input: OHLC
Lookback: 0


Arithmetic Operators

ADD - Addition

Adds two input arrays element-wise.

\[ADD = value_1 + value_2\]

Input: Two close price arrays
Lookback: 0


SUB - Subtraction

Subtracts second input from first input element-wise.

\[SUB = value_1 - value_2\]

Input: Two close price arrays
Lookback: 0


MULT - Multiplication

Multiplies two input arrays element-wise.

\[MULT = value_1 \times value_2\]

Input: Two close price arrays
Lookback: 0


DIV - Division

Divides first input by second input element-wise.

\[DIV = \frac{value_1}{value_2}\]

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.

\[MAX = \max(value[0..period-1])\]

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.

\[MIN = \min(value[0..period-1])\]

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.

\[SUM = \sum_{i=0}^{period-1} value[i]\]

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.

\[SIN = \sin(value)\]

Input: Close price (treated as radians)
Lookback: 0


COS - Cosine

Calculates the cosine of input values.

\[COS = \cos(value)\]

Input: Close price (treated as radians)
Lookback: 0


TAN - Tangent

Calculates the tangent of input values.

\[TAN = \tan(value)\]

Input: Close price (treated as radians)
Lookback: 0


ASIN - Arc Sine

Calculates the arc sine (inverse sine) of input values.

\[ASIN = \arcsin(value)\]

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.

\[ACOS = \arccos(value)\]

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.

\[ATAN = \arctan(value)\]

Input: Close price
Output: Radians
Lookback: 0


Hyperbolic Functions

SINH - Hyperbolic Sine

Calculates the hyperbolic sine of input values.

\[SINH = \sinh(value) = \frac{e^{value} - e^{-value}}{2}\]

Input: Close price
Lookback: 0


COSH - Hyperbolic Cosine

Calculates the hyperbolic cosine of input values.

\[COSH = \cosh(value) = \frac{e^{value} + e^{-value}}{2}\]

Input: Close price
Lookback: 0


TANH - Hyperbolic Tangent

Calculates the hyperbolic tangent of input values.

\[TANH = \tanh(value) = \frac{\sinh(value)}{\cosh(value)}\]

Input: Close price
Output: Range [-1, 1]
Lookback: 0


Other Math Functions

CEIL - Ceiling

Rounds up to the nearest integer.

\[CEIL = \lceil value \rceil\]

Input: Close price
Lookback: 0


FLOOR - Floor

Rounds down to the nearest integer.

\[FLOOR = \lfloor value \rfloor\]

Input: Close price
Lookback: 0


EXP - Exponential

Calculates e raised to the power of input.

\[EXP = e^{value}\]

Input: Close price
Lookback: 0


LN - Natural Logarithm

Calculates the natural logarithm (base e).

\[LN = \ln(value)\]

Input: Close price (must be > 0)
Lookback: 0


LOG10 - Base-10 Logarithm

Calculates the base-10 logarithm.

\[LOG10 = \log_{10}(value)\]

Input: Close price (must be > 0)
Lookback: 0


SQRT - Square Root

Calculates the square root of input values.

\[SQRT = \sqrt{value}\]

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

  1. Price Normalization: Use AVGPRICE or TYPPRICE for normalized price analysis

  2. Spread Analysis: Use SUB to calculate spreads between two instruments

  3. Ratio Analysis: Use DIV to calculate price ratios

  4. Range Analysis: Use MAX and MIN to identify support/resistance levels

  5. Mathematical Transformations: Use trigonometric functions for cycle analysis