|
import streamlit as st |
|
import requests |
|
import pandas as pd |
|
|
|
|
|
API_KEY = "QR8F9B7T6R2SWTAT" |
|
|
|
def fetch_alpha_vantage_data(api_key, symbol): |
|
try: |
|
url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={symbol}&interval=5min&apikey={api_key}' |
|
response = requests.get(url) |
|
response.raise_for_status() |
|
alpha_vantage_data = response.json() |
|
return alpha_vantage_data |
|
except requests.RequestException as e: |
|
st.error(f"Error fetching data: {e}") |
|
return None |
|
|
|
def main(): |
|
st.title("Stock Data Analysis") |
|
|
|
|
|
symbol = st.text_input("Enter Stock Symbol (e.g., IBM):") |
|
|
|
if not symbol: |
|
st.warning("Please enter a valid stock symbol.") |
|
st.stop() |
|
|
|
|
|
api_key = API_KEY |
|
|
|
|
|
alpha_vantage_data = fetch_alpha_vantage_data(api_key, symbol) |
|
|
|
if alpha_vantage_data: |
|
|
|
time_series_key = 'Time Series (5min)' |
|
if time_series_key in alpha_vantage_data: |
|
alpha_vantage_time_series = alpha_vantage_data[time_series_key] |
|
df = pd.DataFrame(alpha_vantage_time_series).T |
|
df.index = pd.to_datetime(df.index) |
|
df = df.dropna(axis=0) |
|
|
|
if not df.empty: |
|
|
|
st.subheader("Raw Data:") |
|
st.write(df) |
|
else: |
|
st.warning("No intraday data available for the specified symbol.") |
|
else: |
|
st.warning(f"Key '{time_series_key}' not found in API response.") |
|
|
|
if __name__ == "__main__": |
|
main() |