π TinyCTA
A Lightweight Python Package for Commodity Trading Advisor Strategies.
Quick Links: π Repository β’ π¦ PyPI β’ π Issues β’ π¬ Discussions
TinyCTA provides essential tools for quantitative finance and algorithmic trading, particularly for trend-following strategies. The package includes:
- Signal processing functions for creating oscillators and adjusting returns
- Linear algebra utilities that handle matrices with missing values
- Matrix shrinkage techniques commonly used in portfolio optimization
This package is designed to be the foundation for implementing CTA strategies in just a few lines of code, hence the name "TinyCTA".
pip install tinyctaClone the repository and install using the provided Makefile:
git clone https://github.com/tschm/tinycta.git
cd tinycta
make installThis will install uv (a fast Python package installer) and create a virtual environment with all dependencies.
from pathlib import Path
import pandas as pd
from tinycta.signal import osc
path = Path(__name__).resolve().parent.parent
# Load price data
prices = pd.read_csv("data.csv", index_col=0, parse_dates=True)
# Create an oscillator with default parameters
oscillator = prices.apply(osc)
# Create an oscillator with custom parameters
custom_oscillator = prices.apply(osc, fast=16, slow=64, scaling=False)from tinycta.signal import returns_adjust
# Adjust returns for volatility
adjusted_returns = prices.apply(returns_adjust)import numpy as np
from tinycta.linalg import solve
# Create a matrix and right-hand side vector
matrix = np.array([[1.0, 0.5], [0.5, 1.0]])
rhs = np.array([1.0, 2.0])
# Solve the linear system
solution = solve(matrix, rhs)
print(solution)[0. 2.]
osc(prices, fast=32, slow=96, scaling=True): Creates an oscillator based on the difference between fast and slow moving averagesreturns_adjust(price, com=32, min_periods=300, clip=4.2): Adjusts log-returns by volatility and applies winsorizationshrink2id(matrix, lamb=1.0): Performs shrinkage of a matrix towards the identity matrix
valid(matrix): Constructs a valid subset of a matrix by filtering out rows/columns with NaN valuesa_norm(vector, matrix=None): Computes the matrix-norm of a vector with respect to a matrixinv_a_norm(vector, matrix=None): Computes the inverse matrix-norm of a vectorsolve(matrix, rhs): Solves a linear system of equations, handling matrices with NaN values
make installmake testmake fmtmake cleanTinyCTA is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.