Jump to content

Alpha Research5: Difference between revisions

From PlusEV Wiki Page
No edit summary
No edit summary
Line 3: Line 3:
'''Branch:''' <code>rikk_mtf_backtest001</code> | '''Companion to:''' [[Alpha_Research]]
'''Branch:''' <code>rikk_mtf_backtest001</code> | '''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.
Component-level implementation of the MTFDR 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".


----
----
Line 24: Line 22:
│  • Calculates weighted alignment score (hierarchical weights)      │
│  • Calculates weighted alignment score (hierarchical weights)      │
│  • 15-minute confirmation requirement                              │
│  • 15-minute confirmation requirement                              │
│  • Output: TimeframeAnalysisResult (aligned/not, direction, score)
│  • Output: TimeframeAnalysisResult (aligned, direction, score)    
└─────────────────────────────────────────────────────────────────────┘
└─────────────────────────────────────────────────────────────────────┘
               │
               │
Line 34: Line 32:
│  7 Detection Algorithms:                                            │
│  7 Detection Algorithms:                                            │
│  1. Railroad Trend - Strong one-sided move (consistency > 80%)      │
│  1. Railroad Trend - Strong one-sided move (consistency > 80%)      │
│  2. Creeper Move - Slow grinding price action (avg range < 0.5%)  
│  2. Creeper Move - Slow grinding action (avg range < 0.5%)        
│  3. Volatility - High (>1%), Normal, Low (<0.3%)                   
│  3. Volatility Classification - High/Normal/Low                    
│  4. Market State Classification - Trending/Range/Creeper/Momentum  
│  4. Market State - Trending/Range/Creeper/Momentum                
│  5. Two-Day Trend - Both days close same direction                  │
│  5. Two-Day Trend - Both days close same direction                  │
│  6. Trend Phase - EARLY/MIDDLE/LATE (based on MA crossover)       
│  6. Trend Phase - EARLY/MIDDLE/LATE                                
│  7. Institutional Behavior - Fight, Accumulation, BOS              │
│  7. Institutional Behavior - Fight, Accumulation, BOS              │
│  Output: MarketStateResult                                          │
│  Output: MarketStateResult                                          │
Line 49: Line 47:
│                                                                    │
│                                                                    │
│  5-Factor Weighted Scoring:                                        │
│  5-Factor Weighted Scoring:                                        │
│  • Timeframe Alignment (30%) - From Layer 1                       
│  • Timeframe Alignment (30%)                                      
│  • Trend Strength (20%) - Penalties: creeper, MA struggle         
│  • Trend Strength (20%)                                            
│  • Entry Quality (15%) - Near MA bonus, clean entry bonus         
│  • Entry Quality (15%)                                            
│  • Key Level Proximity (20%) - Near support/resistance             
│  • Key Level Proximity (20%)                                      
│  • Risk/Reward (15%) - Lookup table: <1=0, 1-1.5=40, 1.5-2=70...   
│  • Risk/Reward (15%)                                              
│                                                                    │
│                                                                    │
│  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)          │
│  Output: SetupQualityResult (grade, score, position_size)          │
└─────────────────────────────────────────────────────────────────────┘
└─────────────────────────────────────────────────────────────────────┘
Line 66: Line 62:
│                                                                    │
│                                                                    │
│  Direction Logic:                                                  │
│  Direction Logic:                                                  │
│  • MA21 rising → LONG direction                                   
│  • MA21 rising → LONG                                              
│  • MA21 declining → SHORT direction                               
│  • MA21 declining → SHORT                                          
│  • Flat → MTF fallback                                              │
│  • Flat → MTF fallback                                              │
│                                                                    │
│                                                                    │
│  Gate Filters (JIRA tickets):                                      │
│  Gate Filters: Hour, MA Direction, Probability Zone               
│  • 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)            │
│  Output: Signal (direction, entry, stop, target, grade)            │
└─────────────────────────────────────────────────────────────────────┘
└─────────────────────────────────────────────────────────────────────┘
Line 84: Line 75:
│  "Execute the trade, manage the position, calculate P&L"            │
│  "Execute the trade, manage the position, calculate P&L"            │
│                                                                    │
│                                                                    │
│  Execution:                                                         │
│  Execution: Slippage modeling, Commission (Rs 20/lot), CTT (0.01%
│  • Signal-to-trade conversion with slippage modeling               │
│  Position Management: Trailing stop, Breakeven stop, Time exit    
│  • 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                            │
│  Output: Trade objects with complete P&L                            │
└─────────────────────────────────────────────────────────────────────┘
└─────────────────────────────────────────────────────────────────────┘
Line 104: Line 87:
{| class="wikitable" style="width:100%"
{| class="wikitable" style="width:100%"
|-
|-
! Layer !! Component !! What It Does !! Code Link
! # !! Component !! Purpose !! Code Link
|-
|-
| 1 || [[#Multi-Timeframe Analysis|multi_timeframe_analysis.py]] || MTF alignment across 5 timeframes || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/multi_timeframe_analysis.py View Code]
| 1 || [[Component:Multi_Timeframe_Analysis|multi_timeframe_analysis.py]] || MTF alignment across 5 timeframes || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/multi_timeframe_analysis.py View]
|-
|-
| (1a) || [[#Trend Analysis Core|trend_analysis_core.py]] || Per-timeframe trend direction, MA slopes || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/trend_analysis_core.py View Code]
| 2 || [[Component:Trend_Analysis_Core|trend_analysis_core.py]] || Per-timeframe trend direction, MA slopes || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/trend_analysis_core.py View]
|-
|-
| 2 || [[#Market State Analysis|market_state_analysis.py]] || Railroad, creeper, volatility, trend phase || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/market_state_analysis.py View Code]
| 3 || [[Component:Market_State_Analysis|market_state_analysis.py]] || Railroad, creeper, volatility, trend phase || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/market_state_analysis.py View]
|-
|-
| 3 || [[#Setup Quality Detection|setup_quality_detection.py]] || 5-factor scoring, A+ to F grades || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/setup_quality_detection.py View Code]
| 4 || [[Component:Setup_Quality_Detection|setup_quality_detection.py]] || 5-factor scoring, A+ to F grades || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/setup_quality_detection.py View]
|-
|-
| 4 || [[#Signal Generation|signal_generation_trade_management.py]] || Direction, filters, entry/stop/target || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/signal_generation_trade_management.py View Code]
| 5 || [[Component:Signal_Generation|signal_generation_trade_management.py]] || Direction, filters, entry/stop/target || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/signal_generation_trade_management.py View]
|-
|-
| 5 || [[#Trade Execution|trade_execution_engine.py]] || Execution, position mgmt, P&L || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/trade_execution_engine.py View Code]
| 6 || [[Component:Trade_Execution|trade_execution_engine.py]] || Execution, position management, P&L || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/trade_execution_engine.py View]
|}
 
'''Support Components:'''
{| class="wikitable" style="width:100%"
|-
! 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) || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/probability_zone_analysis.py View Code]
| 7 || [[Component:Probability_Zone|probability_zone_analysis.py]] || Probability zones (80%/65%/35%/15%), crash bar || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/probability_zone_analysis.py View]
|-
|-
| data_manager.py || Data loading, timeframe aggregation || main.py || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/data_manager.py View Code]
| 8 || [[Component:Data_Manager|data_manager.py]] || Data loading, timeframe aggregation || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/data_manager.py View]
|-
|-
| backtesting_analytics.py || Performance metrics, reporting || main.py || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/backtesting_analytics.py View Code]
| 9 || [[Component:Backtesting_Analytics|backtesting_analytics.py]] || Performance metrics, reporting || [https://github.com/stoic97/plus_ev_code_base/blob/rikk_mtf_backtest001/services/backtest/src/components/backtesting_analytics.py View]
|}
|}


Line 137: Line 114:
{| class="wikitable"
{| class="wikitable"
|-
|-
! PDF Concept !! Implementation Status !! Code Location !! Notes
! Trading Concept !! Status !! Component !! Implementation
|-
| '''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
| '''Halves/Thirds''' || ✅ || probability_zone_analysis.py || Top third = 80%, Bottom third = 15% continuation
|-
|-
| '''Color Change''' || ⚠️ Disabled || probability_zone_analysis.py || TICKET-21: Too restrictive, set enable_color_change_filter=False
| '''Crash Bar''' || || probability_zone_analysis.py || Bar 2x average = structural break
|-
|-
| '''Three-Finger Spread''' || ✅ Implemented || probability_zone_analysis.py || Price/21MA/200MA separation detection (PDF calls it large MA spread)
| '''Three-Finger Spread''' || ✅ || probability_zone_analysis.py || Price/21MA/200MA separation detection
|-
|-
| '''45-degree angle''' || ✅ Implemented || trend_analysis_core.py || MA slope analysis, "flattish" detection
| '''45-degree Angle''' || ✅ || trend_analysis_core.py || MA slope analysis, flattish detection
|-
|-
| '''Railroad Trend''' || ✅ Implemented || market_state_analysis.py || Consistency > 80%, 3+ strong bars
| '''Railroad Trend''' || ✅ || market_state_analysis.py || Consistency > 80%, 3+ strong bars
|-
|-
| '''Creeper Move''' || ✅ Implemented || market_state_analysis.py || Avg range < 0.5% over 7 bars
| '''Creeper Move''' || ✅ || 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
| '''Two-Day Trend''' || ✅ || 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
| '''Color Change''' || || probability_zone_analysis.py || Liquidity sweep pattern detection
|-
|-
| '''Traffic Jam''' || ❌ Not Implemented || - || Prior day congestion as S/R not coded
| '''Fab Four''' || ❌ || - || Not implemented (Three-Finger Spread is closest)
|-
|-
| '''Big Bar by Big Bar''' || ⚠️ Partial || trend_analysis_core.py || Slope calculation uses significant moves; no explicit "noise filter"
| '''Traffic Jam''' || || - || Not implemented
|}
|}


