Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445import streamlit as stimport ccxtimport pandas as pdimport datetime
# Create the exchange objectexchange = ccxt.coinbase()
# Define the time intervaltimeframe = '4h'
def get_data(symbol): # Get the current time now = datetime.datetime.utcnow()
# Calculate the start time (4 hours ago) start = now - datetime.timedelta(hours=4)
# Fetch OHLCV data ohlcv = exchange.fetch_ohlcv(symbol, timeframe, since=start.timestamp())
# Convert the data to a pandas DataFrame df = pd.DataFrame(ohlcv, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
# Convert the timestamp to a datetime object df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
# Set the timestamp as the index df.set_index('timestamp', inplace=True)
return df
# Add a text input for the cryptocurrency symbolsymbol = st.text_input('Enter the cryptocurrency symbol (e.g. BTC/USD)')
# Add a button to fetch the dataif st.button('Fetch data'): if symbol: try: data = get_data(symbol) st.write(data) except Exception as e: st.error(f'Error fetching data: {e}') else: st.warning('Please enter a cryptocurrency symbol.')app.pyAdded+45