OPEN-SOURCE SCRIPT
已更新 Market Cycle

# Market Cycle (SMA200 Slope / ATR)
**A regime classifier for long-term investors.** It labels the market as **UPTREND**, **DOWNTREND**, or **SIDEWAYS** by reading the *slope* of the Daily 200-period SMA, normalized by ATR so the reading is comparable across any symbol regardless of price or volatility. A hysteresis band and directional memory keep the state stable — it commits to a regime and holds it until the trend genuinely turns, instead of flipping on every wiggle.
## How it works
1. **ATR-normalized slope.** The per-bar change of the Daily SMA200 over a lookback window is divided by ATR (and ×100 for readability). This expresses the trend's strength in "ATRs per bar," a volatility-neutral unit that behaves the same on a $5 stock and a $5,000 index.
2. **Hysteresis state machine.** Entering a trend requires the slope to cross an **enter threshold**; leaving it only happens once the slope falls back through a lower **exit threshold**. The gap between the two creates a sticky band that filters noise and prevents whipsaw.
3. **Directional memory (continuation discount).** After a pause, re-entering the *same* direction the market just came from is treated as continuation and needs a smaller threshold, while a full reversal still requires the complete enter threshold. This makes the indicator resume established trends quickly but stay skeptical of reversals.
4. **Daily-locked & multi-timeframe consistent.** The signal and the entire state machine run inside the Daily context. The committed regime on a closed Daily bar is therefore **identical whether you view the Daily, Weekly, or Monthly chart** — no timeframe disagreement.
## Features
- **Regime coloring** — green (uptrend), red (downtrend), gray (sideways) applied to the SMA200 line and an optional background tint.
- **Sideways memory color** — optionally keep the prior trend's color during a pause, so you can see which trend the sideways phase came out of.
- **Trend age counter** — Daily bars since the last reversal (shown in the status line), continues counting through sideways pauses and only resets on a genuine trend flip.
- **Status line readout** — normalized slope value and trend age, without cluttering the price scale.
- **No repaint on history** — history is leak-free (`lookahead_off`); the current forming Daily bar updates in real time and locks at the Daily close.
## Inputs
- **SMA length / Slope lookback / ATR length** — the core signal parameters.
- **Enter & Exit thresholds** — width of the hysteresis band (in ATR/bar ×100).
- **Re-entry factor** — how much cheaper continuation is versus a fresh entry (0–1).
- **Visualization toggles** — SMA200 line, background tint, and sideways-color behavior.
## How to use
Use it as a **top-down regime filter**: take long-side setups only while the state is UPTREND, stand aside or reduce risk in SIDEWAYS, and treat DOWNTREND as a defensive/cash regime. Because the state is Daily-locked, it pairs well with a Weekly or Monthly chart for position-level context while you execute on lower timeframes.
*This indicator is a market-condition tool, not a buy/sell signal. It does not account for fundamentals, news, or risk management. Past regime behavior does not guarantee future results — always do your own research.*
**A regime classifier for long-term investors.** It labels the market as **UPTREND**, **DOWNTREND**, or **SIDEWAYS** by reading the *slope* of the Daily 200-period SMA, normalized by ATR so the reading is comparable across any symbol regardless of price or volatility. A hysteresis band and directional memory keep the state stable — it commits to a regime and holds it until the trend genuinely turns, instead of flipping on every wiggle.
## How it works
1. **ATR-normalized slope.** The per-bar change of the Daily SMA200 over a lookback window is divided by ATR (and ×100 for readability). This expresses the trend's strength in "ATRs per bar," a volatility-neutral unit that behaves the same on a $5 stock and a $5,000 index.
2. **Hysteresis state machine.** Entering a trend requires the slope to cross an **enter threshold**; leaving it only happens once the slope falls back through a lower **exit threshold**. The gap between the two creates a sticky band that filters noise and prevents whipsaw.
3. **Directional memory (continuation discount).** After a pause, re-entering the *same* direction the market just came from is treated as continuation and needs a smaller threshold, while a full reversal still requires the complete enter threshold. This makes the indicator resume established trends quickly but stay skeptical of reversals.
4. **Daily-locked & multi-timeframe consistent.** The signal and the entire state machine run inside the Daily context. The committed regime on a closed Daily bar is therefore **identical whether you view the Daily, Weekly, or Monthly chart** — no timeframe disagreement.
## Features
- **Regime coloring** — green (uptrend), red (downtrend), gray (sideways) applied to the SMA200 line and an optional background tint.
- **Sideways memory color** — optionally keep the prior trend's color during a pause, so you can see which trend the sideways phase came out of.
- **Trend age counter** — Daily bars since the last reversal (shown in the status line), continues counting through sideways pauses and only resets on a genuine trend flip.
- **Status line readout** — normalized slope value and trend age, without cluttering the price scale.
- **No repaint on history** — history is leak-free (`lookahead_off`); the current forming Daily bar updates in real time and locks at the Daily close.
## Inputs
- **SMA length / Slope lookback / ATR length** — the core signal parameters.
- **Enter & Exit thresholds** — width of the hysteresis band (in ATR/bar ×100).
- **Re-entry factor** — how much cheaper continuation is versus a fresh entry (0–1).
- **Visualization toggles** — SMA200 line, background tint, and sideways-color behavior.
## How to use
Use it as a **top-down regime filter**: take long-side setups only while the state is UPTREND, stand aside or reduce risk in SIDEWAYS, and treat DOWNTREND as a defensive/cash regime. Because the state is Daily-locked, it pairs well with a Weekly or Monthly chart for position-level context while you execute on lower timeframes.
*This indicator is a market-condition tool, not a buy/sell signal. It does not account for fundamentals, news, or risk management. Past regime behavior does not guarantee future results — always do your own research.*
版本注释
Update input defaults版本注释
- **Trend age counter now measures calendar days.** Previously it counted Daily bars since the last reversal, which skips weekends and market holidays and so understated the real elapsed time (a full-year trend read ~250 instead of ~365). It is now derived from bar timestamps, so the count reflects actual calendar days elapsed. The continue-through-sideways and reset-on-reversal behavior is unchanged.开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。
开源脚本
秉承TradingView的精神,该脚本的作者将其开源,以便交易者可以查看和验证其功能。向作者致敬!您可以免费使用该脚本,但请记住,重新发布代码须遵守我们的网站规则。
免责声明
这些信息和出版物并非旨在提供,也不构成TradingView提供或认可的任何形式的财务、投资、交易或其他类型的建议或推荐。请阅读使用条款了解更多信息。