Alpha Research5
Appearance
Component Architecture
Branch: rikk_mtf_backtest001 | Companion to: Alpha_Research
This page documents the component-level implementation of the MTFDR (Multi-Timeframe Dynamic Regime) backtesting system. Each component translates trading philosophy into executable Python.
For Developers: Each section maps trading concepts to code. You don't need trading experience—the "why" is explained before the "how".
PART 1: Pipeline Architecture
The system processes each 5-minute bar through 5 sequential layers:
Raw OHLC Data (1D, 4H, 1H, 15M, 5M)
│
▼
┌─────────────────────────────────────────────────────────────────────┐
│ LAYER 1: multi_timeframe_analysis.py │
│ "Are all timeframes pointing the same direction?" │
│ │
│ • Analyzes [1D, 4H, 1H, 15M, 5M] via TrendAnalysisCore │
│ • Calculates weighted alignment score (hierarchical weights) │
│ • 15-minute confirmation requirement │
│ • Output: TimeframeAnalysisResult (aligned/not, direction, score) │
└─────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────┐
│ LAYER 2: market_state_analysis.py │
│ "What kind of market are we in?" │
│ │
│ 7 Detection Algorithms: │
│ 1. Railroad Trend - Strong one-sided move (consistency > 80%) │
│ 2. Creeper Move - Slow grinding price action (avg range < 0.5%) │
│ 3. Volatility - High (>1%), Normal, Low (<0.3%) │
│ 4. Market State Classification - Trending/Range/Creeper/Momentum │
│ 5. Two-Day Trend - Both days close same direction │
│ 6. Trend Phase - EARLY/MIDDLE/LATE (based on MA crossover) │
│ 7. Institutional Behavior - Fight, Accumulation, BOS │
│ Output: MarketStateResult │
└─────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────┐
│ LAYER 3: setup_quality_detection.py │
│ "How good is this setup? Grade it A+ to F." │
│ │
│ 5-Factor Weighted Scoring: │
│ • Timeframe Alignment (30%) - From Layer 1 │
│ • Trend Strength (20%) - Penalties: creeper, MA struggle │
│ • Entry Quality (15%) - Near MA bonus, clean entry bonus │
│ • Key Level Proximity (20%) - Near support/resistance │
│ • Risk/Reward (15%) - Lookup table: <1=0, 1-1.5=40, 1.5-2=70... │
│ │
│ Penalties Applied: creeper(-50), ma_struggle(-30), no_2day(-30) │
│ A+ Enforcement: Must pass all 3 criteria or capped at 79 │
│ Output: SetupQualityResult (grade, score, position_size) │
└─────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────┐
│ LAYER 4: signal_generation_trade_management.py │
│ "Should we trade? What direction? Where's entry/stop/target?" │
│ │
│ Direction Logic: │
│ • MA21 rising → LONG direction │
│ • MA21 declining → SHORT direction │
│ • Flat → MTF fallback │
│ │
│ Gate Filters (JIRA tickets): │
│ • TICKET-12: Hour filter (block 9AM, 10PM, 11PM) │
│ • TICKET-19: MA direction alignment │
│ • TICKET-20: Probability zone filter (uses probability_zone_analysis)│
│ • TICKET-21: Color change confirmation (currently DISABLED) │
│ │
│ Output: Signal (direction, entry, stop, target, grade) │
└─────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────┐
│ LAYER 5: trade_execution_engine.py │
│ "Execute the trade, manage the position, calculate P&L" │
│ │
│ Execution: │
│ • Signal-to-trade conversion with slippage modeling │
│ • Commission: Rs 20/lot (Dhan) │
│ • CTT: 0.01% on sell side │
│ │
│ Position Management: │
│ • TICKET-15/17/20: Trailing stop (ATR-based, 2x ATR) │
│ • TICKET-13/17: Breakeven stop (activate at 25pt profit) │
│ • Time-based exit (8 hour max holding) │
│ │
│ Output: Trade objects with complete P&L │
└─────────────────────────────────────────────────────────────────────┘
PART 2: Component Index
| Layer | Component | What It Does | Code Link |
|---|---|---|---|
| 1 | multi_timeframe_analysis.py | MTF alignment across 5 timeframes | View Code |
| (1a) | trend_analysis_core.py | Per-timeframe trend direction, MA slopes | View Code |
| 2 | market_state_analysis.py | Railroad, creeper, volatility, trend phase | View Code |
| 3 | setup_quality_detection.py | 5-factor scoring, A+ to F grades | View Code |
| 4 | signal_generation_trade_management.py | Direction, filters, entry/stop/target | View Code |
| 5 | trade_execution_engine.py | Execution, position mgmt, P&L | View Code |
Support Components:
| Component | What It Does | Used By | Code Link |
|---|---|---|---|
| probability_zone_analysis.py | Probability zones (80%/65%/35%/15%), crash bar, color change | signal_generation (TICKET-20) | View Code |
| data_manager.py | Data loading, timeframe aggregation | main.py | View Code |
| backtesting_analytics.py | Performance metrics, reporting | main.py | View Code |
PART 3: Trading Concept → Code Mapping
| PDF Concept | Implementation Status | Code Location | Notes |
|---|---|---|---|
| Halves/Thirds (80%/65%/35%/15%) | ✅ Implemented | probability_zone_analysis.py | Used via TICKET-20 filter in signal_generation |
| Crash Bar | ✅ Implemented | probability_zone_analysis.py | Bar 2x average = structural break |
| Color Change | ⚠️ Disabled | probability_zone_analysis.py | TICKET-21: Too restrictive, set enable_color_change_filter=False |
| Three-Finger Spread | ✅ Implemented | probability_zone_analysis.py | Price/21MA/200MA separation detection (PDF calls it large MA spread) |
| 45-degree angle | ✅ Implemented | trend_analysis_core.py | MA slope analysis, "flattish" detection |
| Railroad Trend | ✅ Implemented | market_state_analysis.py | Consistency > 80%, 3+ strong bars |
| Creeper Move | ✅ Implemented | market_state_analysis.py | Avg range < 0.5% over 7 bars |
| Two-Day Trend | ✅ Implemented | market_state_analysis.py | Both daily bars close same direction |
| Fab Four (21MA + 200MA zone) | ❌ Not Implemented | - | PDF concept not directly coded; closest is Three-Finger Spread |
| Traffic Jam | ❌ Not Implemented | - | Prior day congestion as S/R not coded |
| Big Bar by Big Bar | ⚠️ Partial | trend_analysis_core.py | Slope calculation uses significant moves; no explicit "noise filter" |
PART 4: Key Configuration Parameters
Setup Quality Weights (setup_quality_detection.py)
timeframe_alignment_weight = 0.30 # 30%
trend_strength_weight = 0.20 # 20%
entry_technique_weight = 0.15 # 15%
key_level_proximity_weight = 0.20 # 20%
risk_reward_weight = 0.15 # 15%
-------
= 1.00 # Must sum to 1.0
Penalty Constants
creeper_move_penalty = -50 # Major penalty for slow grinding ma_struggle_penalty = -30 # Price repeatedly testing MA two_day_trend_penalty = -30 # Missing 2-day directional trend phase_mismatch_penalty = -25 # Not in MIDDLE phase railroad_trend_bonus = +15 # Strong one-sided move
Grade Thresholds
A+ = score >= 90 → 2 lots A = score >= 80 → 1 lot B = score >= 70 → 1 lot C = score >= 60 → 1 lot D = score >= 50 → 1 lot F = score < 50 → 1 lot (signal may be rejected by filters)
PART 5: Component Details
Multi-Timeframe Analysis
See: Component:Multi_Timeframe_Analysis
Trend Analysis Core
See: Component:Trend_Analysis_Core
Market State Analysis
See: Component:Market_State_Analysis
Setup Quality Detection
See: Component:Setup_Quality_Detection
Signal Generation
See: Component:Signal_Generation
Trade Execution
See: Component:Trade_Execution
PART 6: JIRA Ticket Reference
| Ticket | Component | What It Fixed |
|---|---|---|
| TICKET-5 | setup_quality | Realistic position sizing (was 100-200 lots, now 1-2 lots) |
| TICKET-8 | signal_generation | CTT rate correction (was 0.05%, now 0.01%) |
| TICKET-11 | signal_generation | BOS stop distance (extended lookback from 5 to 10 bars) |
| TICKET-12 | signal_generation | Hour filter (block 9AM, 10PM, 11PM) |
| TICKET-13 | trade_execution | Breakeven stop (25pt activation) |
| TICKET-15/17/20 | trade_execution | Trailing stop (ATR-based, 2x ATR) |
| TICKET-19 | signal_generation | MA direction alignment filter |
| TICKET-20 | signal_generation | Probability zone filter (integrates probability_zone_analysis) |
| TICKET-21 | signal_generation | Color change filter (DISABLED - too restrictive) |
| TICKET-25 | market_state | Fixed creeper detection to use 1H data, not 5M |