File size: 1,791 Bytes
ab50b27
d31c699
69a18df
 
d31c699
 
 
8a8357f
1226aa0
 
 
 
 
 
 
 
 
69a18df
 
0740f1b
69a18df
 
 
 
 
 
 
 
 
 
 
 
 
 
1226aa0
 
e40f1d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69a18df
 
2d114dc
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
50
51
52
53
54
import streamlit as st
import requests
import pandas as pd

# Hard-coded API key for demonstration purposes
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()  # Raise an error for bad responses
        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 Trend Predictor")

    # User input for stock symbol
    symbol = st.text_input("Enter Stock Symbol (e.g., IBM):")

    if not symbol:
        st.warning("Please enter a valid stock symbol.")
        st.stop()

    # Use the hard-coded API key
    api_key = API_KEY

    # Fetch Alpha Vantage data
    alpha_vantage_data = fetch_alpha_vantage_data(api_key, symbol)

    if alpha_vantage_data:
        # Extract relevant data from Alpha Vantage response
        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:
                # Display the raw data
                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()