File size: 1,697 Bytes
322a9a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()