Line 166: Line 141:
== PART 4: Key Configuration Parameters ==
== PART 4: Key Configuration Parameters ==


=== Setup Quality Weights (setup_quality_detection.py) ===
=== Setup Quality Weights ===
<pre>
<pre>
timeframe_alignment_weight = 0.30  # 30%
timeframe_alignment_weight = 0.30  # 30%
Line 174: Line 149:
risk_reward_weight        = 0.15  # 15%
risk_reward_weight        = 0.15  # 15%
                           -------
                           -------
                          = 1.00  # Must sum to 1.0
                            1.00
</pre>
</pre>


=== Penalty Constants ===
=== Penalty & Bonus Constants ===
<pre>
<pre>
creeper_move_penalty      = -50   # Major penalty for slow grinding
creeper_move_penalty      = -50
ma_struggle_penalty        = -30   # Price repeatedly testing MA
ma_struggle_penalty        = -30
two_day_trend_penalty      = -30   # Missing 2-day directional trend
two_day_trend_penalty      = -30
phase_mismatch_penalty    = -25   # Not in MIDDLE phase
phase_mismatch_penalty    = -25
railroad_trend_bonus      = +15   # Strong one-sided move
railroad_trend_bonus      = +15
key_level_bonus            = +10
clean_entry_bonus          = +10
</pre>
</pre>


