File size: 2,820 Bytes
6c61fd9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()