trendfollowing documentation¶
Author: Artur Sepp / First recorded: 2026-08-16
Implemented in trendfollowing. Software citation: CITATION.cff.
trendfollowing is a Python library of closed-form trend-following analytics, reference system implementations and reproducible futures evidence for quantitative researchers and practitioners. It computes the expected return, the gross and net Sharpe ratio, the turnover and the skewness of trend-following systems from the autocorrelation and drift of the returns they trade, and runs the European, American and time-series-momentum systems on a packaged panel of 84 futures. It is the replication package of The Science and Practice of Trend-Following Systems (Sepp and Lucic, 2026). It is a research and replication library, not a broker integration or general-purpose execution engine; portfolio analytics and reporting are delegated to qis.
Start here¶
Install trendfollowing. The core installation command is
python -m pip install trendfollowing.Run the offline quickstart: two closed-form Sharpe ratios in a few lines, with their conventions stated.
Keep Notation and conventions at hand: the reserved symbols, the 260-day annualisation, the arithmetic Sharpe convention, timing and the two cost units are defined there once for every chapter.
Run the example workflows: span selection, Sharpe prediction from an autocorrelation function, and a backtest of the European system on the packaged futures.
The system in one picture¶
The European system holds, in each contract, the variance-preserving EWMA signal of volatility-normalised returns \(z_t=r_t/\sigma_{t-1}\), scaled to a volatility target. Its daily return and its expected annual return are
an autocorrelation channel and a squared-drift channel, and its Sharpe ratio is a closed-form function of the autocorrelation function \(\rho\), the drift \(\mu_{\mathrm{an}}\), the excess kurtosis of the innovations and the span alone. The handbook builds these results step by step and connects them to the code and the data:
Question |
Chapters |
|---|---|
How is the signal built and scaled? |
|
Which return processes are analysed? |
|
Where does the P&L come from? |
|
What is the Sharpe ratio, and how does the span change it? |
The closed-form Sharpe ratio, Sharpe ratios under named processes |
What do costs do, and which span is cost optimal? |
|
Why are trend-following returns positively skewed? |
|
How do the three system designs work in code? |
|
What data and costs do the backtests use? |
|
What does the futures evidence show, and how are Sharpe ratios compared? |
The trendfollowing handbook¶
The methodology chapters form one book. Each chapter defines its method with formulas and concise proofs, states its conventions in an eight-row convention card, works an example whose numbers the test suite checks, and links to the functions that implement it. Symbols keep one meaning throughout; see Notation and conventions and the bibliography.
Part I: Foundations¶
Notation and conventions: reserved symbols, returns of continuous futures, volatility-normalised returns, spans, annualisation, the Sharpe convention, timing and costs.
EWMA filters and variance-preserving signals: span and smoothing parameter, the single and long-short filters with unit variance, impulse responses, mean gains and initialisation.
Volatility normalisation and position sizing: the lagged EWMA volatility, the volatility-target weight, the system-return identity, heavy tails, dampened autocorrelation and the Kelly reading.
Return-generating processes: white noise, AR(1), MA(1) and ARFIMA, their autocorrelation functions and moving-average weights, the hypergeometric generating function and the Monte Carlo engine.
Part II: The European system in closed form¶
The P&L decomposition: autocorrelation and drift channels: the exact sample-path identity, the expected return under any stationary autocorrelation function, and its Poisson-kernel spectral reading.
The closed-form Sharpe ratio: the variance of a product with excess kurtosis, the loadings \(A_\nu\), \(B_\nu\) and \(K_\nu\), and the long-short extension.
Sharpe ratios under white noise, AR(1) and ARFIMA: exact and leading-order rules, the span trade-off and the paper’s verification table.
Turnover, trading costs and the net Sharpe ratio: the signal-turnover proxy, the net Sharpe ratio, the AR(1) break-even cost and cost-optimal spans.
Skewness of aggregated returns: the closed-form skewness under white noise, its master curve and peak near half the span, and its empirical profile.
Part III: Reference systems¶
The European system: continuous weights, caps, portfolio volatility targeting, warmup and the P&L, turnover and cost accounting of
BacktestOutputs.The American system: price crossovers with a range buffer, sizing at inception and trailing stops.
Time-series momentum: normalised sums of signs over \(M\) periods of \(L\) days, and the sign-filter discount.
The futures universe and cost schedule: 84 contracts from 1959 to 2026, benchmarks and volume costs.
Part IV: Evidence and inference¶
Realised Sharpe ratios and their comparison: the realised estimator, its sampling error, the Ledoit–Wolf test and regime-conditional Sharpe ratios.
The futures evidence of Sepp and Lucic (2026): grid backtests, the SG Trend comparison, the attribution of realised Sharpe ratios and the skewness of the cross-section.
Guides¶
Task-oriented pages that run the methods end to end:
Closed-form analytics and span selection: process inputs, units, outputs and the span-selection example.
Predict Sharpe from autocorrelation and drift: the in-sample attribution workflow on packaged futures and its point-in-time boundary.
Compare and backtest the three systems: choosing a design, the common contract, costs, warmups, timing and the paper’s backtests.
Choosing a trend-following or backtesting tool: a dated, source-backed comparison with pysystemtrade, vectorbt and Backtesting.py.
Reference¶
API reference: every public object, grouped by the chapter that explains it.
Bibliography: every work cited by the handbook, in one style.
Paper and replication: the paper, its exhibits and the generators that reproduce them.
Documentation standard: page forms, convention card, notation, executed examples and figure provenance.
Research paper¶
Sepp, A., and Lucic, V. (2026). The Science and Practice of Trend-Following Systems. Working paper. arXiv:2607.19497; SSRN 3167787. Cite the paper for its methods and the software record for the version a replication used, together with qis where its analytics are used.
Project resources¶
Analytics and reporting dependency: qis.