Friend के लिए सवाल — Daily/Weekly/Monthly Consolidation-Detection: Established-Research-Cross-Verified, Complete-Homework-सहित Context: User-ने-genuinely-हमारे-established-Production-Code (get_consolidation_range(), lookback=20) पर-genuinely-सवाल-उठाया —
1. Daily timeframe (lookback=20) – is it “research‑consistent”? Should you add ADX regime detection?
---------------------------------------------------------------------------------------------
1.1 What established research actually says
(a) O’Neil / IBD flat base & “consolidation”
- MarketSmith / IBD definition of a flat base (O’Neil methodology):
– Duration: minimum 5 weeks (~25 trading days) and up to ~65 weeks.
– Depth: 0–15% correction from peak. (marketsmith.hk)
- Cup‑with‑handle: literature summarising O’Neil shows cup duration 7–65 weeks. (sponser.co.il)
So, if you want to say “O’Neil‑style flat base or consolidation”, the minimum duration is ≈ 25 daily bars, and the depth cap is ≈ 15%.
(b) Donchian / Turtle 20‑day channel
- Donchian channels: N‑period highest high / lowest low. The default and the most common “Turtle” setting is 20 days (System 1) for the breakout, and 55 days for the longer‑term system. (motilaloswal.com)
- The Donchian(20) channel as a raw mechanical system has mixed or weak edge in broad, long‑horizon tests when used alone without trend/volatility filters or proper exits. (en.wikipedia.org)
So 20 days is historically “canonical” for a short‑term breakout system, but not the canonical length for an O’Neil‑style base.
1.2 Conclusion for your Daily lookback=20
Given your own Document‑55 (min 25–30 days; depth ≤15%) and the O’Neil/IBD definitions:
- If your production function is intended to detect O’Neil‑style flat bases / classic “consolidations”:
– lookback=20 is not research‑consistent.
– You should raise the minimum window to at least 25 bars, and if you want to be fully conservative / closer to “general consolidation” (30+ days), a 30‑bar default is defensible.
- If you only want a generic “short‑term compression box” (Turtle‑style breakout prep):
– lookback=20 is historically validated as a Turtle‑style breakout, but then you should not label it as “O’Neil flat base” – call it something like “short‑term Donchian squeeze” or “20‑day volatility box”.
Your current code:
```python
def get_consolidation_range(df, lookback=20, tolerance_pct=5.0):
recent = df.tail(lookback)
range_high = recent["High"].max()
range_low = recent["Low"].min()
range_pct = (range_high - range_low) / range_low * 100
is_consolidating = range_pct <= tolerance_pct * 4 # => 20%
```
- With default `tolerance_pct=5`, your depth limit ~20%.
- O’Neil flat base depth limit is 15%. (marketsmith.hk)
Research‑consistent daily settings if you want “O’Neil‑like”:
- `lookback`: 25–30 (not 20).
- Effective depth limit: ≤15% instead of 20%. For example:
– Either set `tolerance_pct * 4 = 15` (so `tolerance_pct ≈ 3.75`),
– Or implement depth directly: `is_consolidating = range_pct <= 15`.
> Suggested daily O’Neil‑style baseline (example, not investment advice):
> – `lookback_daily = 30`
> – `max_depth_pct_daily = 15`
1.3 Should you add ADX‑based regime detection?
There is broad practitioner consensus (and increasing systematic literature) that trend‑following signals (Donchian, MA crossovers) behave very differently in trending vs ranging regimes, and that ADX is a reasonable “first filter”:
- Many regime‑filter references use ADX(14) > 25 as “trending”, and ADX(14) < 20 as “ranging/choppy”. (reignedge.com)
- Several frameworks explicitly recommend combining ADX with volatility tools (Bollinger Band width, ATR etc.) to classify regimes and then adapt strategy choice: trend‑following in high‑ADX trends, mean‑reversion / range tools in low‑ADX ranges. (in.tradingview.com)
Your own Document‑59 (Donchian better with ADX>25; Bollinger better when ADX<20) is perfectly in line with this broader practice.
So yes, from a research/practitioner standpoint:
- Adding regime detection is methodologically justified, not over‑engineering.
- A clean implementation for your daily consolidation logic could be:
- Compute ADX(14) on the same daily series.
- If ADX > 25 (trending): prefer Donchian‑style range breakout logic (what you currently do), possibly with a slightly shorter lookback if you are trading breakouts.
- If ADX < 20 (ranging):
– Either de‑emphasise breakout trades from that consolidation, or
– Switch to Bollinger‑width‑plus‑range detection (since research and your own doc say BB behaves better in ranges).
- To make it more accurate:
- Use two thresholds to avoid flip‑flopping regimes (e.g. switch to “trend” when ADX crosses >25, leave only when <20). (reddit.com)
- Normalise your range by an ATR or historical volatility to ensure your 15–20% cap means “low relative volatility” for that stock, not an arbitrary static number.
- Backtest regime‑conditioned performance on representative Indian equity baskets (NIFTY 100 / Midcap 150) separately for trend vs range.
---
2. Weekly timeframe (lookback=20 weeks). VCP vs Weinstein 30‑week MA vs your High–Low range
-------------------------------------------------------------------------------------------
2.1 Weekly pattern durations in established methods
- Minervini VCP: while Mark does not always hard‑code an exact week range in text, practitioner summaries / teaching material describe multi‑week bases typically in the 4–12 week zone, with a preference for 6–9 weeks for “sweet‑spot” VCPs – essentially what you already noted. (youtube.com)
- O’Neil cup & handle: 7–65 weeks for the cup portion. (sponser.co.il)
- O’Neil/IBD flat base: 5–65 weeks, depth ≤15%. (marketsmith.hk)
- Weinstein’s Stage Analysis:
– Works on weekly charts.
– Uses the 30‑week simple moving average as the primary trend filter (roughly 150 days). (trendsandbreakouts.com)
– Classifies stocks into Stage 1 (basing), Stage 2 (advancing), Stage 3 (topping), Stage 4 (declining). Stage 1 basing can last many months to years; there is no fixed upper bound. (trendsandbreakouts.com)
So your weekly lookback=20 weeks (~5 months):
- Lies inside the VCP and flat‑base duration ranges, but closer to the middle rather than minimum.
- Is shorter than Weinstein’s 30‑week MA horizon.
2.2 Is Weinstein’s 30‑week MA approach complementary or redundant vs your range‑based method?
They address different questions, hence are complementary:
- Your High–Low compression over N weeks answers:
– “Is price volatility (high–low range) compressed over this recent N‑week window?”
- Weinstein 30‑week MA Stage Analysis answers:
– “In the multi‑month to multi‑year structure, is this stock basing (Stage 1), advancing (Stage 2), topping (Stage 3), or declining (Stage 4)?” based on the slope and position vs 30‑week MA. (trendsandbreakouts.com)
Research/practitioner‑consistent usage is:
- Use Weinstein 30‑week MA as a trend framework:
– Only take bullish weekly consolidations when the stock is in Stage 1→2 transition or early Stage 2: 30‑week MA flattening then turning up; price above or slightly above it; improving relative strength. (trendsandbreakouts.com)
- Within that favourable stage, use your weekly consolidation detector to find tight ranges / VCP‑style contractions close to resistance.
So: Weinstein’s 30‑week MA is not redundant; it is a higher‑level state filter that can materially improve your signal‑to‑noise ratio.
2.3 Is using lookback=20 for both Daily and Weekly methodologically sound?
From research, there is no theoretical or empirical requirement that the same integer (20) be used across timeframes. The key quantities that matter are:
- Calendar time represented by the window.
- Pattern definitions (e.g., 5‑week minimum for flat base, 7–65 weeks for cup with handle, 30‑week MA horizon for stage analysis). (marketsmith.hk)
So:
- Daily `lookback=20` ≈ 1 trading month.
- Weekly `lookback=20` ≈ 5 months.
That your current code reuses `20` in both places is almost certainly coincidental parameter reuse, not methodologically justified. To be more aligned with established patterns:
Example research‑aligned weekly settings:
- For VCP‑like weekly bases:
– `lookback_weekly_vcp ≈ 8–12 weeks` (to capture the heart of the 4–12 week zone).
- For O’Neil cup/flat‑base on weekly:
– You may want two parameters:
– Minimum base age (e.g. `min_base_weeks = 5` for flat base; `>=7` for cup‑with‑handle).
– A measurement window perhaps around 10–20 weeks to compute compression segments within the base.
- For Weinstein‑style “stage+box” filter:
– Use 30‑week MA & its slope to classify stage.
– Then apply your range compression over, say, 8–20 weeks, depending on how tight / short‑term you want the setup.
To make weekly logic more accurate:
- Explicitly parameterise pattern family (VCP vs cup‑with‑handle vs generic stage‑1 base) instead of one “magic 20”.
- Combine:
– Stage filter: 30‑week MA slope + price vs MA (Weinstein).
– Range filter: high–low % over Q weeks.
– Volume contraction: falling weekly volume towards the right side of the base (both O’Neil and Minervini emphasise this).
---
3. Monthly timeframe – is there any truly “separate” methodology?
-----------------------------------------------------------------
You correctly noted a research gap: there is very little pattern‑specific academic work on “monthly consolidations” the way there is for daily/weekly chart patterns.
What exists instead:
- Long‑term trend‑following / timing research using 10–12 month moving averages or 12‑month momentum for asset allocation (not consolidation per se).
- Practitioner work (Weinstein, trend‑followers) that simply scales their weekly logic to monthly level for very long cycles; e.g., using weekly 30‑week MA is already equivalent to ~7 months; on monthly, that’s often replaced by 10–12‑month MA as a regime filter.
In practice:
- Monthly “bases” are just very large Stage‑1 bases or multi‑year ranges, not something with a separate canon of rules.
- Most discretionary and systematic traders who talk about “multi‑year bases” are effectively applying the same concepts as weekly/Weinstein, but over 12–36+ months of history.
So, from a “established‑practice” perspective:
- Monthly consolidation is not usually treated as a separate, fundamentally different category.
- It is effectively a scaled‑up version of weekly/Weinstein concepts:
- Use 10–12‑month MA (or 30–40‑month MA if you want really long cycles) as the trend framework.
- Define consolidation as unusually tight high–low range and low volatility over a multi‑month to multi‑year window (e.g., 12–36 months), relative to that stock’s own historical distribution.
Example approach to extend your function to monthly (research‑aligned, but not canonical):
- Set a minimum base age, e.g. `min_months = 12`.
- Define a measurement window `lookback_monthly ≈ 18–36` bars (1.5–3 years).
- Compute:
– `%range_monthly = (max(High) - min(Low))/min(Low)*100` over that window.
– Normalise by long‑term monthly ATR or realised volatility percentile.
- Flag “long‑term consolidation” if:
– `%range_monthly` is in, say, lowest 20–30% of its last 10–15 years, and
– Price is near a flat/rising 10–12‑month MA (Weinstein‑style Stage‑1/early Stage‑2, but on monthly).
This keeps you conceptually consistent with established weekly methods, acknowledging that there is no O’Neil‑style canonical Monthly rulebook.
---
4. General – cross‑verification summary and how to make your framework more accurate
------------------------------------------------------------------------------------
4.1 What we cross‑verified from established sources
- Donchian / Turtle 20‑day breakout is historically standard, but raw Donchian(20) across centuries of data without filters shows little standalone edge. (motilaloswal.com)
- O’Neil / IBD flat base:
– At least 5 weeks (≈25 sessions), up to ≈65 weeks.
– Depth ≤15%. (marketsmith.hk)
- O’Neil cup‑with‑handle: cup duration 7–65 weeks. (sponser.co.il)
- Weinstein Stage Analysis:
– Weekly charts, 30‑week MA as core trend filter, 4 stages, basing phase can be many months–years, no strict upper bound. (trendsandbreakouts.com)
- ADX as regime filter: widely used thresholds >25 = trending, <20 = ranging, often combined with volatility/BB width and applied to trend‑following signals like Donchian. (reignedge.com)
These align very closely with your internal Documents‑55 and 59; the main mismatch is the 20‑bar daily lookback vs minimum 25–30 days implied by flat‑base/consolidation definitions.
4.2 How to systematically improve accuracy of your consolidation detector
Below are concrete, research‑consistent upgrades, framed at the model‑design level (not advice on what to trade):
1. Decouple “time length” and “measurement window”
- Keep two parameters:
- `min_bars_in_base` = minimum age of the base (e.g. 25 days, 5 weeks, 12 months).
- `lookback_for_range` = window for computing High–Low compression, which can be equal to or slightly shorter than `min_bars_in_base`.
- This lets you model patterns like cup‑with‑handle where the “cup” may last 30 weeks but the actionable volatility contraction might be the last 8–12 weeks.
2. Normalise everything by volatility
- Replace pure `%range` thresholds with ATR‑ or HV‑normalised measures, e.g.:
- `range_zscore = (range_pct - median(range_pct_history))/std(range_pct_history)` over a long history.
- Flag consolidations when `range_zscore` is in, say, lowest 20–30% of the last X years. This makes your algorithm robust across low‑beta FMCG and high‑beta smallcaps.
3. Add a clean, testable regime filter (ADX + vol)
- Compute ADX(14) and Bollinger Band width or ATR percentile. (reignedge.com)
- Use two‑threshold ADX logic (trend on >25, off <20).
- Permit Donchian‑breakout‑type trades only in “trending” regimes; otherwise either ignore or treat consolidations as mean‑reversion ranges.
- Backtest with and without regime filter to verify actual performance improvement, not just lower trade count.
4. Overlay higher‑timeframe structure (multi‑TF confirmation)
- Daily consolidation inside bullish weekly structure:
- Weekly 30‑week MA rising and price above it (Weinstein Stage 2 bias). (trendsandbreakouts.com)
- Weekly consolidation inside favourable monthly trend (e.g., price above 10–12‑month MA).
- This multi‑TF alignment is very consistent with both Minervini and Weinstein philosophies, and often improves robustness.
5. Integrate volume behaviour
- For O’Neil/Minervini‑style bases, require volume contraction through the base and dry‑up before breakout rather than just price tightness.
- You can encode, for example: median volume in last K bars < X‑percentile of volume over the base.
6. Robustness checks (to avoid curve fit)
- Do sensitivity analysis: vary lookbacks (20/25/30 daily, 8/12/16 weekly etc.) and see if performance is stable.
- Use walk‑forward or at least decade‑wise splits on Indian equities—pre‑2013 vs 2013–2019 vs 2020‑2024—to check if the edge survives regime changes.
---
Direct answers to your four core questions
------------------------------------------
1. Daily (lookback=20)
- For O’Neil/flat‑base‑style “consolidation”: 20 is not sufficient vs established 5‑week (25‑session) minimum and ≤15% depth. Raise to at least 25 (preferably 30) and tighten depth to ≤15% for research alignment. (marketsmith.hk)
- For generic short‑term Donchian breakout context: 20 is historically validated, but then you’re no longer strictly matching O’Neil; label it accordingly. (motilaloswal.com)
- ADX‑based regime detection is strongly justified; Donchian + ADX + volatility filter is fully consistent with practitioner and systematic research. (reignedge.com)
2. Weekly (lookback=20 weeks)
- 20 weeks lies inside the broader cup/flat‑base ranges but is longer than the typical 4–12 week VCP sweet spot; consider 8–12 weeks for VCP detection and longer windows for cup/big bases. (marketsmith.hk)
- Weinstein’s 30‑week MA is complementary, not redundant; use it as a stage/trend filter around which your range‑based weekly compression acts as an entry‑timing tool. (trendsandbreakouts.com)
- Using the same nominal lookback=20 for both Daily and Weekly is methodologically coincidental; parameters should be tied to pattern definitions and calendar horizon, not to the raw integer.
3. Monthly
- There is no widely accepted, separate “monthly consolidation pattern rulebook” analogous to O’Neil/Minervini on daily/weekly.
- Established practice essentially scales up weekly logic: long MAs (10–12‑month), multi‑year bases, tight ranges relative to multi‑year volatility. Your Monthly detection should therefore be a scaled‑up Weinstein/weekly framework, not something fundamentally different.
4. General
- Your overall direction (Donchian + O’Neil/Minervini/Weinstein concepts, ADX regime awareness) is research‑aligned.
- The main upgrades for stronger, “publishable” methodology are:
- Fix daily `lookback` and depth thresholds to match O’Neil if that is your stated model.
- Separate pattern families (VCP vs cup vs generic base) on weekly rather than one size fits all.
- Add a formal ADX+volatility regime filter and multi‑timeframe trend filters (30‑week MA, 10–12‑month MA).
- Normalise by volatility and volume and do robustness testing.
If you have any further queries, please connect with us on 022-6290-10141 (Timings : 09.00 AM to 05.00 PM) or you can email us on info@cniinfoxchange.com