=== Grade Thresholds ===
=== Grade Thresholds & Position Sizing ===
<pre>
<pre>
A+ = score >= 90    →  2 lots
A+ = score >= 90    →  2 lots
Line 193: Line 170:
C  = score >= 60    →  1 lot
C  = score >= 60    →  1 lot
D  = score >= 50    →  1 lot
D  = score >= 50    →  1 lot
F  = score < 50    →  1 lot (signal may be rejected by filters)
F  = score < 50    →  1 lot
</pre>
</pre>


----
=== Trading Constants ===
 
<pre>
== PART 5: Component Details ==
commission_per_lot    = 20.00      # Rs per lot (Dhan)
 
transaction_tax_rate  = 0.0001    # 0.01% CTT
=== Multi-Timeframe Analysis ===
lot_size_multiplier    = 100        # MCX lot size
''See:'' [[Component:Multi_Timeframe_Analysis]]
min_stop_distance      = 40        # Points
 
default_risk_reward    = 1.5       # R:R ratio
=== Trend Analysis Core ===
</pre>
''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 ==
 
{| class="wikitable"
|-
! 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
|}


----
----
Line 251: Line 186:
[[Category:Architecture]]
[[Category:Architecture]]
[[Category:Components]]
[[Category:Components]]
[[Category:Alpha Research]]

