tter pivots

This commit is contained in:
Dobromir Popov
2025-05-30 03:03:51 +03:00
parent 1130e02f35
commit 75dbac1761
6 changed files with 1459 additions and 1830 deletions

View File

@ -321,6 +321,19 @@ class TradingDashboard:
logger.info("Trading Dashboard initialized with enhanced RL training integration")
logger.info(f"Enhanced RL enabled: {self.enhanced_rl_training_enabled}")
logger.info(f"Stream consumer ID: {self.stream_consumer_id}")
# Initialize Williams Market Structure once
try:
from training.williams_market_structure import WilliamsMarketStructure
self.williams_structure = WilliamsMarketStructure(
swing_strengths=[2, 3, 5], # Simplified for better performance
enable_cnn_feature=False, # Disable CNN until TensorFlow available
training_data_provider=None
)
logger.info("Williams Market Structure initialized for dashboard")
except ImportError:
self.williams_structure = None
logger.warning("Williams Market Structure not available")
def _to_local_timezone(self, dt: datetime) -> datetime:
"""Convert datetime to configured local timezone"""
@ -4532,32 +4545,49 @@ class TradingDashboard:
logger.error(f"Error stopping streaming: {e}")
def _get_williams_pivot_features(self, df: pd.DataFrame) -> Optional[List[float]]:
"""Calculate Williams Market Structure pivot points features"""
"""Get Williams Market Structure pivot features for RL training"""
try:
# Import Williams Market Structure
try:
from training.williams_market_structure import WilliamsMarketStructure
except ImportError:
# Use reused Williams instance
if not self.williams_structure:
logger.warning("Williams Market Structure not available")
return None
# Convert DataFrame to numpy array for Williams calculation
if len(df) < 50:
if len(df) < 20: # Reduced from 50 to match Williams minimum requirement
logger.debug(f"[WILLIAMS] Insufficient data for pivot calculation: {len(df)} bars (need 20+)")
return None
ohlcv_array = np.array([
[self._to_local_timezone(df.index[i]).timestamp() if hasattr(df.index[i], 'timestamp') else time.time(),
df['open'].iloc[i], df['high'].iloc[i], df['low'].iloc[i],
df['close'].iloc[i], df['volume'].iloc[i]]
for i in range(len(df))
])
try:
ohlcv_array = np.array([
[self._to_local_timezone(df.index[i]).timestamp() if hasattr(df.index[i], 'timestamp') else time.time(),
df['open'].iloc[i], df['high'].iloc[i], df['low'].iloc[i],
df['close'].iloc[i], df['volume'].iloc[i]]
for i in range(len(df))
])
logger.debug(f"[WILLIAMS] Prepared OHLCV array: {ohlcv_array.shape}, price range: {ohlcv_array[:, 4].min():.2f} - {ohlcv_array[:, 4].max():.2f}")
except Exception as e:
logger.warning(f"[WILLIAMS] Error preparing OHLCV array: {e}")
return None
# Calculate Williams pivot points
williams = WilliamsMarketStructure()
structure_levels = williams.calculate_recursive_pivot_points(ohlcv_array)
# Calculate Williams pivot points with reused instance
try:
structure_levels = self.williams_structure.calculate_recursive_pivot_points(ohlcv_array)
# Add diagnostics for debugging
total_pivots = sum(len(level.swing_points) for level in structure_levels.values())
if total_pivots == 0:
logger.debug(f"[WILLIAMS] No pivot points detected in {len(ohlcv_array)} bars")
else:
logger.debug(f"[WILLIAMS] Successfully detected {total_pivots} pivot points across {len([l for l in structure_levels.values() if len(l.swing_points) > 0])} levels")
except Exception as e:
logger.warning(f"[WILLIAMS] Error in pivot calculation: {e}")
return None
# Extract features (250 features total)
pivot_features = williams.extract_features_for_rl(structure_levels)
pivot_features = self.williams_structure.extract_features_for_rl(structure_levels)
logger.debug(f"[PIVOT] Calculated {len(pivot_features)} Williams pivot features")
return pivot_features
@ -4795,40 +4825,66 @@ class TradingDashboard:
logger.warning("Williams Market Structure not available for chart")
return None
# Need at least 50 bars for meaningful pivot calculation
if len(df) < 50:
logger.debug(f"[WILLIAMS] Insufficient data for pivot calculation: {len(df)} bars")
# Reduced requirement to match Williams minimum
if len(df) < 20:
logger.debug(f"[WILLIAMS_CHART] Insufficient data for pivot calculation: {len(df)} bars (need 20+)")
return None
# Ensure timezone consistency for the chart data
df = self._ensure_timezone_consistency(df)
# Convert DataFrame to numpy array for Williams calculation with proper timezone handling
ohlcv_array = []
for i in range(len(df)):
timestamp = df.index[i]
try:
ohlcv_array = []
for i in range(len(df)):
timestamp = df.index[i]
# Convert timestamp to local timezone and then to Unix timestamp
if hasattr(timestamp, 'timestamp'):
local_time = self._to_local_timezone(timestamp)
unix_timestamp = local_time.timestamp()
else:
unix_timestamp = time.time()
ohlcv_array.append([
unix_timestamp,
df['open'].iloc[i],
df['high'].iloc[i],
df['low'].iloc[i],
df['close'].iloc[i],
df['volume'].iloc[i]
])
# Convert timestamp to local timezone and then to Unix timestamp
if hasattr(timestamp, 'timestamp'):
local_time = self._to_local_timezone(timestamp)
unix_timestamp = local_time.timestamp()
ohlcv_array = np.array(ohlcv_array)
logger.debug(f"[WILLIAMS_CHART] Prepared OHLCV array: {ohlcv_array.shape}, price range: {ohlcv_array[:, 4].min():.2f} - {ohlcv_array[:, 4].max():.2f}")
except Exception as e:
logger.warning(f"[WILLIAMS_CHART] Error preparing OHLCV array: {e}")
return None
# Calculate Williams pivot points with proper configuration
try:
williams = WilliamsMarketStructure(
swing_strengths=[2, 3, 5], # Start with simpler strengths
enable_cnn_feature=False, # Disable CNN for chart display
training_data_provider=None # No training data provider needed for chart
)
structure_levels = williams.calculate_recursive_pivot_points(ohlcv_array)
# Add diagnostics for debugging
total_pivots_detected = sum(len(level.swing_points) for level in structure_levels.values())
if total_pivots_detected == 0:
logger.warning(f"[WILLIAMS_CHART] No pivot points detected in {len(ohlcv_array)} bars for chart display")
price_volatility = np.std(ohlcv_array[:, 4]) / np.mean(ohlcv_array[:, 4]) if np.mean(ohlcv_array[:, 4]) > 0 else 0.0
logger.debug(f"[WILLIAMS_CHART] Data diagnostics: volatility={price_volatility:.4f}, time_span={ohlcv_array[-1, 0] - ohlcv_array[0, 0]:.0f}s")
return None
else:
unix_timestamp = time.time()
ohlcv_array.append([
unix_timestamp,
df['open'].iloc[i],
df['high'].iloc[i],
df['low'].iloc[i],
df['close'].iloc[i],
df['volume'].iloc[i]
])
ohlcv_array = np.array(ohlcv_array)
# Calculate Williams pivot points
williams = WilliamsMarketStructure()
structure_levels = williams.calculate_recursive_pivot_points(ohlcv_array)
logger.debug(f"[WILLIAMS_CHART] Successfully detected {total_pivots_detected} pivot points for chart")
except Exception as e:
logger.warning(f"[WILLIAMS_CHART] Error in pivot calculation: {e}")
return None
# Extract pivot points for chart display
chart_pivots = {}
@ -4846,7 +4902,7 @@ class TradingDashboard:
# Log swing point details for validation
highs = [s for s in swing_points if s.swing_type.name == 'SWING_HIGH']
lows = [s for s in swing_points if s.swing_type.name == 'SWING_LOW']
logger.debug(f"[WILLIAMS] Level {level}: {len(highs)} highs, {len(lows)} lows, total: {len(swing_points)}")
logger.debug(f"[WILLIAMS_CHART] Level {level}: {len(highs)} highs, {len(lows)} lows, total: {len(swing_points)}")
# Convert swing points to chart format
chart_pivots[f'level_{level}'] = {
@ -4858,11 +4914,11 @@ class TradingDashboard:
}
total_pivots += len(swing_points)
logger.info(f"[WILLIAMS] Calculated {total_pivots} total pivot points across {len(chart_pivots)} levels")
logger.info(f"[WILLIAMS_CHART] Calculated {total_pivots} total pivot points across {len(chart_pivots)} levels")
return chart_pivots
except Exception as e:
logger.warning(f"Error calculating Williams pivot points for chart: {e}")
logger.warning(f"Error calculating Williams pivot points: {e}")
return None
def _add_williams_pivot_points_to_chart(self, fig, pivot_points: Dict, row: int = 1):