Jump to content

Component:Probability Zone: Difference between revisions

From PlusEV Wiki Page
No edit summary
No edit summary
Line 1: Line 1:
= Probability Zone Analysis =
  ---
== Purpose ==
  = Component: Probability Zone Analysis =


'''"Where is price in the range & what are the odds of continuation?"'''
  '''File:''' <code>probability_zone_analysis.py</code><br/>
  '''Type:''' Support Component<br/>
  '''Lines:''' 947<br/>
  '''Branch:''' <code>task/T26_probability_zone</code><br/>
  '''Code:''' [https://github.com/stoic97/plus_ev_code_base/blob/task/T26_probability_zone/services/backtest/src/components/probability_zone_analysis.py View Source]


# Divides price range into halves and thirds
  ----
# Calculates continuation probability based on zone
# Detects Three-Finger Spread (profit-taking pressure)
# Identifies crash bars (structural breaks)
# Filters signals based on zone probability


----
  == Purpose ==


== Trading Market Principle ==
  Answers the question: '''"Where is price in the range, and what are the odds of continuation?"'''


'''"Trade with the odds. Top third after pullback = 80% continuation. Bottom third = 15%."'''
  This component implements the probability zone concepts from trade_philosophy.pdf:
  # Divides price range into halves and thirds
  # Calculates continuation probability based on zone
  # Detects Three-Finger Spread (profit-taking pressure)
  # Identifies crash bars (structural breaks)
  # Filters signals based on zone probability


* Price in top third = high probability of new high (80%) → favor LONG
  ----
* Price in bottom third = low probability of new high (15%) → avoid LONG
* Three-finger spread = profit-taking imminent → avoid new positions
* Crash bar = structural break → short the bounce
* Sideways market = mean reversion → opposite logic applies


----
  == Trading Market Principle ==


== Probability Zones ==
  '''"Trade with the odds. Top third after pullback = 80% continuation. Bottom third = 15%."'''


Divide recent swing range into zones:
  * Price in top third = high probability of new high (80%) → favor LONG
  * Price in bottom third = low probability of new high (15%) → avoid LONG
  * Three-finger spread = profit-taking imminent → avoid new positions
  * Crash bar = structural break → short the bounce
  * Sideways market = mean reversion → opposite logic applies


<pre>
  ----
    ┌───────────────────────────────────────┐ ← Swing High
    │                                      │
    │        TOP THIRD (80%)              │ ← 80% continuation probability
    │    (Above 66.7% of range)            │
    ├───────────────────────────────────────┤
    │                                      │
    │        TOP HALF (65%)                │ ← 65% continuation probability
    │    (50% - 66.7% of range)            │
    ├───────────────────────────────────────┤ ← 50% Line
    │                                      │
    │        BOTTOM HALF (35%)            │ ← 35% continuation probability
    │    (33.3% - 50% of range)            │
    ├───────────────────────────────────────┤
    │                                      │
    │        BOTTOM THIRD (15%)            │ ← Only 15% continuation (85% DOWN)
    │    (Below 33.3% of range)            │
    │                                      │
    └───────────────────────────────────────┘ ← Swing Low
</pre>


{| class="wikitable"
  == Probability Zones ==
|-
! Zone !! Range Position !! Continuation Prob !! Trading Implication
|-
| Top Third || ≥ 66.7% || 80% || Strong LONG zone after pullback
|-
| Top Half || 50% - 66.7% || 65% || OK for LONG with confirmation
|-
| Bottom Half || 33.3% - 50% || 35% || Cautious LONG, watch for confirmation
|-
| Bottom Third || < 33.3% || 15% || Avoid LONG (85% chance of lower)
|}


----
  Divide recent swing range into zones:


== Three-Finger Spread ==
  <pre>
      ┌───────────────────────────────────────┐ ← Swing High
      │                                      │
      │        TOP THIRD (80%)              │ ← 80% continuation probability
      │    (Above 66.7% of range)            │
      ├───────────────────────────────────────┤
      │                                      │
      │        TOP HALF (65%)                │ ← 65% continuation probability
      │    (50% - 66.7% of range)            │
      ├───────────────────────────────────────┤ ← 50% Line
      │                                      │
      │        BOTTOM HALF (35%)            │ ← 35% continuation probability
      │    (33.3% - 50% of range)            │
      ├───────────────────────────────────────┤
      │                                      │
      │        BOTTOM THIRD (15%)            │ ← Only 15% continuation (85% DOWN)
      │    (Below 33.3% of range)            │
      │                                      │
      └───────────────────────────────────────┘ ← Swing Low
  </pre>


Detects large separation between Price, 21 MA, and 200 MA:
  {| class="wikitable"
  |-
  ! Zone !! Range Position !! Continuation Prob !! Trading Implication
  |-
  | Top Third || ≥ 66.7% || 80% || Strong LONG zone after pullback
  |-
  | Top Half || 50% - 66.7% || 65% || OK for LONG with confirmation
  |-
  | Bottom Half || 33.3% - 50% || 35% || Cautious LONG, watch for confirmation
  |-
  | Bottom Third || < 33.3% || 15% || Avoid LONG (85% chance of lower)
  |}


<pre>
  ----
BULLISH THREE-FINGER SPREAD:
    Price >> 21 MA >> 200 MA (large gaps)


    Interpretation:
  == Three-Finger Spread ==
    - Many traders with unrealized profits
    - Any red bar can trigger mass profit-taking
    - Creates fast, violent declines
    - AVOID LONGS in this condition


BEARISH THREE-FINGER SPREAD:
  Detects large separation between Price, 21 MA, and 200 MA:
    Price << 21 MA << 200 MA (large gaps)


    Interpretation:
  <pre>
    - Many traders with unrealized losses
  BULLISH THREE-FINGER SPREAD:
    - Potential for short squeeze
      Price >> 21 MA >> 200 MA (large gaps)
    - AVOID SHORTS in this condition
</pre>


'''Detection Threshold:''' 2% minimum spread between each level
      Interpretation:
      - Many traders with unrealized profits
      - Any red bar can trigger mass profit-taking
      - Creates fast, violent declines
      - AVOID LONGS in this condition


----
  BEARISH THREE-FINGER SPREAD:
      Price << 21 MA << 200 MA (large gaps)


== Crash Bar Detection ==
      Interpretation:
      - Many traders with unrealized losses
      - Potential for short squeeze
      - AVOID SHORTS in this condition
  </pre>


Identifies structural breaks (crash bars):
  '''Detection Threshold:''' 2% minimum spread between each level


<pre>
  ----
Crash Bar Criteria:
    1. Range ≥ 2× average range of prior 10 bars
    2. Close near extreme (within 30% of high/low)
    3. Fast, violent, elongated movement


IF crash bar detected:
  == Crash Bar Detection ==
    - Block LONG signals
    - Allow SHORT signals with 1.5x strength
    - Require color change confirmation for entry


Post-Crash (within 5 bars):
  Identifies structural breaks (crash bars):
    - Bounce to top zone = prime SHORT entry
    - Block LONG signals
</pre>


----
  <pre>
  Crash Bar Criteria:
      1. Range ≥ 2× average range of prior 10 bars
      2. Close near extreme (within 30% of high/low)
      3. Fast, violent, elongated movement


== Pullback Classification ==
  IF crash bar detected:
      - Block LONG signals
      - Allow SHORT signals with 1.5x strength


Classifies pullbacks as healthy or collapse:
  Post-Crash (within 5 bars):
      - Bounce to top zone = prime SHORT entry
      - Block LONG signals
  </pre>


{| class="wikitable"
  ----
|-
! Type !! Criteria !! Trading Action
|-
| HEALTHY_PULLBACK || ≤ 50% retracement, 45-degree drift || Good entry opportunity
|-
| COLLAPSE || ≥ 60% retracement, vertical drop || Avoid buying the dip
|-
| BOUNCE || Between healthy and collapse || Short opportunity in downtrend
|-
| NO_PULLBACK || < 10% retracement || Price at highs, wait for pullback
|}


----
  == Pullback Classification ==


== Signal Filtering Logic ==
  Classifies pullbacks as healthy or collapse:


=== Trending Market ===
  {| class="wikitable"
  |-
  ! Type !! Criteria !! Trading Action
  |-
  | HEALTHY_PULLBACK || ≤ 50% retracement, 45-degree drift || Good entry opportunity
  |-
  | COLLAPSE || ≥ 60% retracement, vertical drop || Avoid buying the dip
  |-
  | BOUNCE || Between healthy and collapse || Short opportunity in downtrend
  |-
  | NO_PULLBACK || < 10% retracement || Price at highs, wait for pullback
  |}


<pre>
   ----
UPTREND:
    Top Third:      LONG ✓ (80% odds), SHORT ✗
    Top Half:      LONG ✓ (65% odds), SHORT ✗
    Bottom Half:    LONG ⚠ (35% odds, reduced strength), SHORT ✗
    Bottom Third:   LONG ✗ (15% odds), SHORT ✓ (trend break signal)


DOWNTREND:
   == Signal Filtering Logic ==
    Bottom Third:   SHORT ✓, LONG ✗
    Top Third:      SHORT ✓ (bounce entry), LONG ✗
</pre>


=== Sideways/Creeper Market ===
  === Trending Market ===


Mean reversion logic (opposite of trending):
  <pre>
  UPTREND:
      Top Third:      LONG ✓ (80% odds), SHORT ✗
      Top Half:      LONG ✓ (65% odds), SHORT ✗
      Bottom Half:    LONG ⚠ (35% odds, reduced strength), SHORT ✗
      Bottom Third:   LONG ✗ (15% odds), SHORT ✓ (trend break signal)


<pre>
  DOWNTREND:
SIDEWAYS:
      Bottom Third:   SHORT ✓, LONG
    Top Third:     LONG needs confirmation, SHORT ✓
      Top Third:     SHORT ✓ (bounce entry), LONG
    Top Half:      LONG needs confirmation, SHORT ✓
  </pre>
    Bottom Half:    LONG needs confirmation, SHORT ✓
    Bottom Third:   LONG ✓ (85% reversal up), SHORT
</pre>


----
  === Sideways/Creeper Market ===


== Configuration ==
  Mean reversion logic (opposite of trending):


{| class="wikitable"
  <pre>
|-
  SIDEWAYS:
! Parameter !! Value !! Description
      Top Third:      LONG needs confirmation, SHORT ✓
|-
      Top Half:      LONG needs confirmation, SHORT ✓
| <code>top_third_threshold</code> || 0.667 || Above 66.7% = top third
      Bottom Half:    LONG needs confirmation, SHORT ✓
|-
      Bottom Third:  LONG ✓ (85% reversal up), SHORT ✗
| <code>bottom_third_threshold</code> || 0.333 || Below 33.3% = bottom third
  </pre>
|-
| <code>top_third_probability</code> || 0.80 || 80% continuation
|-
| <code>bottom_third_probability</code> || 0.15 || 15% continuation
|-
| <code>swing_lookback_bars</code> || 20 || Bars for swing detection
|-
| <code>three_finger_min_spread_pct</code> || 0.02 || 2% minimum spread
|-
| <code>crash_bar_multiplier</code> || 2.0 || Bar 2x average = crash
|-
| <code>crash_bar_close_threshold</code> || 0.30 || Close within 30% of extreme
|-
| <code>healthy_pullback_max_depth</code> || 0.50 || Max 50% for healthy
|-
| <code>collapse_min_depth</code> || 0.60 || Min 60% for collapse
|-
| <code>color_change_ma_proximity</code> || 15.0 || Points to consider "near" MA
|}


----
  ----


== Output ==
  == Configuration ==


'''ProbabilityZoneResult:'''
  {| class="wikitable"
{| class="wikitable"
  |-
|-
  ! Parameter !! Value !! Description
! Field !! Type !! Description
  |-
|-
  | <code>top_third_threshold</code> || 0.667 || Above 66.7% = top third
| <code>zone</code> || Enum || TOP_THIRD, TOP_HALF, BOTTOM_HALF, BOTTOM_THIRD
  |-
|-
  | <code>bottom_third_threshold</code> || 0.333 || Below 33.3% = bottom third
| <code>zone_position_pct</code> || float || 0-100, where in range
  |-
|-
  | <code>top_third_probability</code> || 0.80 || 80% continuation
| <code>continuation_probability</code> || float || 0.15 - 0.80
  |-
|-
  | <code>bottom_third_probability</code> || 0.15 || 15% continuation
| <code>swing_high</code> || float || Recent swing high
  |-
|-
  | <code>swing_lookback_bars</code> || 20 || Bars for swing detection
| <code>swing_low</code> || float || Recent swing low
  |-
|-
  | <code>three_finger_min_spread_pct</code> || 0.02 || 2% minimum spread
| <code>has_three_finger_spread</code> || bool || Large MA spread detected
  |-
|-
  | <code>crash_bar_multiplier</code> || 2.0 || Bar 2x average = crash
| <code>ma_structure</code> || Enum || UPTREND, DOWNTREND, SIDEWAYS, THREE_FINGER_*
  |-
|-
  | <code>crash_bar_close_threshold</code> || 0.30 || Close within 30% of extreme
| <code>pullback_type</code> || Enum || HEALTHY, COLLAPSE, BOUNCE, NO_PULLBACK
  |-
|-
  | <code>healthy_pullback_max_depth</code> || 0.50 || Max 50% for healthy
| <code>is_crash_bar</code> || bool || Crash bar detected
  |-
|-
  | <code>collapse_min_depth</code> || 0.60 || Min 60% for collapse
| <code>crash_bar_magnitude</code> || float || How many times larger than average
  |}
|-
| <code>allows_long</code> || bool || Should LONG be allowed?
|-
| <code>allows_short</code> || bool || Should SHORT be allowed?
|-
| <code>signal_strength_modifier</code> || float || 0.5 - 1.5 multiplier
|-
| <code>requires_color_change</code> || bool || Needs color change confirmation
|}


----
  ----


== Dependencies ==
  == Output ==


'''Used By:'''
  '''ProbabilityZoneResult:'''
* [[Component:Signal_Generation|signal_generation_trade_management.py]] - Gate 4: Probability Zone Filter
  {| class="wikitable"
  |-
  ! Field !! Type !! Description
  |-
  | <code>zone</code> || Enum || TOP_THIRD, TOP_HALF, BOTTOM_HALF, BOTTOM_THIRD
  |-
  | <code>zone_position_pct</code> || float || 0-100, where in range
  |-
  | <code>continuation_probability</code> || float || 0.15 - 0.80
  |-
  | <code>swing_high</code> || float || Recent swing high
  |-
  | <code>swing_low</code> || float || Recent swing low
  |-
  | <code>has_three_finger_spread</code> || bool || Large MA spread detected
  |-
  | <code>ma_structure</code> || Enum || UPTREND, DOWNTREND, SIDEWAYS, THREE_FINGER_*
  |-
  | <code>pullback_type</code> || Enum || HEALTHY, COLLAPSE, BOUNCE, NO_PULLBACK
  |-
  | <code>is_crash_bar</code> || bool || Crash bar detected
  |-
  | <code>crash_bar_magnitude</code> || float || How many times larger than average
  |-
  | <code>allows_long</code> || bool || Should LONG be allowed?
  |-
  | <code>allows_short</code> || bool || Should SHORT be allowed?
  |-
  | <code>signal_strength_modifier</code> || float || 0.5 - 1.5 multiplier
  |}


'''Provides:'''
  ----
* Zone classification for signal filtering
* Continuation probability for strength adjustment
* Color change detection for entry confirmation


----
  == Dependencies ==


== Quick Reference ==
  '''Used By:'''
  * [[Component:Signal_Generation|signal_generation_trade_management.py]] - Gate 4: Probability Zone Filter


{| class="wikitable"
  '''Provides:'''
|-
  * Zone classification for signal filtering
! Condition !! LONG !! SHORT !! Reason
  * Continuation probability for strength adjustment
|-
 
| Top third, uptrend || ✓ Strong || ✗ Block || 80% continuation
  ----
|-
 
| Bottom third, uptrend || ✗ Block || ✓ Allow || 85% down probability
  == Quick Reference ==
|-
 
| Three-finger spread (bull) || ✗ Block || ✓ Allow || Profit-taking imminent
  {| class="wikitable"
|-
  |-
| Three-finger spread (bear) || ✓ Allow || ✗ Block || Potential reversal
  ! Condition !! LONG !! SHORT !! Reason
|-
  |-
| Crash bar detected || ✗ Block || ✓ Strong || Structural break
  | Top third, uptrend || ✓ Strong || ✗ Block || 80% continuation
|-
  |-
| Post-crash bounce to top || ✗ Block || ✓ Strong || Bounce short opportunity
  | Bottom third, uptrend || ✗ Block || ✓ Allow || 85% down probability
|-
  |-
| Collapse detected || ✗ Block || ✓ Allow || Not healthy pullback
  | Three-finger spread (bull) || ✗ Block || ✓ Allow || Profit-taking imminent
|-
  |-
| Sideways, top third || Needs confirm || ✓ Allow || Mean reversion
  | Three-finger spread (bear) || ✓ Allow || ✗ Block || Potential reversal
|-
  |-
| Sideways, bottom third || ✓ Strong || ✗ Block || 85% reversal up
  | Crash bar detected || ✗ Block || ✓ Strong || Structural break
|}
  |-
  | Post-crash bounce to top || ✗ Block || ✓ Strong || Bounce short opportunity
  |-
  | Collapse detected || ✗ Block || ✓ Allow || Not healthy pullback
  |-
  | Sideways, top third || Needs confirm || ✓ Allow || Mean reversion
  |-
  | Sideways, bottom third || ✓ Strong || ✗ Block || 85% reversal up
  |}
 
  ----

Revision as of 00:10, 10 January 2026

 ---
 = Component: Probability Zone Analysis =
 File: probability_zone_analysis.py
Type: Support Component
Lines: 947
Branch: task/T26_probability_zone
Code: View Source
 ----
 == Purpose ==
 Answers the question: "Where is price in the range, and what are the odds of continuation?"
 This component implements the probability zone concepts from trade_philosophy.pdf:
 # Divides price range into halves and thirds
 # Calculates continuation probability based on zone
 # Detects Three-Finger Spread (profit-taking pressure)
 # Identifies crash bars (structural breaks)
 # Filters signals based on zone probability
 ----
 == Trading Market Principle ==
 "Trade with the odds. Top third after pullback = 80% continuation. Bottom third = 15%."
 * Price in top third = high probability of new high (80%) → favor LONG
 * Price in bottom third = low probability of new high (15%) → avoid LONG
 * Three-finger spread = profit-taking imminent → avoid new positions
 * Crash bar = structural break → short the bounce
 * Sideways market = mean reversion → opposite logic applies
 ----
 == Probability Zones ==
 Divide recent swing range into zones:
      ┌───────────────────────────────────────┐ ← Swing High
      │                                       │
      │         TOP THIRD (80%)               │ ← 80% continuation probability
      │     (Above 66.7% of range)            │
      ├───────────────────────────────────────┤
      │                                       │
      │         TOP HALF (65%)                │ ← 65% continuation probability
      │     (50% - 66.7% of range)            │
      ├───────────────────────────────────────┤ ← 50% Line
      │                                       │
      │         BOTTOM HALF (35%)             │ ← 35% continuation probability
      │     (33.3% - 50% of range)            │
      ├───────────────────────────────────────┤
      │                                       │
      │         BOTTOM THIRD (15%)            │ ← Only 15% continuation (85% DOWN)
      │     (Below 33.3% of range)            │
      │                                       │
      └───────────────────────────────────────┘ ← Swing Low
  
Zone Range Position Continuation Prob Trading Implication
Top Third ≥ 66.7% 80% Strong LONG zone after pullback
Top Half 50% - 66.7% 65% OK for LONG with confirmation
Bottom Half 33.3% - 50% 35% Cautious LONG, watch for confirmation
Bottom Third < 33.3% 15% Avoid LONG (85% chance of lower)
 ----
 == Three-Finger Spread ==
 Detects large separation between Price, 21 MA, and 200 MA:
  BULLISH THREE-FINGER SPREAD:
      Price >> 21 MA >> 200 MA (large gaps)

      Interpretation:
      - Many traders with unrealized profits
      - Any red bar can trigger mass profit-taking
      - Creates fast, violent declines
      - AVOID LONGS in this condition

  BEARISH THREE-FINGER SPREAD:
      Price << 21 MA << 200 MA (large gaps)

      Interpretation:
      - Many traders with unrealized losses
      - Potential for short squeeze
      - AVOID SHORTS in this condition
  
 Detection Threshold: 2% minimum spread between each level
 ----
 == Crash Bar Detection ==
 Identifies structural breaks (crash bars):
  Crash Bar Criteria:
      1. Range ≥ 2× average range of prior 10 bars
      2. Close near extreme (within 30% of high/low)
      3. Fast, violent, elongated movement

  IF crash bar detected:
      - Block LONG signals
      - Allow SHORT signals with 1.5x strength

  Post-Crash (within 5 bars):
      - Bounce to top zone = prime SHORT entry
      - Block LONG signals
  
 ----
 == Pullback Classification ==
 Classifies pullbacks as healthy or collapse:
Type Criteria Trading Action
HEALTHY_PULLBACK ≤ 50% retracement, 45-degree drift Good entry opportunity
COLLAPSE ≥ 60% retracement, vertical drop Avoid buying the dip
BOUNCE Between healthy and collapse Short opportunity in downtrend
NO_PULLBACK < 10% retracement Price at highs, wait for pullback
 ----
 == Signal Filtering Logic ==
 === Trending Market ===
  UPTREND:
      Top Third:      LONG ✓ (80% odds), SHORT ✗
      Top Half:       LONG ✓ (65% odds), SHORT ✗
      Bottom Half:    LONG ⚠ (35% odds, reduced strength), SHORT ✗
      Bottom Third:   LONG ✗ (15% odds), SHORT ✓ (trend break signal)

  DOWNTREND:
      Bottom Third:   SHORT ✓, LONG ✗
      Top Third:      SHORT ✓ (bounce entry), LONG ✗
  
 === Sideways/Creeper Market ===
 Mean reversion logic (opposite of trending):
  SIDEWAYS:
      Top Third:      LONG needs confirmation, SHORT ✓
      Top Half:       LONG needs confirmation, SHORT ✓
      Bottom Half:    LONG needs confirmation, SHORT ✓
      Bottom Third:   LONG ✓ (85% reversal up), SHORT ✗
  
 ----
 == Configuration ==
Parameter Value Description
top_third_threshold 0.667 Above 66.7% = top third
bottom_third_threshold 0.333 Below 33.3% = bottom third
top_third_probability 0.80 80% continuation
bottom_third_probability 0.15 15% continuation
swing_lookback_bars 20 Bars for swing detection
three_finger_min_spread_pct 0.02 2% minimum spread
crash_bar_multiplier 2.0 Bar 2x average = crash
crash_bar_close_threshold 0.30 Close within 30% of extreme
healthy_pullback_max_depth 0.50 Max 50% for healthy
collapse_min_depth 0.60 Min 60% for collapse
 ----
 == Output ==
 ProbabilityZoneResult:
Field Type Description
zone Enum TOP_THIRD, TOP_HALF, BOTTOM_HALF, BOTTOM_THIRD
zone_position_pct float 0-100, where in range
continuation_probability float 0.15 - 0.80
swing_high float Recent swing high
swing_low float Recent swing low
has_three_finger_spread bool Large MA spread detected
ma_structure Enum UPTREND, DOWNTREND, SIDEWAYS, THREE_FINGER_*
pullback_type Enum HEALTHY, COLLAPSE, BOUNCE, NO_PULLBACK
is_crash_bar bool Crash bar detected
crash_bar_magnitude float How many times larger than average
allows_long bool Should LONG be allowed?
allows_short bool Should SHORT be allowed?
signal_strength_modifier float 0.5 - 1.5 multiplier
 ----
 == Dependencies ==
 Used By:
 * signal_generation_trade_management.py - Gate 4: Probability Zone Filter
 Provides:
 * Zone classification for signal filtering
 * Continuation probability for strength adjustment
 ----
 == Quick Reference ==
Condition LONG SHORT Reason
Top third, uptrend ✓ Strong ✗ Block 80% continuation
Bottom third, uptrend ✗ Block ✓ Allow 85% down probability
Three-finger spread (bull) ✗ Block ✓ Allow Profit-taking imminent
Three-finger spread (bear) ✓ Allow ✗ Block Potential reversal
Crash bar detected ✗ Block ✓ Strong Structural break
Post-crash bounce to top ✗ Block ✓ Strong Bounce short opportunity
Collapse detected ✗ Block ✓ Allow Not healthy pullback
Sideways, top third Needs confirm ✓ Allow Mean reversion
Sideways, bottom third ✓ Strong ✗ Block 85% reversal up
 ----