app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
import plotly.graph_objs as go
|
|
|
|
| 5 |
|
| 6 |
# Function to fetch historical price data from CoinGecko
|
| 7 |
def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
|
|
@@ -11,6 +12,8 @@ def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
|
|
| 11 |
response.raise_for_status()
|
| 12 |
data = response.json()
|
| 13 |
prices = data.get('prices', [])
|
|
|
|
|
|
|
| 14 |
return prices
|
| 15 |
except requests.RequestException as e:
|
| 16 |
return f"Error fetching historical data: {e}"
|
|
@@ -22,22 +25,30 @@ def fetch_current_price(coin_id):
|
|
| 22 |
response = requests.get(url)
|
| 23 |
response.raise_for_status()
|
| 24 |
data = response.json()
|
| 25 |
-
|
|
|
|
| 26 |
except requests.RequestException as e:
|
| 27 |
return f"Error fetching current price: {e}"
|
| 28 |
|
| 29 |
# Function to convert dates to timestamps
|
| 30 |
def date_to_timestamp(date_str):
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Function to plot historical prices using Plotly
|
| 34 |
def plot_historical_prices(coin_name, from_date, to_date):
|
| 35 |
from_timestamp = date_to_timestamp(from_date)
|
| 36 |
to_timestamp = date_to_timestamp(to_date)
|
| 37 |
|
|
|
|
|
|
|
|
|
|
| 38 |
prices = fetch_historical_data(coin_name, from_timestamp, to_timestamp)
|
| 39 |
|
| 40 |
-
if isinstance(prices, str): # Error
|
| 41 |
return prices
|
| 42 |
|
| 43 |
try:
|
|
@@ -131,7 +142,7 @@ if tab == "Price Chart":
|
|
| 131 |
|
| 132 |
if st.button("Plot Chart"):
|
| 133 |
fig = plot_historical_prices(selected_crypto, from_date, to_date)
|
| 134 |
-
if isinstance(fig, str): # Error
|
| 135 |
st.error(fig)
|
| 136 |
else:
|
| 137 |
st.plotly_chart(fig)
|
|
@@ -144,7 +155,7 @@ elif tab == "Current Price":
|
|
| 144 |
elif tab == "Real-Time Trade Data":
|
| 145 |
st.header("Real-Time Cryptocurrency Trade Data")
|
| 146 |
data = fetch_real_time_trade_data(selected_crypto)
|
| 147 |
-
if isinstance(data, str): # Error
|
| 148 |
st.error(data)
|
| 149 |
else:
|
| 150 |
st.write(f"The latest trading data for {selected_crypto.capitalize()} is: ${data[1]}")
|
|
@@ -157,3 +168,6 @@ elif tab == "AI BTC Analysis":
|
|
| 157 |
elif tab == "TradingView Chart":
|
| 158 |
st.header("Live TradingView Chart")
|
| 159 |
st.markdown(tradingview_widget(), unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
import plotly.graph_objs as go
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
# Function to fetch historical price data from CoinGecko
|
| 8 |
def fetch_historical_data(coin_id, from_timestamp, to_timestamp):
|
|
|
|
| 12 |
response.raise_for_status()
|
| 13 |
data = response.json()
|
| 14 |
prices = data.get('prices', [])
|
| 15 |
+
if not prices:
|
| 16 |
+
return "No historical data available for the selected date range."
|
| 17 |
return prices
|
| 18 |
except requests.RequestException as e:
|
| 19 |
return f"Error fetching historical data: {e}"
|
|
|
|
| 25 |
response = requests.get(url)
|
| 26 |
response.raise_for_status()
|
| 27 |
data = response.json()
|
| 28 |
+
price = data.get(coin_id, {}).get('usd', 'N/A')
|
| 29 |
+
return price
|
| 30 |
except requests.RequestException as e:
|
| 31 |
return f"Error fetching current price: {e}"
|
| 32 |
|
| 33 |
# Function to convert dates to timestamps
|
| 34 |
def date_to_timestamp(date_str):
|
| 35 |
+
try:
|
| 36 |
+
return int(pd.Timestamp(date_str).timestamp())
|
| 37 |
+
except Exception as e:
|
| 38 |
+
st.error(f"Error converting date to timestamp: {e}")
|
| 39 |
+
return None
|
| 40 |
|
| 41 |
# Function to plot historical prices using Plotly
|
| 42 |
def plot_historical_prices(coin_name, from_date, to_date):
|
| 43 |
from_timestamp = date_to_timestamp(from_date)
|
| 44 |
to_timestamp = date_to_timestamp(to_date)
|
| 45 |
|
| 46 |
+
if from_timestamp is None or to_timestamp is None:
|
| 47 |
+
return "Invalid date format. Please ensure dates are in YYYY-MM-DD format."
|
| 48 |
+
|
| 49 |
prices = fetch_historical_data(coin_name, from_timestamp, to_timestamp)
|
| 50 |
|
| 51 |
+
if isinstance(prices, str): # Error message
|
| 52 |
return prices
|
| 53 |
|
| 54 |
try:
|
|
|
|
| 142 |
|
| 143 |
if st.button("Plot Chart"):
|
| 144 |
fig = plot_historical_prices(selected_crypto, from_date, to_date)
|
| 145 |
+
if isinstance(fig, str): # Error message
|
| 146 |
st.error(fig)
|
| 147 |
else:
|
| 148 |
st.plotly_chart(fig)
|
|
|
|
| 155 |
elif tab == "Real-Time Trade Data":
|
| 156 |
st.header("Real-Time Cryptocurrency Trade Data")
|
| 157 |
data = fetch_real_time_trade_data(selected_crypto)
|
| 158 |
+
if isinstance(data, str): # Error message
|
| 159 |
st.error(data)
|
| 160 |
else:
|
| 161 |
st.write(f"The latest trading data for {selected_crypto.capitalize()} is: ${data[1]}")
|
|
|
|
| 168 |
elif tab == "TradingView Chart":
|
| 169 |
st.header("Live TradingView Chart")
|
| 170 |
st.markdown(tradingview_widget(), unsafe_allow_html=True)
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
|