Geek7 commited on
Commit
5955af1
·
verified ·
1 Parent(s): 49720b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -17
app.py CHANGED
@@ -1,19 +1,19 @@
1
  import streamlit as st
2
  import requests
3
  import pandas as pd
4
- from datetime import datetime
5
 
6
  # Hard-coded API key for demonstration purposes
7
  API_KEY = "QR8F9B7T6R2SWTAT"
8
 
9
- def fetch_alpha_vantage_data(api_key, symbol):
10
- url = f'https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={symbol}&apikey={api_key}'
11
  response = requests.get(url)
12
  alpha_vantage_data = response.json()
13
  return alpha_vantage_data
14
 
15
  def main():
16
- st.title("Real-Time Stock Data")
17
 
18
  # User input for stock symbol
19
  symbol = st.text_input("Enter Stock Symbol (e.g., IBM):")
@@ -25,22 +25,28 @@ def main():
25
  # Use the hard-coded API key
26
  api_key = API_KEY
27
 
28
- # Continuously fetch and display real-time data
29
- while True:
30
- # Fetch Alpha Vantage data
31
- alpha_vantage_data = fetch_alpha_vantage_data(api_key, symbol)
32
 
33
- # Extract relevant data from Alpha Vantage response
34
- alpha_vantage_quote = alpha_vantage_data.get('Global Quote', {})
35
- df = pd.DataFrame([alpha_vantage_quote])
36
- df.index = [datetime.now()] # Use the current timestamp as the index
37
- df = df.dropna(axis=0)
38
 
39
- # Display the real-time data
40
- st.subheader("Real-Time Data:")
41
- st.write(df)
 
42
 
43
-
 
 
 
 
 
 
 
 
 
 
44
 
45
  if __name__ == "__main__":
46
  main()
 
1
  import streamlit as st
2
  import requests
3
  import pandas as pd
4
+ from datetime import datetime, timedelta
5
 
6
  # Hard-coded API key for demonstration purposes
7
  API_KEY = "QR8F9B7T6R2SWTAT"
8
 
9
+ def fetch_alpha_vantage_intraday(api_key, symbol):
10
+ url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={symbol}&interval=1min&apikey={api_key}'
11
  response = requests.get(url)
12
  alpha_vantage_data = response.json()
13
  return alpha_vantage_data
14
 
15
  def main():
16
+ st.title("Latest Traded Data (Last Hour)")
17
 
18
  # User input for stock symbol
19
  symbol = st.text_input("Enter Stock Symbol (e.g., IBM):")
 
25
  # Use the hard-coded API key
26
  api_key = API_KEY
27
 
28
+ # Set the time interval for fetching historical intraday data
29
+ interval = 1 # 1-minute intervals
 
 
30
 
31
+ # Fetch Alpha Vantage intraday data
32
+ alpha_vantage_data = fetch_alpha_vantage_intraday(api_key, symbol)
 
 
 
33
 
34
+ # Extract relevant data from Alpha Vantage response
35
+ alpha_vantage_time_series = alpha_vantage_data.get(f'Time Series ({interval}min)', {})
36
+ df = pd.DataFrame(alpha_vantage_time_series).T
37
+ df.index = pd.to_datetime(df.index)
38
 
39
+ # Add a new column for the trading day
40
+ df['Trading Day'] = df.index.date
41
+
42
+ # Filter data for the last hour
43
+ current_time = datetime.now()
44
+ one_hour_ago = current_time - timedelta(hours=1)
45
+ filtered_df = df[df.index >= one_hour_ago]
46
+
47
+ # Display the latest traded data for the last hour including the trading day
48
+ st.subheader("Latest Traded Data (Last Hour):")
49
+ st.write(filtered_df[['Trading Day', 'open', 'high', 'low', 'close', 'volume']].tail(1))
50
 
51
  if __name__ == "__main__":
52
  main()