Python API Reference
TechKit Python bindings provide high-performance technical analysis with NumPy integration.
Installation
pip install techkit
Requirements:
Python 3.10+
NumPy >= 1.21.0 (installed automatically)
Quick Reference
Object-Oriented API
from techkit import SMA, EMA, RSI, MACD, BBANDS
# Create indicator
sma = SMA(period=20)
# Incremental update
result = sma.update(price)
if result.valid:
print(result.value)
# Batch calculation
import numpy as np
prices = np.array([...])
values = sma.calculate(prices)
TA-Lib Compatible API
from techkit import talib_compat as ta
# Same signatures as TA-Lib
sma = ta.SMA(prices, timeperiod=20)
rsi = ta.RSI(prices, timeperiod=14)
macd, signal, hist = ta.MACD(prices)
upper, middle, lower = ta.BBANDS(prices)
Module: techkit
Result Classes
IndicatorResult
@dataclass
class IndicatorResult:
value: float # Calculated value
valid: bool # True if valid, False during warmup
def __bool__(self) -> bool:
return self.valid
Usage:
result = sma.update(100.0)
if result: # Uses __bool__
print(f"SMA: {result.value}")
MACDResult
@dataclass
class MACDResult:
macd: float # MACD line
signal: float # Signal line
histogram: float # Histogram
valid: bool
def __iter__(self):
return iter([self.macd, self.signal, self.histogram])
Usage:
result = macd.update(price)
if result.valid:
m, s, h = result # Tuple unpacking via __iter__
BBandsResult
@dataclass
class BBandsResult:
upper: float
middle: float
lower: float
valid: bool
StochResult
@dataclass
class StochResult:
k: float
d: float
valid: bool
Base Indicator Class
class Indicator:
def update(self, value: float) -> IndicatorResult:
"""Update with single value (close price)."""
def update_ohlcv(self, open: float, high: float, low: float,
close: float, volume: float) -> IndicatorResult:
"""Update with OHLCV bar."""
def calculate(self, data: np.ndarray) -> np.ndarray:
"""Batch calculate. Returns NaN for warmup period."""
def reset(self) -> None:
"""Reset indicator state."""
@property
def lookback(self) -> int:
"""Number of bars before first valid output."""
@property
def name(self) -> str:
"""Indicator name."""
@property
def is_ready(self) -> bool:
"""True if indicator has produced valid output."""
Indicator Classes
Moving Averages
Class |
Constructor |
Input |
Description |
|---|---|---|---|
|
|
close |
Simple Moving Average |
|
|
close |
Exponential Moving Average |
|
|
close |
Weighted Moving Average |
|
|
close |
Double EMA |
|
|
close |
Triple EMA |
|
|
close |
Kaufman Adaptive MA |
|
|
close |
Triangular MA |
|
|
close |
T3 Moving Average |
Momentum Indicators
Class |
Constructor |
Input |
Description |
|---|---|---|---|
|
|
close |
Relative Strength Index |
|
|
close |
MACD (3 outputs) |
|
|
OHLCV |
Stochastic (2 outputs) |
|
|
OHLCV |
Average Directional Index |
|
|
OHLCV |
Commodity Channel Index |
|
|
OHLCV |
Williams %R |
|
|
OHLCV |
Money Flow Index |
|
|
close |
Momentum |
|
|
close |
Rate of Change |
|
|
close |
Absolute Price Oscillator |
|
|
close |
Percentage Price Oscillator |
|
|
close |
Chande Momentum Oscillator |
|
|
close |
Triple Exponential Average |
|
|
OHLCV |
Aroon Indicator (2 outputs) |
|
|
OHLCV |
Ultimate Oscillator |
Volatility Indicators
Class |
Constructor |
Input |
Description |
|---|---|---|---|
|
|
OHLCV |
Average True Range |
|
|
OHLCV |
Normalized ATR |
|
|
OHLCV |
True Range |
|
|
close |
Bollinger Bands (3 outputs) |
Volume Indicators
Class |
Constructor |
Input |
Description |
|---|---|---|---|
|
|
OHLCV |
On Balance Volume |
|
|
OHLCV |
Accumulation/Distribution |
|
|
OHLCV |
A/D Oscillator |
Statistics Functions
Class |
Constructor |
Input |
Description |
|---|---|---|---|
|
|
close |
Standard Deviation |
|
|
close |
Variance |
|
|
close |
Linear Regression |
|
|
close |
Regression Slope |
|
|
close |
Regression Intercept |
|
|
close |
Regression Angle |
|
|
close |
Time Series Forecast |
|
|
close + close |
Beta Coefficient |
|
|
close + close |
Pearson Correlation |
|
|
close |
Minimum Value |
|
|
close |
Maximum Value |
|
|
close |
Sum over Period |
|
|
close |
Min and Max (2 outputs) |
Risk Metrics
Class |
Constructor |
Input |
Description |
|---|---|---|---|
|
|
returns |
Rolling Sharpe Ratio |
|
|
returns |
Sortino Ratio |
|
|
price |
Maximum Drawdown |
|
|
price |
Drawdown Series |
|
|
returns |
Calmar Ratio |
|
|
returns |
Historical VaR |
|
|
returns |
Conditional VaR |
Volatility Models
Class |
Constructor |
Input |
Description |
|---|---|---|---|
|
|
returns |
EWMA Volatility |
|
|
returns |
Realized Volatility |
|
|
OHLCV |
Parkinson Volatility |
|
|
returns |
GARCH(1,1) Volatility |
Pattern Recognition
Class |
Constructor |
Input |
Description |
|---|---|---|---|
|
|
OHLCV |
Harmonic Patterns |
|
|
OHLCV |
Chart Patterns |
|
|
OHLCV |
ZigZag Indicator |
|
|
OHLCV |
Swing Detection |
|
|
OHLCV |
Pivot Points |
Candlestick Patterns (61 patterns)
All CDL classes follow the same interface:
from techkit import CDL_DOJI, CDL_HAMMER, CDL_ENGULFING
doji = CDL_DOJI()
result = doji.calculate_ohlcv(open, high, low, close, volume)
# result: array of signals (+100 bullish, -100 bearish, 0 none)
Chain Class
class Chain:
"""Chain multiple indicators together.
Output of first indicator feeds into second, etc.
Useful for smoothing or combining indicators.
Example:
>>> # Smoothed RSI: RSI(14) -> EMA(9)
>>> chain = Chain([RSI(14), EMA(9)])
>>> result = chain.update(price)
>>> # Batch
>>> smoothed = chain.calculate(prices)
"""
def __init__(self, indicators: List[Indicator]): ...
def update(self, value: float) -> IndicatorResult: ...
def calculate(self, data: np.ndarray) -> np.ndarray: ...
def reset(self) -> None: ...
Module: techkit.talib_compat
Drop-in replacement for TA-Lib. Same function signatures.
Migration from TA-Lib
# Before (TA-Lib)
import talib
sma = talib.SMA(prices, timeperiod=20)
macd, signal, hist = talib.MACD(prices)
# After (TechKit)
from techkit import talib_compat as ta
sma = ta.SMA(prices, timeperiod=20) # Same!
macd, signal, hist = ta.MACD(prices) # Same!
Function Reference
Overlap Studies
Function |
Signature |
Returns |
|---|---|---|
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
Tuple[3] |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
Momentum Indicators
Function |
Signature |
Returns |
|---|---|---|
|
|
np.ndarray |
|
|
Tuple[3] |
|
|
Tuple[2] |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
Tuple[2] |
|
|
np.ndarray |
|
|
np.ndarray |
Volatility
Function |
Signature |
Returns |
|---|---|---|
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
Volume
Function |
Signature |
Returns |
|---|---|---|
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
Statistics
Function |
Signature |
Returns |
|---|---|---|
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
Price Transform
Function |
Signature |
Returns |
|---|---|---|
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
Math Operators
Function |
Signature |
Returns |
|---|---|---|
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
Tuple[2] |
|
|
np.ndarray |
Hilbert Transform
Function |
Signature |
Returns |
|---|---|---|
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
np.ndarray |
|
|
Tuple[2] |
|
|
Tuple[2] |
Pattern Recognition
All 61 candlestick pattern functions are available:
CDL_DOJI,CDL_HAMMER,CDL_ENGULFING, etc.Signature:
CDL_*(open, high, low, close)Returns:
np.ndarraywith +100 (bullish), -100 (bearish), or 0 (none)
Input Types
All functions accept:
numpy.ndarraypandas.Serieslist
import numpy as np
import pandas as pd
from techkit import talib_compat as ta
# All work
ta.SMA(np.array([1, 2, 3]), 2)
ta.SMA(pd.Series([1, 2, 3]), 2)
ta.SMA([1, 2, 3], 2)
Type Hints
TechKit includes type stubs for full IDE support.
# Type checking works
from techkit import SMA, RSI
import numpy as np
sma: SMA = SMA(20)
result = sma.update(100.0) # result: IndicatorResult
values: np.ndarray = sma.calculate(np.array([...]))
Working with Pandas
import pandas as pd
import numpy as np
from techkit import SMA, RSI, MACD, BBANDS
# Load data
df = pd.read_csv('ohlcv.csv')
# Add indicators
df['SMA_20'] = SMA(20).calculate(df['close'].values)
df['SMA_50'] = SMA(50).calculate(df['close'].values)
df['RSI'] = RSI(14).calculate(df['close'].values)
# Multi-output
macd, signal, hist = MACD().calculate(df['close'].values)
df['MACD'] = macd
df['MACD_Signal'] = signal
df['MACD_Hist'] = hist
upper, middle, lower = BBANDS().calculate(df['close'].values)
df['BB_Upper'] = upper
df['BB_Middle'] = middle
df['BB_Lower'] = lower
# Generate signals
df['Signal'] = np.where(
(df['RSI'] < 30) & (df['close'] < df['BB_Lower']),
'BUY',
np.where(
(df['RSI'] > 70) & (df['close'] > df['BB_Upper']),
'SELL',
'HOLD'
)
)
Performance Tips
Batch over incremental for backtesting:
# Fast
values = SMA(20).calculate(large_array)
# Slower
sma = SMA(20)
values = [sma.update(p).value for p in large_array]
Reuse indicator instances:
sma = SMA(20)
for symbol in symbols:
sma.reset()
values = sma.calculate(data[symbol])
Use NumPy arrays not lists:
# Fast
prices = np.array(price_list)
# Slower
prices = price_list # Will be converted internally
Version Information
import techkit
print(techkit.__version__) # "1.2.1"