St / app.py
Geek7's picture
Update app.py
77a8be7 verified
raw
history blame
2.29 kB
import streamlit as st
import requests
from Pandas_Market_Predictor import Pandas_Market_Predictor
import pandas as pd
# Hard-coded API key for demonstration purposes
API_KEY = "QR8F9B7T6R2SWTAT"
def fetch_alpha_vantage_data(api_key):
url = f'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&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 main():
st.title("Stock Trend Predictor")
# Use the hard-coded API key
api_key = API_KEY
# Fetch Alpha Vantage data
alpha_vantage_data = fetch_alpha_vantage_data(api_key)
# 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)
# Create predictor
my_market_predictor = Pandas_Market_Predictor(df)
# Predict Trend
indicators = ["Doji", "Inside"]
trend = my_market_predictor.Trend_Detection(indicators, 10)
# Display results
st.subheader("Predicted Trend:")
st.write("Buy Trend :", trend['BUY'])
st.write("Sell Trend :", trend['SELL'])
# Calculate and display Support and Resistance Levels
levels = my_market_predictor.Support_Resistance_Estimation_Tool(indicators)
st.subheader("Support and Resistance Levels:")
st.write("Support Level:", levels.get('Support', 'Not Available'))
st.write("Resistance Level:", levels.get('Resistance', 'Not Available'))
# Delete the DataFrame to release memory
del df
if __name__ == "__main__":
main()