Spaces:
Sleeping
Sleeping
unveilingthetruth
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import yfinance as yf
|
3 |
+
import pandas as pd
|
4 |
+
import numpy as np
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import seaborn as sns
|
7 |
+
|
8 |
+
# Function to fetch data from Yahoo Finance
|
9 |
+
def fetch_data(symbols, start_date, end_date):
|
10 |
+
data = yf.download(symbols, start=start_date, end=end_date)
|
11 |
+
return data['Adj Close']
|
12 |
+
|
13 |
+
# Function to plot line chart
|
14 |
+
def plot_line_chart(data, title):
|
15 |
+
st.write("##", title)
|
16 |
+
st.line_chart(data)
|
17 |
+
|
18 |
+
# Function to calculate correlation matrix and plot heatmap
|
19 |
+
def plot_correlation(data):
|
20 |
+
st.write("## Correlation Matrix")
|
21 |
+
correlation_matrix = data.corr()
|
22 |
+
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', fmt=".2f")
|
23 |
+
st.pyplot()
|
24 |
+
|
25 |
+
# Streamlit App
|
26 |
+
def main():
|
27 |
+
st.title("Financial Data Analysis App")
|
28 |
+
st.write("This app fetches data from Yahoo Finance, plots charts, and calculates correlations.")
|
29 |
+
|
30 |
+
# Input for symbols
|
31 |
+
symbols_input = st.text_input("Enter symbols separated by commas (e.g., AAPL, MSFT, GOOGL)", "AAPL, MSFT, GOOGL")
|
32 |
+
symbols = [symbol.strip() for symbol in symbols_input.split(',')]
|
33 |
+
|
34 |
+
# Fetch data
|
35 |
+
if st.button("Fetch Data"):
|
36 |
+
st.write("Fetching data...")
|
37 |
+
try:
|
38 |
+
data = fetch_data(symbols, pd.Timestamp.now() - pd.DateOffset(years=15), pd.Timestamp.now())
|
39 |
+
st.success("Data fetched successfully!")
|
40 |
+
|
41 |
+
# Plot line charts
|
42 |
+
for symbol in symbols:
|
43 |
+
plot_line_chart(data[symbol], f"Adjusted Close Price for {symbol}")
|
44 |
+
|
45 |
+
# Plot correlation matrix
|
46 |
+
plot_correlation(data)
|
47 |
+
|
48 |
+
except Exception as e:
|
49 |
+
st.error(f"Error fetching data: {e}")
|
50 |
+
|
51 |
+
if __name__ == "__main__":
|
52 |
+
main()
|