2025-06-12 13:27:30 +08:00

156 lines
5.9 KiB
Python

"""
Chart and Visualization Components - Redirect to New System
This module redirects to the new modular chart system in components/charts/.
For new development, use the ChartBuilder class directly from components.charts.
"""
# Import and re-export the new modular chart system for simple migration
from .charts import (
ChartBuilder,
create_candlestick_chart,
create_strategy_chart,
validate_market_data,
prepare_chart_data,
get_indicator_colors
)
from .charts.config import (
get_available_indicators,
calculate_indicators,
get_overlay_indicators,
get_subplot_indicators,
get_indicator_display_config
)
# Convenience functions for common operations
def get_supported_symbols():
"""Get list of symbols that have data in the database."""
builder = ChartBuilder()
candles = builder.fetch_market_data("BTC-USDT", "1m", days_back=1) # Test query
if candles:
from database.operations import get_database_operations
from utils.logger import get_logger
logger = get_logger()
try:
db = get_database_operations(logger)
with db.market_data.get_session() as session:
from sqlalchemy import text
result = session.execute(text("SELECT DISTINCT symbol FROM market_data ORDER BY symbol"))
return [row[0] for row in result]
except Exception:
pass
return ['BTC-USDT', 'ETH-USDT'] # Fallback
def get_supported_timeframes():
"""Get list of timeframes that have data in the database."""
builder = ChartBuilder()
candles = builder.fetch_market_data("BTC-USDT", "1m", days_back=1) # Test query
if candles:
from database.operations import get_database_operations
from utils.logger import get_logger
logger = get_logger()
try:
db = get_database_operations(logger)
with db.market_data.get_session() as session:
from sqlalchemy import text
result = session.execute(text("SELECT DISTINCT timeframe FROM market_data ORDER BY timeframe"))
return [row[0] for row in result]
except Exception:
pass
return ['5s', '1m', '15m', '1h'] # Fallback
# Legacy function names for compatibility during transition
get_available_technical_indicators = get_available_indicators
fetch_market_data = lambda symbol, timeframe, days_back=7, exchange="okx": ChartBuilder().fetch_market_data(symbol, timeframe, days_back, exchange)
create_candlestick_with_volume = lambda df, symbol, timeframe: create_candlestick_chart(symbol, timeframe)
create_empty_chart = lambda message="No data available": ChartBuilder()._create_empty_chart(message)
create_error_chart = lambda error_message: ChartBuilder()._create_error_chart(error_message)
def get_market_statistics(symbol: str, timeframe: str = "1h"):
"""Calculate market statistics from recent data."""
builder = ChartBuilder()
candles = builder.fetch_market_data(symbol, timeframe, days_back=1)
if not candles:
return {'Price': 'N/A', '24h Change': 'N/A', '24h Volume': 'N/A', 'High 24h': 'N/A', 'Low 24h': 'N/A'}
import pandas as pd
df = pd.DataFrame(candles)
latest = df.iloc[-1]
current_price = float(latest['close'])
# Calculate 24h change
if len(df) > 1:
price_24h_ago = float(df.iloc[0]['open'])
change_percent = ((current_price - price_24h_ago) / price_24h_ago) * 100
else:
change_percent = 0
from .charts.utils import format_price, format_volume
return {
'Price': format_price(current_price, decimals=2),
'24h Change': f"{'+' if change_percent >= 0 else ''}{change_percent:.2f}%",
'24h Volume': format_volume(df['volume'].sum()),
'High 24h': format_price(df['high'].max(), decimals=2),
'Low 24h': format_price(df['low'].min(), decimals=2)
}
def check_data_availability(symbol: str, timeframe: str):
"""Check data availability for a symbol and timeframe."""
from datetime import datetime, timezone, timedelta
from database.operations import get_database_operations
from utils.logger import get_logger
try:
logger = get_logger()
db = get_database_operations(logger)
latest_candle = db.market_data.get_latest_candle(symbol, timeframe)
if latest_candle:
latest_time = latest_candle['timestamp']
time_diff = datetime.now(timezone.utc) - latest_time.replace(tzinfo=timezone.utc)
return {
'has_data': True,
'latest_timestamp': latest_time,
'time_since_last': time_diff,
'is_recent': time_diff < timedelta(hours=1),
'message': f"Latest data: {latest_time.strftime('%Y-%m-%d %H:%M:%S UTC')}"
}
else:
return {
'has_data': False,
'latest_timestamp': None,
'time_since_last': None,
'is_recent': False,
'message': f"No data available for {symbol} {timeframe}"
}
except Exception as e:
return {
'has_data': False,
'latest_timestamp': None,
'time_since_last': None,
'is_recent': False,
'message': f"Error checking data: {str(e)}"
}
def create_data_status_indicator(symbol: str, timeframe: str):
"""Create a data status indicator for the dashboard."""
status = check_data_availability(symbol, timeframe)
if status['has_data']:
if status['is_recent']:
icon, color, status_text = "🟢", "#27ae60", "Real-time Data"
else:
icon, color, status_text = "🟡", "#f39c12", "Delayed Data"
else:
icon, color, status_text = "🔴", "#e74c3c", "No Data"
return f'<span style="color: {color}; font-weight: bold;">{icon} {status_text}</span><br><small>{status["message"]}</small>'