Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import os
|
| 4 |
+
import streamlit as st
|
| 5 |
+
|
| 6 |
+
# Define path to the Stocks folder inside the Data folder
|
| 7 |
+
stocks_path = "/root/.cache/kagglehub/datasets/borismarjanovic/price-volume-data-for-all-us-stocks-etfs/versions/3/Data/Stocks"
|
| 8 |
+
|
| 9 |
+
# Function to analyze stock data
|
| 10 |
+
def analyze_stock(stock_file, desired_return, risk_tolerance):
|
| 11 |
+
df_stock = pd.read_csv(stock_file, sep=",") # Adjust separator if needed
|
| 12 |
+
df_stock['Date'] = pd.to_datetime(df_stock['Date'])
|
| 13 |
+
df_stock = df_stock.sort_values(by='Date')
|
| 14 |
+
|
| 15 |
+
# Calculate daily returns
|
| 16 |
+
df_stock['Daily Return'] = df_stock['Close'].pct_change()
|
| 17 |
+
|
| 18 |
+
# Calculate annualized return and volatility
|
| 19 |
+
annualized_return = df_stock['Daily Return'].mean() * 252
|
| 20 |
+
volatility = df_stock['Daily Return'].std() * np.sqrt(252)
|
| 21 |
+
|
| 22 |
+
# Return analysis results
|
| 23 |
+
return stock_file, annualized_return, volatility
|
| 24 |
+
|
| 25 |
+
# Streamlit app for user inputs and stock analysis
|
| 26 |
+
def main():
|
| 27 |
+
st.title("Investment Advisory System")
|
| 28 |
+
|
| 29 |
+
st.sidebar.header("User Inputs")
|
| 30 |
+
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)
|
| 31 |
+
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)
|
| 32 |
+
|
| 33 |
+
st.write(f"Desired Annual Return: {desired_return * 100}%")
|
| 34 |
+
st.write(f"Risk Tolerance (Volatility): {risk_tolerance * 100}%")
|
| 35 |
+
|
| 36 |
+
st.header("Stock Analysis Results")
|
| 37 |
+
|
| 38 |
+
# Create a list to hold matching stocks
|
| 39 |
+
matching_stocks = []
|
| 40 |
+
|
| 41 |
+
# Iterate through all stock files in the Stocks folder
|
| 42 |
+
for stock_file in os.listdir(stocks_path):
|
| 43 |
+
stock_file_path = os.path.join(stocks_path, stock_file)
|
| 44 |
+
if stock_file.endswith('.txt'): # Process only text files
|
| 45 |
+
stock_name, annualized_return, volatility = analyze_stock(stock_file_path, desired_return, risk_tolerance)
|
| 46 |
+
|
| 47 |
+
# Display results for each stock
|
| 48 |
+
st.write(f"### {stock_name}")
|
| 49 |
+
st.write(f"**Annualized Return**: {annualized_return * 100:.2f}%")
|
| 50 |
+
st.write(f"**Annualized Volatility**: {volatility * 100:.2f}%")
|
| 51 |
+
|
| 52 |
+
# Check if the stock meets the user's criteria
|
| 53 |
+
if annualized_return >= desired_return and volatility <= risk_tolerance:
|
| 54 |
+
matching_stocks.append(stock_name)
|
| 55 |
+
st.success("This stock matches your criteria!")
|
| 56 |
+
else:
|
| 57 |
+
st.warning("This stock does not meet your preferences.")
|
| 58 |
+
|
| 59 |
+
# Display matched stocks at the end
|
| 60 |
+
if matching_stocks:
|
| 61 |
+
st.header("Matching Stocks")
|
| 62 |
+
for stock in matching_stocks:
|
| 63 |
+
st.write(f"- {stock}")
|
| 64 |
+
else:
|
| 65 |
+
st.write("No stocks matched your preferences.")
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
main()
|