use common williams market structure calcs

This commit is contained in:
Dobromir Popov
2025-10-24 12:30:17 +03:00
parent bd95ff610c
commit 42bf91b735
3 changed files with 260 additions and 162 deletions

View File

@@ -65,18 +65,22 @@ class WilliamsMarketStructure:
logger.info(f"Williams Market Structure initialized with {self.max_levels} levels")
def calculate_recursive_pivot_points(self, ohlcv_data: np.ndarray) -> Dict[int, TrendLevel]:
def calculate_recursive_pivot_points(self, ohlcv_data: np.ndarray, min_pivot_distance: int = None) -> Dict[int, TrendLevel]:
"""
Calculate recursive pivot points following Williams Market Structure methodology
Args:
ohlcv_data: OHLCV data array with shape (N, 6) [timestamp, O, H, L, C, V]
min_pivot_distance: Override the instance's min_pivot_distance for this calculation (default: None uses instance value)
Returns:
Dictionary of trend levels with pivot points
"""
try:
if len(ohlcv_data) < self.min_pivot_distance * 2 + 1:
# Use provided min_pivot_distance or fall back to instance default
pivot_distance = min_pivot_distance if min_pivot_distance is not None else self.min_pivot_distance
if len(ohlcv_data) < pivot_distance * 2 + 1:
logger.warning(f"Insufficient data for pivot calculation: {len(ohlcv_data)} bars")
return {}
@@ -87,6 +91,10 @@ class WilliamsMarketStructure:
# Initialize pivot levels
self.pivot_levels = {}
# Temporarily set the pivot distance for this calculation
original_distance = self.min_pivot_distance
self.min_pivot_distance = pivot_distance
# Level 1: Calculate pivot points from raw OHLCV data
level_1_pivots = self._calculate_level_1_pivots(df)
if level_1_pivots:
@@ -110,6 +118,9 @@ class WilliamsMarketStructure:
else:
break # No more higher level pivots possible
# Restore original pivot distance
self.min_pivot_distance = original_distance
logger.debug(f"Calculated {len(self.pivot_levels)} pivot levels")
return self.pivot_levels