Skip to content

VernonOY/qtype

Repository files navigation

qtype

English | 简体中文

Static analyzer for quantitative trading code. Catches look-ahead bias, future functions, survival bias, alignment errors, and return offset bugs — before they ruin your backtest.

pip install qtype
qtype check my_strategy.py

Why qtype?

Backtests lie when your code has subtle time-leak bugs. The most expensive ones look completely innocent:

df["forward_return"] = df["close"].pct_change()   # ← inverted!

That one line will turn your sharpe-0.3 strategy into a sharpe-3.0 hallucination. qtype is a Python AST-based linter that catches these patterns before your strategy ever hits production.

Find the bug

import pandas as pd

def make_signal(df: pd.DataFrame) -> pd.Series:
    return df["close"].shift(-1) > df["close"]
Click to reveal

shift(-1) moves the data forward in time, so at row t you're seeing tomorrow's close. qtype flags this as QT001.

$ qtype check make_signal.py

  Found 1 issue in 1 file

  make_signal.py
    QT001  line 4  shift(-1) leaks future data at row t
                   → use shift(1) to access past data

Built-in rules (v0.1)

ID Name Severity What it catches
QT001 look-ahead-bias error .shift(N) with negative integer literal
QT002 future-function error calls to lead, look_forward, peek_future, forward_fill_future
QT003 survival-bias warning universe-builder functions missing ST / suspended / delisted filters
QT004 alignment-error warning .merge() calls without explicit on= / left_on= / left_index=
QT005 return-offset warning pct_change() assigned to a forward_* / next_* / target variable

Run qtype rules for a live list with severity colors.

Usage

qtype check ./my_strategy/                  # scan a directory
qtype check alpha.py --format json          # machine-readable output
qtype check alpha.py --rules QT001,QT003    # only these rules
qtype rules                                 # list all rules
qtype init .                                # scaffold .qtype.toml (v0.2 will load it)

Exit codes: 0 clean, 1 violations found.

As a Python library

from qtype import QtypeAnalyzer

analyzer = QtypeAnalyzer()
violations = analyzer.check_source(open("alpha.py").read(), filename="alpha.py")
for v in violations:
    print(f"{v.rule_id} {v.file}:{v.line}  {v.message}")

Installation

pip install qtype                           # with pip
uv add qtype                                # with uv (recommended)

Requires Python 3.11+.

Roadmap

Version What
v0.1 5 rules + CLI + SDK (you are here)
v0.2 # noqa: QTNNN inline suppression + .qtype.toml config + --fix autofix
v0.3 GitHub Action wrapper + ruff plugin compatibility
v0.4 LLM-assisted suggestions for missed rules

Part of alpha-kit

qtype is the first tool in alpha-kit — a polyrepo of focused, single-purpose quant research tools (regime-lens, backtest-debugger, paper2alpha, ...). Each tool ships independently.

License

MIT (c) VernonOY

About

Static analyzer for quantitative trading code — catches look-ahead bias, future functions, survival bias, and other fatal bugs

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages