downtrend-duration-analyzer

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Downtrend Duration Analyzer

下跌趋势持续时间分析器

Overview

概述

Analyze historical price data to identify downtrend periods (peak-to-trough) and build statistical distributions of correction durations. Generate interactive HTML visualizations with histograms segmented by sector and market cap to help traders understand typical recovery timeframes and set realistic expectations for mean reversion strategies.
分析历史价格数据以识别下跌趋势周期(从峰值到谷底),并构建回调时长的统计分布。生成按行业和市值划分的交互式HTML可视化直方图,帮助交易者了解典型的恢复时间范围,为均值回归策略设定合理预期。

When to Use

适用场景

  • Trader asks about typical correction lengths for a sector or market cap tier
  • User wants to understand historical drawdown recovery times
  • Building mean reversion or pullback strategies that need realistic holding period estimates
  • Comparing correction behavior across different market segments
  • Setting stop-loss timeouts or position holding period limits
  • 交易者咨询某行业或市值层级的典型回调时长时
  • 用户想要了解历史回撤的恢复时间时
  • 构建需要合理估算持仓周期的均值回归或回调策略时
  • 比较不同细分市场的回调表现时
  • 设置止损超时时间或持仓周期限制时

Prerequisites

前置要求

  • Python 3.9+
  • FMP API key (set
    FMP_API_KEY
    environment variable or use
    --api-key
    )
  • Required packages:
    requests
    ,
    pandas
    ,
    numpy
    (standard data analysis stack)
  • Python 3.9+
  • FMP API密钥(设置
    FMP_API_KEY
    环境变量,或使用
    --api-key
    参数传入)
  • 依赖包:
    requests
    pandas
    numpy
    (标准数据分析技术栈)

Workflow

工作流程

Step 1: Fetch Historical Price Data

步骤1:获取历史价格数据

Run the analysis script to fetch OHLC data for a universe of stocks and identify downtrend periods.
bash
python3 skills/downtrend-duration-analyzer/scripts/analyze_downtrends.py \
  --sector "Technology" \
  --lookback-years 5 \
  --output-dir reports/
运行分析脚本获取全市场股票的OHLC数据,识别下跌趋势周期。
bash
python3 skills/downtrend-duration-analyzer/scripts/analyze_downtrends.py \
  --sector "Technology" \
  --lookback-years 5 \
  --output-dir reports/

Step 2: Analyze Downtrend Durations

步骤2:分析下跌趋势持续时间

The script automatically:
  1. Identifies local peaks and troughs using rolling window analysis
  2. Calculates duration (trading days) and depth (% decline) for each downtrend
  3. Segments results by sector and market cap tier (Mega, Large, Mid, Small)
  4. Computes summary statistics (median, mean, percentiles)
脚本会自动完成以下操作:
  1. 使用滚动窗口分析识别局部峰值和谷底
  2. 计算每一轮下跌的持续时间(交易日)和幅度(跌幅百分比)
  3. 按行业和市值层级(超大盘、大盘、中盘、小盘)划分结果
  4. 计算汇总统计数据(中位数、均值、百分位数)

Step 3: Generate Interactive HTML Visualization

步骤3:生成交互式HTML可视化

bash
python3 skills/downtrend-duration-analyzer/scripts/generate_histogram_html.py \
  --input reports/downtrend_analysis_*.json \
  --output-dir reports/
This creates an interactive HTML file with:
  • Histogram of downtrend durations
  • Filters for sector and market cap
  • Hover tooltips with percentile information
  • Summary statistics table
bash
python3 skills/downtrend-duration-analyzer/scripts/generate_histogram_html.py \
  --input reports/downtrend_analysis_*.json \
  --output-dir reports/
这一步会生成交互式HTML文件,包含以下内容:
  • 下跌趋势持续时间直方图
  • 行业和市值筛选器
  • 带百分位信息的悬浮提示
  • 统计数据汇总表

Step 4: Review Distribution Insights

步骤4:查看分布分析结论

Load the generated markdown report to interpret the findings:
  • Short corrections (5-15 days): Typical pullbacks within uptrends
  • Medium corrections (15-40 days): Standard sector rotations
  • Extended corrections (40+ days): Trend changes or bear markets
加载生成的markdown报告解读分析结果:
  • 短期回调(5-15天):上升趋势中的典型回踩
  • 中期回调(15-40天):常规行业轮动
  • 长期回调(40天以上):趋势转向或熊市

Output Format

输出格式

JSON Report

JSON报告