Revision as of 18:29, 7 January 2026

Component Architecture

Branch: rikk_mtf_backtest001 | Companion to: Alpha_Research

Component-level implementation of the MTFDR backtesting system. Each component translates trading philosophy into executable Python.


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, 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 action (avg range < 0.5%)          │
│  3. Volatility Classification - High/Normal/Low                     │
│  4. Market State - Trending/Range/Creeper/Momentum                  │
│  5. Two-Day Trend - Both days close same direction                  │
│  6. Trend Phase - EARLY/MIDDLE/LATE                                 │
│  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%)                                        │
│  • Trend Strength (20%)                                             │
│  • Entry Quality (15%)                                              │
│  • Key Level Proximity (20%)                                        │
│  • Risk/Reward (15%)                                                │
│                                                                     │
│  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                                               │
│  • MA21 declining → SHORT                                           │
│  • Flat → MTF fallback                                              │
│                                                                     │
│  Gate Filters: Hour, MA Direction, Probability Zone                 │
│  Output: Signal (direction, entry, stop, target, grade)             │
└─────────────────────────────────────────────────────────────────────┘
              │
              ▼
┌─────────────────────────────────────────────────────────────────────┐
│  LAYER 5: trade_execution_engine.py                                 │
│  "Execute the trade, manage the position, calculate P&L"            │
│                                                                     │
│  Execution: Slippage modeling, Commission (Rs 20/lot), CTT (0.01%)  │
│  Position Management: Trailing stop, Breakeven stop, Time exit      │
│  Output: Trade objects with complete P&L                            │
└─────────────────────────────────────────────────────────────────────┘

PART 2: Component Index

# Component Purpose Code Link
1 multi_timeframe_analysis.py MTF alignment across 5 timeframes View
2 trend_analysis_core.py Per-timeframe trend direction, MA slopes View
3 market_state_analysis.py Railroad, creeper, volatility, trend phase View
4 setup_quality_detection.py 5-factor scoring, A+ to F grades View
5 signal_generation_trade_management.py Direction, filters, entry/stop/target View
6 trade_execution_engine.py Execution, position management, P&L View
7 probability_zone_analysis.py Probability zones (80%/65%/35%/15%), crash bar View
8 data_manager.py Data loading, timeframe aggregation View
9 backtesting_analytics.py Performance metrics, reporting View

PART 3: Trading Concept → Code Mapping

Trading Concept Status Component Implementation
Halves/Thirds probability_zone_analysis.py Top third = 80%, Bottom third = 15% continuation
Crash Bar probability_zone_analysis.py Bar 2x average = structural break
Three-Finger Spread probability_zone_analysis.py Price/21MA/200MA separation detection
45-degree Angle trend_analysis_core.py MA slope analysis, flattish detection
Railroad Trend market_state_analysis.py Consistency > 80%, 3+ strong bars
Creeper Move market_state_analysis.py Avg range < 0.5% over 7 bars
Two-Day Trend market_state_analysis.py Both daily bars close same direction
Color Change probability_zone_analysis.py Liquidity sweep pattern detection
Fab Four - Not implemented (Three-Finger Spread is closest)
Traffic Jam - Not implemented

PART 4: Key Configuration Parameters

Setup Quality Weights

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

Penalty & Bonus Constants

creeper_move_penalty       = -50
ma_struggle_penalty        = -30
two_day_trend_penalty      = -30
phase_mismatch_penalty     = -25
railroad_trend_bonus       = +15
key_level_bonus            = +10
clean_entry_bonus          = +10

Grade Thresholds & Position Sizing

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

Trading Constants

commission_per_lot     = 20.00      # Rs per lot (Dhan)
transaction_tax_rate   = 0.0001     # 0.01% CTT
lot_size_multiplier    = 100        # MCX lot size
min_stop_distance      = 40         # Points
default_risk_reward    = 1.5        # R:R ratio