import streamlit as st import yfinance as yf import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns # Function to fetch data from Yahoo Finance def fetch_data(symbols, start_date, end_date): data = yf.download(symbols, start=start_date, end=end_date) return data['Adj Close'] # Function to plot line chart def plot_line_chart(data, title): st.write("##", title) st.line_chart(data) # Function to calculate correlation matrix and plot heatmap def plot_correlation(data): st.write("## Correlation Matrix") correlation_matrix = data.corr() sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f") st.pyplot() # Streamlit App def main(): st.title("Financial Data Analysis App") st.write("This app fetches data from Yahoo Finance, plots charts, and calculates correlations.") # Input for symbols symbols_input = st.text_input("Enter symbols separated by commas (e.g., AAPL, MSFT, GOOGL)", "AAPL, MSFT, GOOGL") symbols = [symbol.strip() for symbol in symbols_input.split(',')] # Fetch data if st.button("Fetch Data"): st.write("Fetching data...") try: data = fetch_data(symbols, pd.Timestamp.now() - pd.DateOffset(years=15), pd.Timestamp.now()) st.success("Data fetched successfully!") # Plot line charts for symbol in symbols: plot_line_chart(data[symbol], f"Adjusted Close Price for {symbol}") # Plot correlation matrix plot_correlation(data) except Exception as e: st.error(f"Error fetching data: {e}") if __name__ == "__main__": main()