Metadata-Version: 2.4
Name: oisigma-drawdown
Version: 0.1.0
Summary: Descriptive drawdown episode distribution, depth percentiles, time-under-water statistics, and severity histogram (Python companion to Drawdown Severity Histogram [oisigma] on TradingView).
Author-email: Oisigma <m@oisigma.com>
License: MIT
Project-URL: TradingView Indicator, https://www.tradingview.com/script/W2UDFbGc-Drawdown-Severity-Histogram-oisigma/
Project-URL: Oisigma Homepage, https://oisigma.com
Project-URL: Empirical Proof, https://oisigma.com/proof
Project-URL: Working Paper, https://oisigma.com/paper
Project-URL: Research Blog, https://oisigma.com/blog
Keywords: drawdown,drawdown-histogram,time-under-water,tradingview,risk-analysis,quantitative-finance,oisigma
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Financial and Insurance Industry
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Office/Business :: Financial :: Investment
Classifier: Topic :: Scientific/Engineering :: Mathematics
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# Drawdown Severity Histogram `[oisigma]` — Python Companion & Episode Distribution Toolkit

[![TradingView Indicator](https://img.shields.io/badge/TradingView_Script-Drawdown_Severity_Histogram_%5Boisigma%5D-1F4E79)](https://www.tradingview.com/script/W2UDFbGc-Drawdown-Severity-Histogram-oisigma/)
[![Oisigma](https://img.shields.io/badge/Research-oisigma.com-1F4E79)](https://oisigma.com)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)

Most drawdown information on a chart is a single number: how far price is from its peak right now. That number is hard to judge on its own. Is a 7% drawdown a routine dip or a rare event? Historically, how deep do drawdowns on an instrument go, and how long do they take to recover?

**`oisigma-drawdown`** is the open-source Python companion to the free public TradingView indicator **[Drawdown Severity Histogram [oisigma]](https://www.tradingview.com/script/W2UDFbGc-Drawdown-Severity-Histogram-oisigma/)** developed by **[Oisigma](https://oisigma.com)**.

It scans a price series, identifies every completed drawdown episode, bins those episodes by maximum percentage depth, and computes **depth percentiles** (`median`, `90th`, `95th`, `deepest`) and **time-under-water duration statistics**—while tracking the currently open, unfinished drawdown separately so it does not bias the completed counts.

> **Important:** This is a **descriptive tool, not a predictive one**. It measures what has already happened on the loaded history. It does not forecast price, does not generate buy or sell signals, and says nothing about whether a current drawdown will deepen or reverse.

---

## Live TradingView Indicator (Free Community Script)

To plot the histogram and summary table directly on any TradingView chart:
* **[Drawdown Severity Histogram [oisigma] on TradingView](https://www.tradingview.com/script/W2UDFbGc-Drawdown-Severity-Histogram-oisigma/)**

For Oisigma's research on calibrated expected ranges and empirical volatility band audits across 97 years of S&P 500 data and 40 cross-asset instruments:
* **[Oisigma Home (`oisigma.com`)](https://oisigma.com)**
* **[Empirical Proof (`oisigma.com/proof`)](https://oisigma.com/proof)**
* **[Working Paper (`oisigma.com/paper`)](https://oisigma.com/paper)**
* **[Oisigma Research Blog (`oisigma.com/blog`)](https://oisigma.com/blog)**

---

## How It Counts Drawdown Episodes

1. **Episode Definition:** An episode begins at a peak, runs through its lowest point, and ends when price sets a new peak. At that moment its maximum depth (%) and its duration in bars (*time under water*) are recorded, and a new episode begins.
2. **Completed vs. Open Drawdown Separation:** Only completed episodes enter the histogram and the percentile statistics. The drawdown that is still open is tracked separately and highlighted in its provisional depth bin, because it has not finished yet and including it would bias the counts.
3. **Minimum Depth Floor (`min_depth_pct`, default `1.0%`):** Episodes shallower than 1% are excluded by default so trivial one-bar dips below a peak do not dominate the shallowest bucket and distort the median.
4. **Close vs. High/Low Consistency:** By default, closing prices are used for both peaks and troughs for an internally consistent series. When High/Low mode is used, on a bar that sets a new peak, the intrabar order of high and low is unknown, so that bar's low is not counted toward the next trough.

---

## Installation

```bash
pip install oisigma-drawdown
```

---

## Quickstart in Python

```python
import math
from oisigma_drawdown import analyze_drawdown_episodes

# Example price series
closes = [100.0 * math.exp(0.0015 * i - 0.018 * math.sin(i * 0.15)) for i in range(500)]

summary = analyze_drawdown_episodes(
    closes,
    bin_width_pct=5.0,   # 5% depth buckets
    min_depth_pct=1.0,   # Ignore trivial dips shallower than 1%
)

print(summary.format_report())
```

---

## Limitations to Know Before Using It

* **Statistics describe the loaded history only:** A short price series gives unreliable percentiles, so load as much historical data as possible.
* **Deep drawdowns are rare:** Even with long history, the right-hand tail bins and the 95th percentile rest on a small number of completed episodes.
* **Open drawdown comparison is provisional:** Compare percentiles with the open drawdown's *deepest point so far*, bearing in mind an unfinished drawdown can still deepen before recovering to a new peak.

---

## Related Oisigma Research & Links

* **[Drawdown Severity Histogram [oisigma] — TradingView Script](https://www.tradingview.com/script/W2UDFbGc-Drawdown-Severity-Histogram-oisigma/)**
* **[Oisigma — Calibrated Expected Range Indicator for TradingView](https://oisigma.com)**
* **[How It Works (`oisigma.com/how-it-works`)](https://oisigma.com/how-it-works)**
* **[Empirical Proof (`oisigma.com/proof`)](https://oisigma.com/proof)**
* **[Working Paper (`oisigma.com/paper`)](https://oisigma.com/paper)**
