File size: 1,728 Bytes
b6f64d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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

# Настройка страницы Streamlit
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

# Если вы хотите добавить кнопку для остановки/запуска обновления, это можно сделать так:
# if st.button('Stop/Start'):
#     running = not running