File size: 4,155 Bytes
ab50b27 d31c699 087ef0b 8a8357f 26d7e54 ab50b27 d31c699 8a8357f d31c699 34c23a9 a261fc2 8a8357f 04f6c13 a261fc2 04f6c13 34c23a9 d31c699 357e80b 34c23a9 a2bfd8d b073561 8b8721a |
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
import streamlit as st
import requests
import pandas as pd
from datetime import datetime
import time
# Hard-coded API key for demonstration purposes
API_KEY = "QR8F9B7T6R2SWTAT"
def fetch_alpha_vantage_data(api_key, symbol):
url = f'https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol={symbol}&apikey={api_key}'
response = requests.get(url)
alpha_vantage_data = response.json()
return alpha_vantage_data
def main():
st.title("Real-Time Stock Data")
# 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)
# Extract relevant data from Alpha Vantage response
alpha_vantage_quote = alpha_vantage_data.get('Global Quote', {})
df = pd.DataFrame([alpha_vantage_quote])
df.index = [datetime.now()] # Use the current timestamp as the index
df = df.dropna(axis=0)
# Display the real-time data
st.subheader("Real-Time Data:")
st.write(df)
# Notify user and stop the app
st.balloons()
st.stop()
if __name__ == "__main__":
main()
# Hard-coded API key for demonstration purposes
API_KEY = "QR8F9B7T6R2SWTAT"
def fetch_alpha_vantage_data(api_key, symbol):
url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={symbol}&interval=5min&apikey={api_key}'
response = requests.get(url)
alpha_vantage_data = response.json()
return alpha_vantage_data
def calculate_indicators(data):
# Convert all columns to numeric
data = data.apply(pd.to_numeric, errors='coerce')
# Example: Simple condition for doji and inside
data['Doji'] = abs(data['Close'] - data['Open']) <= 0.01 * (data['High'] - data['Low'])
data['Inside'] = (data['High'] < data['High'].shift(1)) & (data['Low'] > data['Low'].shift(1))
return data
def display_signals(signal_type, signals):
st.subheader(f"{signal_type} Signals:")
st.write(signals)
def main():
st.title("Stock Trend Predictor")
# Input for stock symbol
symbol = st.text_input("Enter stock symbol (e.g., AAPL):", "AAPL")
# Fetch Alpha Vantage data
alpha_vantage_data = fetch_alpha_vantage_data(API_KEY, symbol)
# Extract relevant data from Alpha Vantage response
alpha_vantage_time_series = alpha_vantage_data.get('Time Series (5min)', {})
df = pd.DataFrame(alpha_vantage_time_series).T
df.index = pd.to_datetime(df.index)
df = df.dropna(axis=0)
# Rename columns
df = df.rename(columns={'1. open': 'Open', '2. high': 'High', '3. low': 'Low', '4. close': 'Close', '5. volume': 'Volume'})
# Calculate indicators
df = calculate_indicators(df)
# Display stock trading signals
strategic_signals = StrategicSignals(symbol=symbol)
# Display loading message during processing
with st.spinner("Predicting signals using Strategic Indicators..."):
# Display signals
st.subheader(":orange[Strategic Indicators Trend Prediction]")
display_signals("Bollinger Bands", strategic_signals.get_bollinger_bands_signals())
display_signals("Breakout", strategic_signals.get_breakout_signals())
display_signals("Crossover", strategic_signals.get_crossover_signals())
display_signals("MACD", strategic_signals.get_macd_signals())
display_signals("RSI", strategic_signals.get_rsi_signals())
# Create predictor
my_market_predictor = Pandas_Market_Predictor(df)
# Predict Trend
indicators = ["Doji", "Inside"]
# Display loading message during prediction
with st.spinner("Predicting trend using AI ...."):
# Predict trend
trend = my_market_predictor.Trend_Detection(indicators, 10)
# Display results
st.subheader(":orange[AI Trend Prediction]")
st.write("Buy Trend :", trend['BUY'])
st.write("Sell Trend :", trend['SELL'])
# Delete the DataFrame to release memory
del df
if __name__ == "__main__":
main() |