TCPDashboard/dashboard/layouts/market_data.py

119 lines
4.7 KiB
Python
Raw Normal View History

"""
Market data layout for the dashboard.
"""
from dash import html, dcc
from utils.logger import get_logger
from components.charts import get_supported_symbols, get_supported_timeframes
from components.charts.config import get_available_strategy_names
from components.charts.indicator_manager import get_indicator_manager
from components.charts.indicator_defaults import ensure_default_indicators
from dashboard.components.chart_controls import (
create_chart_config_panel,
create_auto_update_control
)
logger = get_logger("market_data_layout")
def get_market_data_layout():
"""Create the market data visualization layout with indicator controls."""
# Get available symbols and timeframes from database
symbols = get_supported_symbols()
timeframes = get_supported_timeframes()
# Create dropdown options
symbol_options = [{'label': symbol, 'value': symbol} for symbol in symbols]
timeframe_options = [
{'label': '1 Minute', 'value': '1m'},
{'label': '5 Minutes', 'value': '5m'},
{'label': '15 Minutes', 'value': '15m'},
{'label': '1 Hour', 'value': '1h'},
{'label': '4 Hours', 'value': '4h'},
{'label': '1 Day', 'value': '1d'},
]
# Filter timeframe options to only show those available in database
available_timeframes = [tf for tf in ['1m', '5m', '15m', '1h', '4h', '1d'] if tf in timeframes]
if not available_timeframes:
available_timeframes = ['1h'] # Default fallback
timeframe_options = [opt for opt in timeframe_options if opt['value'] in available_timeframes]
# Get available strategies and indicators
try:
strategy_names = get_available_strategy_names()
strategy_options = [{'label': name.replace('_', ' ').title(), 'value': name} for name in strategy_names]
# Get user indicators from the new indicator manager
indicator_manager = get_indicator_manager()
# Ensure default indicators exist
ensure_default_indicators()
# Get indicators by display type
overlay_indicators = indicator_manager.get_indicators_by_type('overlay')
subplot_indicators = indicator_manager.get_indicators_by_type('subplot')
# Create checkbox options for overlay indicators
overlay_options = []
for indicator in overlay_indicators:
display_name = f"{indicator.name} ({indicator.type.upper()})"
overlay_options.append({'label': display_name, 'value': indicator.id})
# Create checkbox options for subplot indicators
subplot_options = []
for indicator in subplot_indicators:
display_name = f"{indicator.name} ({indicator.type.upper()})"
subplot_options.append({'label': display_name, 'value': indicator.id})
except Exception as e:
logger.warning(f"Error loading indicator options: {e}")
strategy_options = [{'label': 'Basic Chart', 'value': 'basic'}]
overlay_options = []
subplot_options = []
# Create components using the new modular functions
chart_config_panel = create_chart_config_panel(strategy_options, overlay_options, subplot_options)
auto_update_control = create_auto_update_control()
return html.Div([
# Title and basic controls
html.H3("💹 Market Data Visualization", style={'color': '#2c3e50', 'margin-bottom': '20px'}),
# Main chart controls
html.Div([
html.Div([
html.Label("Symbol:", style={'font-weight': 'bold'}),
dcc.Dropdown(
id='symbol-dropdown',
options=symbol_options,
value=symbols[0] if symbols else 'BTC-USDT',
clearable=False,
style={'margin-bottom': '10px'}
)
], style={'width': '48%', 'display': 'inline-block'}),
html.Div([
html.Label("Timeframe:", style={'font-weight': 'bold'}),
dcc.Dropdown(
id='timeframe-dropdown',
options=timeframe_options,
value='1h',
clearable=False,
style={'margin-bottom': '10px'}
)
], style={'width': '48%', 'float': 'right', 'display': 'inline-block'})
], style={'margin-bottom': '20px'}),
# Chart Configuration Panel
chart_config_panel,
# Auto-update control
auto_update_control,
# Chart
dcc.Graph(id='price-chart'),
# Market statistics
html.Div(id='market-stats', style={'margin-top': '20px'})
])