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

  1. Install trendfollowing. The core installation command is python -m pip install trendfollowing.

  2. Run the offline quickstart: two closed-form Sharpe ratios in a few lines, with their conventions stated.

  3. 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.

  4. 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

\[ f_t=\frac{\sigma_{\mathrm{target}}}{\sqrt{\mathrm{af}}}S_{t-1}z_t, \qquad \bar F_{1y}=h_{1y}\sum_{m=1}^{\infty}\nu^m\rho(m)+\frac{l\sigma_{\mathrm{target}}}{\sqrt{\mathrm{af}}}\mu_{\mathrm{an}}^2 , \]

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?

EWMA filters, volatility normalisation

Which return processes are analysed?

Return-generating processes

Where does the P&L come from?

The P&L decomposition

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?

Turnover, costs and the net Sharpe ratio

Why are trend-following returns positively skewed?

Skewness of aggregated returns

How do the three system designs work in code?

European, American, TSMOM

What data and costs do the backtests use?

The futures universe and cost schedule

What does the futures evidence show, and how are Sharpe ratios compared?

The futures evidence, realised Sharpe ratios

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

Part III: Reference systems

Part IV: Evidence and inference

Guides

Task-oriented pages that run the methods end to end:

Reference

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