models cleanup

This commit is contained in:
Dobromir Popov
2025-05-30 03:20:05 +03:00
parent 75dbac1761
commit 2a148b0ac6
21 changed files with 937 additions and 570 deletions

View File

@ -3,17 +3,29 @@ Williams Market Structure Implementation for RL Training
This module implements Larry Williams market structure analysis methodology for
RL training enhancement with:
- Swing high/low detection with configurable strength
- 5 levels of recursive pivot point calculation
- Trend analysis (higher highs/lows vs lower highs/lows)
- Market bias determination across multiple timeframes
- Feature extraction for RL training (250 features)
**SINGLE TIMEFRAME RECURSIVE APPROACH:**
- Level 0: 1s OHLCV data → swing points using configurable strength [2, 3, 5]
- Level 1: Level 0 swing points treated as "price bars" → higher-level swing points
- Level 2: Level 1 swing points treated as "price bars" → even higher-level swing points
- Level 3: Level 2 swing points treated as "price bars" → top-level swing points
- Level 4: Level 3 swing points treated as "price bars" → highest-level swing points
**RECURSIVE METHODOLOGY:**
Each level uses the previous level's swing points as input "price data", where:
- Each swing point becomes a "price bar" with OHLC = swing point price
- Swing strength detection applied to find patterns in swing point sequences
- This creates fractal market structure analysis across 5 recursive levels
**NOT MULTI-TIMEFRAME:**
Williams structure uses ONLY 1s data and builds recursively.
Multi-timeframe data (1m, 1h) is used separately for CNN feature enhancement.
Based on Larry Williams' teachings on market structure:
- Markets move in swings between support and resistance
- Higher timeframe structure determines lower timeframe bias
- Recursive analysis reveals fractal patterns
- Trend direction determined by swing point relationships
- Higher recursive levels reveal longer-term structure patterns
- Recursive analysis reveals fractal patterns within market movements
- Trend direction determined by swing point relationships across levels
"""
import numpy as np
@ -116,7 +128,7 @@ class WilliamsMarketStructure:
enable_cnn_feature: If True, enables CNN prediction and training at pivots.
training_data_provider: Provider/stream for accessing TrainingDataPacket
"""
self.swing_strengths = swing_strengths or [2, 3, 5, 8, 13] # Fibonacci-based strengths
self.swing_strengths = swing_strengths or [2, 3, 5] # Simplified strengths for better performance
self.max_levels = 5
self.min_swings_for_trend = 3
@ -154,35 +166,47 @@ class WilliamsMarketStructure:
def calculate_recursive_pivot_points(self, ohlcv_data: np.ndarray) -> Dict[str, MarketStructureLevel]:
"""
Calculate 5 levels of recursive pivot points using TRUE recursion
Calculate 5 levels of recursive pivot points using SINGLE TIMEFRAME (1s) data
Level 1: Calculated from 1s OHLCV data
Level 2: Calculated from Level 1 pivot points treated as individual price bars
Level 3: Calculated from Level 2 pivot points treated as individual price bars
etc.
**RECURSIVE STRUCTURE:**
- Level 0: Raw 1s OHLCV data → swing points (strength 2, 3, 5)
- Level 1: Level 0 swing points treated as "price bars" → higher-level swing points
- Level 2: Level 1 swing points → treated as "price bars" → even higher-level swing points
- Level 3: Level 2 swing points → treated as "price bars" → top-level swing points
- Level 4: Level 3 swing points → treated as "price bars" → highest-level swing points
**HOW RECURSION WORKS:**
1. Start with 1s OHLCV data (timestamp, open, high, low, close, volume)
2. Find Level 0 swing points using configurable strength [2, 3, 5]
3. Convert Level 0 swing points to "price bar" format where OHLC = swing price
4. Apply swing detection to these "price bars" to find Level 1 swing points
5. Repeat process: Level N swing points → "price bars" → Level N+1 swing points
This creates a fractal analysis where each level reveals longer-term structure patterns
within the same 1s timeframe data, NOT across different timeframes.
Args:
ohlcv_data: OHLCV data array with columns [timestamp, open, high, low, close, volume]
ohlcv_data: 1s OHLCV data array [timestamp, open, high, low, close, volume]
Returns:
Dict of market structure levels with swing points and trend analysis
Dict of 5 market structure levels with recursive swing points and analysis
"""
if len(ohlcv_data) < 20:
logger.warning("Insufficient data for Williams structure analysis")
return self._create_empty_structure()
levels = {}
current_price_points = ohlcv_data.copy() # Start with raw price data
current_price_points = ohlcv_data.copy() # Start with raw 1s OHLCV data
for level in range(self.max_levels):
logger.debug(f"Analyzing level {level} with {len(current_price_points)} data points")
if level == 0:
# Level 0 (Level 1): Calculate from raw OHLCV data
# Level 0: Calculate swing points from raw 1s OHLCV data
swing_points = self._find_swing_points_multi_strength(current_price_points)
else:
# Level 1+ (Level 2+): Calculate from previous level's pivot points
# Treat pivot points as individual price bars
# Level 1-4: Calculate swing points from previous level's swing points
# Previous level's swing points are treated as "price bars"
swing_points = self._find_pivot_points_from_pivot_points(current_price_points, level)
if len(swing_points) < self.min_swings_for_trend:
@ -557,14 +581,24 @@ class WilliamsMarketStructure:
def _find_pivot_points_from_pivot_points(self, pivot_array: np.ndarray, level: int) -> List[SwingPoint]:
"""
Find pivot points from previous level's pivot points
Find swing points from previous level's swing points (RECURSIVE APPROACH)
For Level 2+: A Level N low pivot is when a Level N-1 pivot low is surrounded
by higher Level N-1 pivot lows (and vice versa for highs)
**RECURSIVE SWING DETECTION:**
For Level N (where N > 0): A Level N swing high occurs when a Level N-1 swing point
is higher than surrounding Level N-1 swing points (and vice versa for lows).
This is NOT multi-timeframe analysis - it's recursive fractal analysis where:
- Level 1 finds patterns in Level 0 swing sequences (from 1s data)
- Level 2 finds patterns in Level 1 swing sequences
- Level 3 finds patterns in Level 2 swing sequences
- Level 4 finds patterns in Level 3 swing sequences
All based on the original 1s timeframe data, recursively analyzed.
Args:
pivot_array: Array of pivot points as [timestamp, price, price, price, price, 0] format
level: Current level being calculated
pivot_array: Array of Level N-1 swing points formatted as "price bars"
[timestamp, price, price, price, price, 0] format
level: Current recursive level being calculated (1, 2, 3, or 4)
"""
identified_swings_in_this_call = [] # Temporary list
@ -620,10 +654,22 @@ class WilliamsMarketStructure:
def _convert_pivots_to_price_points(self, swing_points: List[SwingPoint]) -> np.ndarray:
"""
Convert swing points to price point array for next level calculation
Convert swing points to "price bar" format for next recursive level calculation
Each swing point becomes a "price bar" where OHLC = pivot price
This allows the next level to treat pivot points as individual price data
**RECURSIVE CONVERSION PROCESS:**
Each swing point from Level N becomes a "price bar" for Level N+1 calculation:
- Timestamp = swing point timestamp
- Open = High = Low = Close = swing point price (since it's a single point)
- Volume = 0 (not applicable for swing points)
This allows Level N+1 to treat Level N swing points as if they were regular
OHLCV price bars, enabling the same swing detection algorithm to find
higher-level patterns in the swing point sequences.
Example:
- Level 0: 1000 x 1s bars → 50 swing points
- Level 1: 50 "price bars" (from Level 0 swings) → 10 swing points
- Level 2: 10 "price bars" (from Level 1 swings) → 3 swing points
"""
if len(swing_points) < 2:
return np.array([])
@ -865,7 +911,7 @@ class WilliamsMarketStructure:
Features include:
- ETH: 5 min ticks → 300 x 1s bars with ticks features (4 features)
- ETH: 900 x 1s OHLCV + indicators (10 features)
- ETH: 900 x 1s OHLCV + indicators (10 features)
- ETH: 900 x 1m OHLCV + indicators (10 features)
- ETH: 900 x 1h OHLCV + indicators (10 features)
- ETH: All pivot points from all levels (15 features)