Skip to main content

market-structure

CI Deploy to GitHub Pages PyPI Python versions License Ruff

Python library for market structure analysis — swings, trends, support/resistance zones, break/retest/flip signals. Works with any OHLCV DataFrame. Includes a Freqtrade integration.

Live Demo — interactive chart viewer showing market structure overlays on real price data.

Market Structure Viewer — SOL/USDT 4h showing support/resistance zones, structure break, bearish divergence, and wave metrics

Quick start

import pandas as pd
from market_structure.hydrate import hydrate
from market_structure.tsi import compute_tsi
from market_structure.atr import _compute_atr

# df: any OHLCV DataFrame with open, high, low, close, volume columns
df["tsi_hist"] = compute_tsi(df["close"])["tsi_histogram"]
helper = hydrate(df, histogram_key="tsi_hist")

atr = _compute_atr(df["high"].to_numpy(), df["low"].to_numpy(),
                    df["close"].to_numpy(), period=14)

for z in helper.get_support_zones(atr_arr=atr):
    body_low, body_high = z.range       # body-anchored (accepted price)
    wick_low, wick_high = z.wick_range   # wick extrema (for stop placement)
    print(f"{z.anchor_wave_id}  body=({body_low:.2f}, {body_high:.2f})  "
          f"wick=({wick_low:.2f}, {wick_high:.2f})  double={z.is_double}")

Freqtrade integration

For Freqtrade strategies, a single call projects all 67 ms_* columns onto the DataFrame:

from market_structure.freqtrade import attach_market_structure

df = attach_market_structure(df, histogram_key="tsi_hist")
# df now has ms_support_zone_low, ms_wave_side, ms_is_trending_up, etc.

Backtest: market structure as a strategy filter

To validate the library, we ran an A/B comparison using a TSI signal-line crossover strategy on 4h Binance futures (BTC, ETH, SOL, XRP, LTC) over 360 days in a -14.91% market. The only difference: the "With MS" variant filters entries and exits through the ms_* columns; the "Without" variant uses the same TSI signal and risk parameters but skips market structure entirely.

Metric With MS filter Without MS filter
Total profit +39.85% +22.59%
Profit factor 1.50 1.21
Sharpe 0.94 0.58
Max drawdown 17.76% 21.75%
Trades 164 208
Win rate 36.0% 31.7%
Stop-loss hits 36 46

The filter blocked 44 low-quality entries (mostly would-be stop-outs), nearly doubling profit while cutting drawdown by ~4 percentage points. Backtest data and results for all three strategies live in refs/freqtrade/.

Disclaimer: This is educational software, not financial advice. Past backtest performance does not guarantee future results. Parameters are curve-fit to a specific historical window. Do not trade real capital based on these examples without independent validation and risk assessment.

Documentation

  • Column Reference — all 67 ms_* columns projected onto the DataFrame, with dtypes, tier descriptions, and strategy examples.

Install

pip install market-structure

The PyPI distribution is market-structure; the import name is market_structure. The GitHub repo is named pymarket-structure.

Development

This project uses uv for everything: Python version management, virtual environment, dependencies, lockfile, and dev tooling. Install uv first (one line — see the link above), then:

git clone git@github.com:fortunato/pymarket-structure.git
cd pymarket-structure
uv sync                            # creates .venv, installs all deps from uv.lock
uv run pre-commit install          # one-time: enable git pre-commit hooks
uv run just                        # list all available development commands
uv run just check                  # run lint + format-check + type + test

That's the entire bootstrap. Everything else is described by the project files themselves:

  • pyproject.toml — package metadata, dependencies, ruff/pyright/pytest configuration
  • Justfile — development commands (run uv run just for the full menu)
  • .pre-commit-config.yaml — git hook configuration
  • .editorconfig — editor formatting rules
  • .python-version — pinned Python version

Tech stack

Tool Purpose
uv Package manager, virtual environment, Python version manager
ruff Linter + formatter (replaces black, isort, flake8, pylint)
pyright Strict static type checker
pytest + pytest-cov Test runner with branch coverage
pre-commit Git hook framework
just Task runner

Common tasks

Once uv sync has run, the day-to-day workflow is driven by just recipes. A few highlights:

uv run just check              # full CI pipeline (lint + format + type + test)
uv run just test               # run all tests
uv run just test -k swing      # run only tests matching a pattern
uv run just lint               # ruff lint with auto-fix
uv run just fmt                # ruff format
uv run just type               # pyright (strict mode)
uv run just hooks              # run all pre-commit hooks against every file

If your shell has the project's venv activated (source .venv/bin/activate), you can drop the uv run prefix and just type just check, pytest, ruff check, etc. directly. Most editors (PyCharm, VS Code) auto-activate the venv once the interpreter is configured.

Support

If this library saves you time or helps your trading, consider buying me a coffee.

Buy Me A Coffee

License

See LICENSE.

Metadata

Release files for market-structure 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for market-structure 0.1.0
File Size Uploaded Provenance
market_structure-0.1.0.tar.gz 39.6 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for market-structure 0.1.0
File Interpreter ABI Platform
market_structure-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 81.2 kB

Release files / market_structure-0.1.0.tar.gz

Download URL market_structure-0.1.0.tar.gz
Size 39.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2cfd536b100fd5826b9b3cc0f5f24f25ff01c95cee59f5cd593e317f0379e86a
BLAKE2b-256 checksum
How to use checksums
ea20b6cb78c20b2578009b7e6126760b757d235646134b87aab1d64dc615cc7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 18, 2026.

Transparency log

Release files / market_structure-0.1.0-py3-none-any.whl

Download URL market_structure-0.1.0-py3-none-any.whl
Size 41.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a5d6372f2c5574bf3210cbf34f720c6a0eabb553ad23f0fa1e9ec38acaef5d41
BLAKE2b-256 checksum
How to use checksums
ce780701077946a8794887a1806d541558af78b5eb95f0c806f580ab4b12c92c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 18, 2026.

Transparency log

Security for market-structure 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for market_structure-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 18, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page