Update app.py
Browse files
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
|
10 |
-
url = f'https://www.alphavantage.co/query?function=
|
11 |
response = requests.get(url)
|
12 |
alpha_vantage_data = response.json()
|
13 |
return alpha_vantage_data
|
14 |
|
15 |
def main():
|
16 |
-
st.title("
|
17 |
|
18 |
# User input for stock symbol
|
19 |
symbol = st.text_input("Enter Stock Symbol (e.g., IBM):")
|
@@ -25,28 +25,23 @@ def main():
|
|
25 |
# Use the hard-coded API key
|
26 |
api_key = API_KEY
|
27 |
|
28 |
-
#
|
29 |
-
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
df.index = pd.to_datetime(df.index)
|
38 |
|
39 |
-
|
40 |
-
|
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()
|
|
|
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 |
# 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 |
+
# Add a delay to avoid exceeding API rate limits
|
44 |
+
st.experimental_sleep(60) # Sleep for 60 seconds (adjust as needed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
if __name__ == "__main__":
|
47 |
main()
|