103 lines
2.9 KiB
Python
103 lines
2.9 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
Simple test for Dash to ensure chart rendering is working correctly
|
|
"""
|
|
|
|
import dash
|
|
from dash import html, dcc, Input, Output
|
|
import plotly.graph_objects as go
|
|
import numpy as np
|
|
import pandas as pd
|
|
from datetime import datetime, timedelta
|
|
|
|
# Create some sample data
|
|
def generate_sample_data(days=10):
|
|
end_date = datetime.now()
|
|
start_date = end_date - timedelta(days=days)
|
|
dates = pd.date_range(start=start_date, end=end_date, freq='1h')
|
|
|
|
np.random.seed(42)
|
|
prices = np.random.normal(loc=100, scale=5, size=len(dates))
|
|
prices = np.cumsum(np.random.normal(loc=0, scale=1, size=len(dates))) + 100
|
|
|
|
df = pd.DataFrame({
|
|
'timestamp': dates,
|
|
'open': prices,
|
|
'high': prices + np.random.normal(loc=1, scale=0.5, size=len(dates)),
|
|
'low': prices - np.random.normal(loc=1, scale=0.5, size=len(dates)),
|
|
'close': prices + np.random.normal(loc=0, scale=0.5, size=len(dates)),
|
|
'volume': np.abs(np.random.normal(loc=100, scale=50, size=len(dates)))
|
|
})
|
|
|
|
return df
|
|
|
|
# Create a Dash app
|
|
app = dash.Dash(__name__)
|
|
|
|
# Layout
|
|
app.layout = html.Div([
|
|
html.H1("Test Chart"),
|
|
dcc.Graph(id='test-chart', style={'height': '800px'}),
|
|
dcc.Interval(
|
|
id='interval-component',
|
|
interval=1*1000, # in milliseconds
|
|
n_intervals=0
|
|
),
|
|
html.Div(id='signal-display', children="No Signal")
|
|
])
|
|
|
|
# Callback
|
|
@app.callback(
|
|
[Output('test-chart', 'figure'),
|
|
Output('signal-display', 'children')],
|
|
[Input('interval-component', 'n_intervals')]
|
|
)
|
|
def update_chart(n):
|
|
# Generate new data on each update
|
|
df = generate_sample_data()
|
|
|
|
# Create a candlestick chart
|
|
fig = go.Figure(data=[go.Candlestick(
|
|
x=df['timestamp'],
|
|
open=df['open'],
|
|
high=df['high'],
|
|
low=df['low'],
|
|
close=df['close']
|
|
)])
|
|
|
|
# Add some random buy/sell signals
|
|
if n % 5 == 0:
|
|
signal_point = df.iloc[np.random.randint(0, len(df))]
|
|
action = "BUY" if n % 10 == 0 else "SELL"
|
|
color = "green" if action == "BUY" else "red"
|
|
|
|
fig.add_trace(go.Scatter(
|
|
x=[signal_point['timestamp']],
|
|
y=[signal_point['close']],
|
|
mode='markers',
|
|
marker=dict(
|
|
size=10,
|
|
color=color,
|
|
symbol='triangle-up' if action == 'BUY' else 'triangle-down'
|
|
),
|
|
name=action
|
|
))
|
|
|
|
signal_text = f"Current Signal: {action}"
|
|
else:
|
|
signal_text = "No Signal"
|
|
|
|
# Update layout
|
|
fig.update_layout(
|
|
title='Test Price Chart',
|
|
yaxis_title='Price',
|
|
xaxis_title='Time',
|
|
template='plotly_dark',
|
|
height=800
|
|
)
|
|
|
|
return fig, signal_text
|
|
|
|
if __name__ == '__main__':
|
|
print("Starting Dash server on http://localhost:8090/")
|
|
app.run_server(debug=False, host='localhost', port=8090) |