downtrend-duration-analyzer
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDowntrend 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 environment variable or use
FMP_API_KEY)--api-key - Required packages: ,
requests,pandas(standard data analysis stack)numpy
- 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:
- Identifies local peaks and troughs using rolling window analysis
- Calculates duration (trading days) and depth (% decline) for each downtrend
- Segments results by sector and market cap tier (Mega, Large, Mid, Small)
- Computes summary statistics (median, mean, percentiles)
脚本会自动完成以下操作:
- 使用滚动窗口分析识别局部峰值和谷底
- 计算每一轮下跌的持续时间(交易日)和幅度(跌幅百分比)
- 按行业和市值层级(超大盘、大盘、中盘、小盘)划分结果
- 计算汇总统计数据(中位数、均值、百分位数)
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
undefinedmarkdown
undefinedDowntrend Duration Analysis
下跌趋势持续时间分析
Date: 2026-03-28
Lookback: 5 years
Sector: Technology
日期: 2026-03-28
回溯周期: 5年
行业: 科技
Summary Statistics
统计汇总
| Metric | Value |
|---|---|
| Total Downtrends | 1,234 |
| Median Duration | 18 days |
| Mean Duration | 24.5 days |
| 25th Percentile | 10 days |
| 75th Percentile | 32 days |
| 90th Percentile | 55 days |
| 指标 | 数值 |
|---|---|
| 总下跌次数 | 1,234 |
| 中位数持续时长 | 18天 |
| 平均持续时长 | 24.5天 |
| 25百分位时长 | 10天 |
| 75百分位时长 | 32天 |
| 90百分位时长 | 55天 |
By Market Cap Tier
按市值层级划分
| Tier | Count | Median | Mean |
|---|---|---|---|
| Mega ($200B+) | 200 | 12 days | 15.2 days |
| Large ($10-200B) | 300 | 16 days | 20.1 days |
| Mid ($2-10B) | 400 | 22 days | 28.4 days |
| Small (<$2B) | 334 | 28 days | 35.6 days |
| 层级 | 数量 | 中位数时长 | 平均时长 |
|---|---|---|---|
| 超大盘 ($200B+) | 200 | 12天 | 15.2天 |
| 大盘 ($10-200B) | 300 | 16天 | 20.1天 |
| 中盘 ($2-10B) | 400 | 22天 | 28.4天 |
| 小盘 (<$2B) | 334 | 28天 | 35.6天 |
Key Insights
核心结论
- Larger companies recover faster from corrections
- Technology sector shows shorter median correction than market average
- 90% of corrections resolve within 55 trading days
undefined- 规模更大的公司从回调中恢复的速度更快
- 科技行业的回调中位数时长低于市场平均水平
- 90%的回调会在55个交易日内结束
undefinedHTML Visualization
HTML可视化
Interactive histogram saved to with:
reports/downtrend_histogram_YYYY-MM-DD.html- 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 with filenames:
reports/downtrend_analysis_YYYY-MM-DD_HHMMSS.jsondowntrend_analysis_YYYY-MM-DD_HHMMSS.mddowntrend_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.jsondowntrend_analysis_YYYY-MM-DD_HHMMSS.mddowntrend_histogram_YYYY-MM-DD_HHMMSS.html
Resources
资源
- -- Main analysis script for fetching data and computing downtrend durations
scripts/analyze_downtrends.py - -- HTML visualization generator with interactive histograms
scripts/generate_histogram_html.py - -- Peak/trough detection algorithms and market cap tier definitions
references/downtrend_methodology.md
- -- 核心分析脚本,用于获取数据和计算下跌趋势持续时间
scripts/analyze_downtrends.py - -- 生成交互式直方图HTML可视化的脚本
scripts/generate_histogram_html.py - -- 峰谷检测算法和市值层级定义说明
references/downtrend_methodology.md
Key Principles
核心原则
- Statistical Rigor: Use robust peak/trough detection to avoid noise-induced false signals
- Segmentation Matters: Always analyze by sector and market cap; averages hide important differences
- Realistic Expectations: Use percentiles (not just means) to understand the full distribution of outcomes
- 统计严谨性:使用可靠的峰谷检测算法,避免噪声导致的虚假信号
- 分组分析的重要性:始终按行业和市值分析,平均值会掩盖重要差异
- 预期合理化:使用百分位数(而非仅均值)了解完整的结果分布