lightweight-charts library candlestick charts for 1s, -> 1d resolution ohlc prompt user to enter symbol
To upload files, please first save the app
import streamlit as st
import ccxt
import pandas as pd
from datetime import datetime, timedelta
st.title("Crypto Trading Charts")
# Symbol input
symbol = st.text_input("Enter trading symbol (e.g. BTC/USDT):", "BTC/USDT")
# Timeframe selection
timeframe = st.selectbox(
"Select timeframe",
["1s", "1m", "5m", "15m", "1h", "4h", "1d"],
index=1
)
# Initialize exchange
exchange = ccxt.binance()
try:
# Fetch OHLCV data
timeframe_mapping = {
"1s": "1s",
"1m": "1m",
"5m": "5m",
"15m": "15m",
"1h": "1h",
"4h": "4h",
"1d": "1d"
}
ohlcv = exchange.fetch_ohlcv(
symbol,
timeframe_mapping[timeframe],
limit=100
)
# Convert to DataFrame
df = pd.DataFrame(
ohlcv,
columns=['timestamp', 'open', 'high', 'low', 'close', 'volume']
)
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
# Prepare data for lightweight-charts
chart_data = {
"time": df['timestamp'].dt.strftime('%Y-%m-%d %H:%M:%S').tolist(),
"open": df['open'].tolist(),
"high": df['high'].tolist(),
"low": df['low'].tolist(),
"close": df['close'].tolist()
}
# Create candlestick chart using lightweight-charts
st.components.v1.html(
f"""
<div id="chart"></div>
<script src="https://unpkg.com/lightweight-charts/dist/lightweight-charts.standalone.production.js"></script>
<script>
const chart = LightweightCharts.createChart(document.getElementById('chart'), {{
width: 800,
height: 400,
layout: {{
background: {{
color: '#ffffff'
}},
textColor: '#333'
}}
}});
const candlestickSeries = chart.addCandlestickSeries();
candlestickSeries.setData({chart_data});
chart.timeScale().fitContent();
</script>
""",
height=450
)
# Display raw data in a table
if st.checkbox("Show raw data"):
st.dataframe(df)
except Exception as e:
st.error(f"Error fetching data: {str(e)}")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?