Jump to content

Component:Trend Analysis Core

From PlusEV Wiki Page

Trend Analysis Core

[edit]

Purpose

[edit]

"Is this timeframe trending or flattish[low odds of follow-through, moves are short-lived]"

For each timeframe, this component determines:

  1. Trend Direction - UP, DOWN, or SIDEWAYS
  2. MA Trending Status - Is 21 MA moving with conviction (45-degree) or flat?
  3. Price-MA Struggle - Is price stuck near MA, unable to break away?

Trading Market Principle

[edit]
 "Only trade when Market shows clear directional conviction."
 - 45-degree MA slope = healthy, tradeable trend → too steep or flattish = skip
 - MA21 slope tells us conviction → rising = bullish, declining = bearish, flat = no trade
 - Price stuck near MA = indecision → wait for clean break before entry
 - Direction needs both price position AND slope confirmation → one without the other is weak

Three Core Algorithms

[edit]

Algorithm 1: Trend Direction

[edit]

Determines if timeframe is UP, DOWN, or SIDEWAYS.

Input:  close, MA21, MA200, MA21_slope
Output: "up" | "down" | "sideways" | "unknown"

Logic:
  IF close > MA21 AND MA21_slope > +0.01%:
      return "up"

  ELIF close < MA21 AND MA21_slope < -0.01%:
      return "down"

  ELSE:
      // Fallback to MA stack
      IF MA21 > MA200: return "up"
      IF MA21 < MA200: return "down"
      return "sideways"
[edit]

Checks if MA21 has significant slope (45-degree angle).

Input:  MA21 values (last 6 bars)
Output: True (trending) | False (flattish)

Formula:
  slope = (MA21_current - MA21_5_bars_ago) / MA21_5_bars_ago

  return |slope| > 0.05%

Translation to trading language:

Slope Interpretation Trading Action
> +0.05% Bullish 45-degree Look for LONG entries
< -0.05% Bearish 45-degree Look for SHORT entries
-0.05% to +0.05% Flattish/Sideways WEAKEST Low odds of follow-through, moves are short-lived

Algorithm 3: Price-MA Struggle

[edit]

Detects when price is stuck near MA (indecision).

Input:  close values, MA21 values (last 5 bars)
Output: True (struggling) | False (moving freely)

Logic:
  FOR each of last 5 bars:
      distance = |close - MA21| / MA21
      IF distance > 0.5%:
          return False  // Price broke away

  return True  // All bars within 0.5% = struggling

Trading interpretation:

  • price_ma_struggle = True → Price is "fighting" the MA, no clear direction
  • price_ma_struggle = False → Price has conviction, clean trend

Configuration

[edit]
Parameter Value Description
primary_ma_period 21 Fast moving average (21 MA)
secondary_ma_period 200 Slow moving average (200 MA)
bullish_slope_threshold 0.0001 +0.01% = bullish direction
bearish_slope_threshold -0.0001 -0.01% = bearish direction
ma_trending_min_slope 0.0005 0.05% = 45-degree threshold
price_ma_struggle_threshold 0.005 0.5% = struggle zone
min_bars_for_analysis 10 Minimum bars needed
min_bars_for_slope 6 Bars for slope calculation

Output

[edit]

TrendAnalysisResult:

Field Type Description
trend_direction str "up", "down", "sideways", "unknown"
primary_ma_slope float Actual slope value of MA21
ma_trending_status bool True = 45-degree, False = flattish
price_ma_struggle bool True = stuck near MA
trend_confidence float 0-1 confidence score
trend_strength float 0-1 strength score

Confidence Calculation

[edit]

Confidence is weighted sum of three factors:

Confidence = (slope_factor × 0.4) + (position_factor × 0.3) + (alignment_factor × 0.3)

Where:
  slope_factor    = min(|MA_slope| / 0.05%, 1.0)
  position_factor = min(|price - MA| / 0.5%, 1.0)
  alignment_factor = 1.0 if direction matches MA trending, else 0.5

How MTF Analysis Uses This

[edit]

multi_timeframe_analysis.py calls this component for each timeframe:

for timeframe in [1D, 4H, 1H, 15M, 5M]:
    result = trend_analysis_core._analyze_timeframe(data, timeframe)

    // Uses result.trend_direction for alignment check
    // Uses result.ma_trending_status for quality assessment
    // Uses result.price_ma_struggle for penalty detection

Example

[edit]

Scenario: 5-minute bar with:

  • Close = 5850
  • MA21 = 5820 (30 points below close)
  • MA21 5 bars ago = 5800
Step 1: Calculate MA slope
  slope = (5820 - 5800) / 5800 = 0.00345 = 0.345%

Step 2: Determine trend direction
  close (5850) > MA21 (5820) ✓
  slope (0.345%) > bullish_threshold (0.01%) ✓
  → trend_direction = "up"

Step 3: Check MA trending
  |slope| (0.345%) > ma_trending_min (0.05%) ✓
  → ma_trending_status = True (45-degree angle)

Step 4: Check price-MA struggle
  distance = |5850 - 5820| / 5820 = 0.52%
  0.52% > 0.5% threshold
  → price_ma_struggle = False (price broke away cleanly)

Result: Strong bullish trend with 45-degree MA, price moving with conviction.


Key Methods

[edit]

_analyze_timeframe()

[edit]

Main entry point for single timeframe analysis. Calls all three algorithms.

_determine_trend_direction()

[edit]

Algorithm 1: UP/DOWN/SIDEWAYS based on price vs MA21 and slope.

_check_ma_trending()

[edit]

Algorithm 2: Is MA21 slope > 0.05%? (45-degree detection)

_check_price_ma_struggle()

[edit]

Algorithm 3: Are all last 5 bars within 0.5% of MA21?

_calculate_ma_slope()

[edit]

Helper: Calculates (MA_current - MA_5_bars_ago) / MA_5_bars_ago


Dependencies

[edit]

Used By:

Provides Data To: