newhackaton / app.py
akazmi's picture
Create app.py
6c61fd9 verified
import pandas as pd
import numpy as np
import os
import streamlit as st
# Define path to the Stocks folder inside the Data folder
stocks_path = "/root/.cache/kagglehub/datasets/borismarjanovic/price-volume-data-for-all-us-stocks-etfs/versions/3/Data/Stocks"
# Function to analyze stock data
def analyze_stock(stock_file, desired_return, risk_tolerance):
df_stock = pd.read_csv(stock_file, sep=",") # Adjust separator if needed
df_stock['Date'] = pd.to_datetime(df_stock['Date'])
df_stock = df_stock.sort_values(by='Date')
# Calculate daily returns
df_stock['Daily Return'] = df_stock['Close'].pct_change()
# Calculate annualized return and volatility
annualized_return = df_stock['Daily Return'].mean() * 252
volatility = df_stock['Daily Return'].std() * np.sqrt(252)
# Return analysis results
return stock_file, annualized_return, volatility
# Streamlit app for user inputs and stock analysis
def main():
st.title("Investment Advisory System")
st.sidebar.header("User Inputs")
desired_return = st.sidebar.number_input("Enter your desired annual return (as a decimal, e.g., 0.1 for 10%)", min_value=0.0, value=0.1)
risk_tolerance = st.sidebar.number_input("Enter your risk tolerance (standard deviation, e.g., 0.2 for 20%)", min_value=0.0, value=0.2)
st.write(f"Desired Annual Return: {desired_return * 100}%")
st.write(f"Risk Tolerance (Volatility): {risk_tolerance * 100}%")
st.header("Stock Analysis Results")
# Create a list to hold matching stocks
matching_stocks = []
# Iterate through all stock files in the Stocks folder
for stock_file in os.listdir(stocks_path):
stock_file_path = os.path.join(stocks_path, stock_file)
if stock_file.endswith('.txt'): # Process only text files
stock_name, annualized_return, volatility = analyze_stock(stock_file_path, desired_return, risk_tolerance)
# Display results for each stock
st.write(f"### {stock_name}")
st.write(f"**Annualized Return**: {annualized_return * 100:.2f}%")
st.write(f"**Annualized Volatility**: {volatility * 100:.2f}%")
# Check if the stock meets the user's criteria
if annualized_return >= desired_return and volatility <= risk_tolerance:
matching_stocks.append(stock_name)
st.success("This stock matches your criteria!")
else:
st.warning("This stock does not meet your preferences.")
# Display matched stocks at the end
if matching_stocks:
st.header("Matching Stocks")
for stock in matching_stocks:
st.write(f"- {stock}")
else:
st.write("No stocks matched your preferences.")
if __name__ == "__main__":
main()