json
{
  "schema_version": "1.0",
  "analysis_date": "2026-03-28T07:00:00Z",
  "parameters": {
    "lookback_years": 5,
    "sector_filter": "Technology",
    "peak_window": 20,
    "trough_window": 20
  },
  "summary": {
    "total_downtrends": 1234,
    "median_duration_days": 18,
    "mean_duration_days": 24.5,
    "p25_duration_days": 10,
    "p75_duration_days": 32,
    "p90_duration_days": 55
  },
  "by_sector": {
    "Technology": {
      "count": 456,
      "median_days": 15,
      "mean_days": 20.3
    }
  },
  "by_market_cap": {
    "Mega": {"count": 200, "median_days": 12},
    "Large": {"count": 300, "median_days": 16},
    "Mid": {"count": 400, "median_days": 22},
    "Small": {"count": 334, "median_days": 28}
  },
  "downtrends": [
    {
      "symbol": "AAPL",
      "sector": "Technology",
      "market_cap_tier": "Mega",
      "peak_date": "2025-01-15",
      "trough_date": "2025-02-10",
      "duration_days": 18,
      "depth_pct": -12.5
    }
  ]
}
json
{
  "schema_version": "1.0",
  "analysis_date": "2026-03-28T07:00:00Z",
  "parameters": {
    "lookback_years": 5,
    "sector_filter": "Technology",
    "peak_window": 20,
    "trough_window": 20
  },
  "summary": {
    "total_downtrends": 1234,
    "median_duration_days": 18,
    "mean_duration_days": 24.5,
    "p25_duration_days": 10,
    "p75_duration_days": 32,
    "p90_duration_days": 55
  },
  "by_sector": {
    "Technology": {
      "count": 456,
      "median_days": 15,
      "mean_days": 20.3
    }
  },
  "by_market_cap": {
    "Mega": {"count": 200, "median_days": 12},
    "Large": {"count": 300, "median_days": 16},
    "Mid": {"count": 400, "median_days": 22},
    "Small": {"count": 334, "median_days": 28}
  },
  "downtrends": [
    {
      "symbol": "AAPL",
      "sector": "Technology",
      "market_cap_tier": "Mega",
      "peak_date": "2025-01-15",
      "trough_date": "2025-02-10",
      "duration_days": 18,
      "depth_pct": -12.5
    }
  ]
}

Markdown Report

Markdown报告

markdown
undefined
markdown
undefined

Downtrend Duration Analysis

下跌趋势持续时间分析

Date: 2026-03-28 Lookback: 5 years Sector: Technology
日期: 2026-03-28 回溯周期: 5年 行业: 科技

Summary Statistics

统计汇总

MetricValue
Total Downtrends1,234
Median Duration18 days
Mean Duration24.5 days
25th Percentile10 days
75th Percentile32 days
90th Percentile55 days
指标数值
总下跌次数1,234
中位数持续时长18天
平均持续时长24.5天
25百分位时长10天
75百分位时长32天
90百分位时长55天

By Market Cap Tier

按市值层级划分

TierCountMedianMean
Mega ($200B+)20012 days15.2 days
Large ($10-200B)30016 days20.1 days
Mid ($2-10B)40022 days28.4 days
Small (<$2B)33428 days35.6 days
层级数量中位数时长平均时长
超大盘 ($200B+)20012天15.2天
大盘 ($10-200B)30016天20.1天
中盘 ($2-10B)40022天28.4天
小盘 (<$2B)33428天35.6天

Key Insights

核心结论

  1. Larger companies recover faster from corrections
  2. Technology sector shows shorter median correction than market average
  3. 90% of corrections resolve within 55 trading days
undefined
  1. 规模更大的公司从回调中恢复的速度更快
  2. 科技行业的回调中位数时长低于市场平均水平
  3. 90%的回调会在55个交易日内结束
undefined

HTML Visualization

HTML可视化

Interactive histogram saved to
reports/downtrend_histogram_YYYY-MM-DD.html
with:
  • Plotly.js-based interactive charts
  • Sector and market cap dropdown filters
  • Duration distribution with bin controls
  • Percentile markers (P25, P50, P75, P90)
Reports are saved to
reports/
with filenames:
  • downtrend_analysis_YYYY-MM-DD_HHMMSS.json
  • downtrend_analysis_YYYY-MM-DD_HHMMSS.md
  • downtrend_histogram_YYYY-MM-DD_HHMMSS.html
交互式直方图保存至
reports/downtrend_histogram_YYYY-MM-DD.html
,包含:
  • 基于Plotly.js的交互式图表
  • 行业和市值下拉筛选器
  • 支持调整分组区间的持续时间分布
  • 百分位标记(P25、P50、P75、P90)
报告保存至
reports/
目录,文件名格式如下:
  • downtrend_analysis_YYYY-MM-DD_HHMMSS.json
  • downtrend_analysis_YYYY-MM-DD_HHMMSS.md
  • downtrend_histogram_YYYY-MM-DD_HHMMSS.html

Resources

资源

  • scripts/analyze_downtrends.py
    -- Main analysis script for fetching data and computing downtrend durations
  • scripts/generate_histogram_html.py
    -- HTML visualization generator with interactive histograms
  • references/downtrend_methodology.md
    -- Peak/trough detection algorithms and market cap tier definitions
  • scripts/analyze_downtrends.py
    -- 核心分析脚本,用于获取数据和计算下跌趋势持续时间
  • scripts/generate_histogram_html.py
    -- 生成交互式直方图HTML可视化的脚本
  • references/downtrend_methodology.md
    -- 峰谷检测算法和市值层级定义说明

Key Principles

核心原则

  1. Statistical Rigor: Use robust peak/trough detection to avoid noise-induced false signals
  2. Segmentation Matters: Always analyze by sector and market cap; averages hide important differences
  3. Realistic Expectations: Use percentiles (not just means) to understand the full distribution of outcomes
  1. 统计严谨性:使用可靠的峰谷检测算法,避免噪声导致的虚假信号
  2. 分组分析的重要性:始终按行业和市值分析,平均值会掩盖重要差异
  3. 预期合理化:使用百分位数(而非仅均值)了解完整的结果分布