|
import streamlit as st |
|
import ccxt |
|
import pandas as pd |
|
import plotly.graph_objects as go |
|
from datetime import datetime, timedelta |
|
import time |
|
|
|
|
|
exchange = ccxt.binance() |
|
|
|
def get_realtime_data(symbol='BTC/USDT', timeframe='1m'): |
|
candles = exchange.fetch_ohlcv(symbol, timeframe) |
|
df = pd.DataFrame(candles, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume']) |
|
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms') |
|
return df |
|
|
|
def update_chart(): |
|
data = get_realtime_data() |
|
fig = go.Figure(data=[go.Candlestick(x=data['timestamp'], |
|
open=data['open'], |
|
high=data['high'], |
|
low=data['low'], |
|
close=data['close'])]) |
|
fig.update_layout(title='BTC/USDT Real-Time Candlestick Chart', yaxis_title='Price') |
|
return fig |
|
|
|
|
|
st.set_page_config(page_title="Crypto Real-Time Chart", page_icon=":chart_with_upwards_trend:") |
|
|
|
st.title('Crypto Real-Time Candlestick Chart') |
|
|
|
|
|
chart_placeholder = st.empty() |
|
|
|
|
|
running = True |
|
|
|
while running: |
|
try: |
|
chart = update_chart() |
|
chart_placeholder.plotly_chart(chart) |
|
time.sleep(60) |
|
except Exception as e: |
|
st.error(f"An error occurred: {e}") |
|
running = False |
|
|
|
|
|
|
|
|