-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
962 lines (801 loc) Β· 38.8 KB
/
app.py
File metadata and controls
962 lines (801 loc) Β· 38.8 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
import streamlit as st
import yfinance as yf
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
from datetime import datetime, timedelta
import numpy as np
from reportlab.lib.pagesizes import letter
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer, PageBreak, Image
from reportlab.lib.enums import TA_CENTER, TA_LEFT
import matplotlib
matplotlib.use('Agg') # Use non-interactive backend
import matplotlib.pyplot as plt
import io
# Set page configuration
st.set_page_config(
page_title="Stock Financial Data Analyzer",
page_icon="π",
layout="wide"
)
# Main title
st.title("π Stock Financial Data Analyzer")
st.markdown("Get comprehensive financial analysis and interactive charts for any stock symbol")
# Sidebar for stock input and time range selection
with st.sidebar:
st.header("Configuration")
# Stock symbol input
stock_symbol = st.text_input(
"Enter Stock Symbol",
value="AAPL",
help="Enter a valid stock symbol (e.g., AAPL, GOOGL, MSFT)"
).upper()
# Time range selection for charts
time_ranges = {
"1M": 30,
"3M": 90,
"6M": 180,
"1Y": 365,
"5Y": 1825
}
selected_range = st.selectbox(
"Select Time Range for Charts",
options=list(time_ranges.keys()),
index=3 # Default to 1Y
)
analyze_button = st.button("Analyze Stock", type="primary")
# Multi-stock comparison section
st.divider()
st.subheader("Compare Multiple Stocks")
comparison_symbols = st.text_input(
"Enter stock symbols (comma-separated)",
value="AAPL,MSFT,GOOGL",
help="Enter 2-5 stock symbols separated by commas"
)
compare_button = st.button("Compare Stocks", type="secondary")
# Stock screener section
st.divider()
st.subheader("Stock Screener")
screener_symbols = st.text_area(
"Enter stock symbols to screen (comma-separated)",
value="AAPL,MSFT,GOOGL,AMZN,TSLA,META,NVDA,AMD,NFLX,DIS",
help="Enter stock symbols separated by commas"
)
with st.expander("Screening Criteria"):
col1, col2 = st.columns(2)
with col1:
min_pe = st.number_input("Min P/E Ratio", value=0.0, step=1.0, help="Minimum P/E ratio")
max_pe = st.number_input("Max P/E Ratio", value=100.0, step=1.0, help="Maximum P/E ratio")
min_market_cap = st.number_input("Min Market Cap (Billions)", value=0.0, step=10.0, help="Minimum market cap in billions")
max_market_cap = st.number_input("Max Market Cap (Billions)", value=10000.0, step=100.0, help="Maximum market cap in billions")
with col2:
min_volume = st.number_input("Min Avg Volume (Millions)", value=0.0, step=1.0, help="Minimum average volume in millions")
min_dividend = st.number_input("Min Dividend Yield (%)", value=0.0, step=0.5, help="Minimum dividend yield percentage")
screen_button = st.button("Screen Stocks", type="secondary")
def get_stock_info(symbol):
"""Fetch stock information from Yahoo Finance"""
try:
stock = yf.Ticker(symbol)
info = stock.info
# Check if the stock exists by verifying we have basic info
if not info or info.get('regularMarketPrice') is None:
return None, "Invalid stock symbol or no data available"
return stock, None
except Exception as e:
return None, f"Error fetching stock data: {str(e)}"
def get_financial_summary(stock_info):
"""Extract key financial metrics from stock info"""
try:
summary_data = {
"Current Price": stock_info.get('regularMarketPrice', 'N/A'),
"Previous Close": stock_info.get('previousClose', 'N/A'),
"Market Cap": stock_info.get('marketCap', 'N/A'),
"P/E Ratio": stock_info.get('trailingPE', 'N/A'),
"Forward P/E": stock_info.get('forwardPE', 'N/A'),
"Dividend Yield (%)": stock_info.get('dividendYield', 'N/A'),
"52 Week High": stock_info.get('fiftyTwoWeekHigh', 'N/A'),
"52 Week Low": stock_info.get('fiftyTwoWeekLow', 'N/A'),
"Volume": stock_info.get('volume', 'N/A'),
"Average Volume": stock_info.get('averageVolume', 'N/A'),
"Beta": stock_info.get('beta', 'N/A'),
"EPS": stock_info.get('trailingEps', 'N/A')
}
# Format certain values
if summary_data["Market Cap"] != 'N/A':
summary_data["Market Cap"] = f"${summary_data['Market Cap']:,}"
if summary_data["Dividend Yield (%)"] != 'N/A':
summary_data["Dividend Yield (%)"] = f"{summary_data['Dividend Yield (%)'] * 100:.2f}%"
if summary_data["Current Price"] != 'N/A':
summary_data["Current Price"] = f"${summary_data['Current Price']:.2f}"
if summary_data["Previous Close"] != 'N/A':
summary_data["Previous Close"] = f"${summary_data['Previous Close']:.2f}"
if summary_data["52 Week High"] != 'N/A':
summary_data["52 Week High"] = f"${summary_data['52 Week High']:.2f}"
if summary_data["52 Week Low"] != 'N/A':
summary_data["52 Week Low"] = f"${summary_data['52 Week Low']:.2f}"
return summary_data
except Exception as e:
st.error(f"Error processing financial summary: {str(e)}")
return None
def get_historical_data(stock, days):
"""Fetch historical stock data"""
try:
end_date = datetime.now()
start_date = end_date - timedelta(days=days)
hist_data = stock.history(start=start_date, end=end_date)
if hist_data.empty:
return None, "No historical data available for this time range"
return hist_data, None
except Exception as e:
return None, f"Error fetching historical data: {str(e)}"
def calculate_sma(data, window):
"""Calculate Simple Moving Average"""
return data['Close'].rolling(window=window).mean()
def calculate_rsi(data, period=14):
"""Calculate Relative Strength Index"""
delta = data['Close'].diff()
gain = (delta.where(delta > 0, 0)).rolling(window=period).mean()
loss = (-delta.where(delta < 0, 0)).rolling(window=period).mean()
rs = gain / loss
rsi = 100 - (100 / (1 + rs))
return rsi
def calculate_macd(data, fast=12, slow=26, signal=9):
"""Calculate MACD (Moving Average Convergence Divergence)"""
exp1 = data['Close'].ewm(span=fast, adjust=False).mean()
exp2 = data['Close'].ewm(span=slow, adjust=False).mean()
macd = exp1 - exp2
signal_line = macd.ewm(span=signal, adjust=False).mean()
histogram = macd - signal_line
return macd, signal_line, histogram
def create_price_chart(hist_data, symbol, time_range, show_sma20=False, show_sma50=False, show_sma200=False):
"""Create interactive price chart with optional technical indicators"""
fig = go.Figure()
# Main price line
fig.add_trace(go.Scatter(
x=hist_data.index,
y=hist_data['Close'],
mode='lines',
name='Close Price',
line=dict(color='#1f77b4', width=2)
))
# Add SMA indicators if requested
if show_sma20:
sma20 = calculate_sma(hist_data, 20)
fig.add_trace(go.Scatter(
x=hist_data.index,
y=sma20,
mode='lines',
name='SMA 20',
line=dict(color='#ff7f0e', width=1, dash='dash')
))
if show_sma50:
sma50 = calculate_sma(hist_data, 50)
fig.add_trace(go.Scatter(
x=hist_data.index,
y=sma50,
mode='lines',
name='SMA 50',
line=dict(color='#2ca02c', width=1, dash='dash')
))
if show_sma200:
sma200 = calculate_sma(hist_data, 200)
fig.add_trace(go.Scatter(
x=hist_data.index,
y=sma200,
mode='lines',
name='SMA 200',
line=dict(color='#d62728', width=1, dash='dash')
))
fig.update_layout(
title=f"{symbol} Stock Price - {time_range}",
xaxis_title="Date",
yaxis_title="Price ($)",
hovermode='x unified',
template='plotly_white',
height=500
)
return fig
def create_volume_chart(hist_data, symbol, time_range):
"""Create interactive volume chart"""
fig = go.Figure()
fig.add_trace(go.Bar(
x=hist_data.index,
y=hist_data['Volume'],
name='Volume',
marker_color='#ff7f0e'
))
fig.update_layout(
title=f"{symbol} Trading Volume - {time_range}",
xaxis_title="Date",
yaxis_title="Volume",
template='plotly_white',
height=400
)
return fig
def create_rsi_chart(hist_data, symbol, time_range):
"""Create RSI chart"""
rsi = calculate_rsi(hist_data)
fig = go.Figure()
fig.add_trace(go.Scatter(
x=hist_data.index,
y=rsi,
mode='lines',
name='RSI',
line=dict(color='#9467bd', width=2)
))
# Add overbought/oversold reference lines
fig.add_hline(y=70, line_dash="dash", line_color="red", annotation_text="Overbought (70)")
fig.add_hline(y=30, line_dash="dash", line_color="green", annotation_text="Oversold (30)")
fig.update_layout(
title=f"{symbol} RSI (Relative Strength Index) - {time_range}",
xaxis_title="Date",
yaxis_title="RSI",
template='plotly_white',
height=400,
yaxis=dict(range=[0, 100])
)
return fig
def create_macd_chart(hist_data, symbol, time_range):
"""Create MACD chart"""
macd, signal_line, histogram = calculate_macd(hist_data)
fig = go.Figure()
# MACD line
fig.add_trace(go.Scatter(
x=hist_data.index,
y=macd,
mode='lines',
name='MACD',
line=dict(color='#1f77b4', width=2)
))
# Signal line
fig.add_trace(go.Scatter(
x=hist_data.index,
y=signal_line,
mode='lines',
name='Signal',
line=dict(color='#ff7f0e', width=2)
))
# Histogram
colors = ['red' if val < 0 else 'green' for val in histogram]
fig.add_trace(go.Bar(
x=hist_data.index,
y=histogram,
name='Histogram',
marker_color=colors,
opacity=0.5
))
fig.update_layout(
title=f"{symbol} MACD - {time_range}",
xaxis_title="Date",
yaxis_title="MACD",
template='plotly_white',
height=400
)
return fig
def prepare_csv_data(summary_data, hist_data, symbol):
"""Prepare data for CSV export"""
# Create summary DataFrame
summary_df = pd.DataFrame(list(summary_data.items()), columns=['Metric', 'Value'])
summary_df.insert(0, 'Stock Symbol', symbol)
# Prepare historical data
hist_df = hist_data.reset_index()
hist_df.insert(0, 'Stock Symbol', symbol)
return summary_df, hist_df
def create_comparison_chart(symbols_data, time_range):
"""Create overlay price comparison chart for multiple stocks"""
fig = go.Figure()
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd']
for idx, (symbol, data) in enumerate(symbols_data.items()):
hist_data = data['historical']
# Normalize to percentage change from first day
if len(hist_data) > 0:
first_price = hist_data['Close'].iloc[0]
normalized_prices = ((hist_data['Close'] / first_price) - 1) * 100
fig.add_trace(go.Scatter(
x=hist_data.index,
y=normalized_prices,
mode='lines',
name=symbol,
line=dict(color=colors[idx % len(colors)], width=2)
))
fig.update_layout(
title=f"Stock Price Comparison (% Change) - {time_range}",
xaxis_title="Date",
yaxis_title="% Change from Start",
hovermode='x unified',
template='plotly_white',
height=500,
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
)
return fig
def get_comparison_metrics(symbols):
"""Get key metrics for multiple stocks"""
comparison_data = {}
failed_symbols = []
for symbol in symbols:
stock, error = get_stock_info(symbol.strip().upper())
if not error and stock:
info = stock.info
comparison_data[symbol.upper()] = {
'Company': info.get('longName', symbol),
'Current Price': f"${info.get('regularMarketPrice', 0):.2f}",
'Market Cap': f"${info.get('marketCap', 0):,}" if info.get('marketCap') else 'N/A',
'P/E Ratio': f"{info.get('trailingPE', 0):.2f}" if info.get('trailingPE') else 'N/A',
'Dividend Yield': f"{info.get('dividendYield', 0)*100:.2f}%" if info.get('dividendYield') else 'N/A',
'52W High': f"${info.get('fiftyTwoWeekHigh', 0):.2f}" if info.get('fiftyTwoWeekHigh') else 'N/A',
'52W Low': f"${info.get('fiftyTwoWeekLow', 0):.2f}" if info.get('fiftyTwoWeekLow') else 'N/A',
'Beta': f"{info.get('beta', 0):.2f}" if info.get('beta') else 'N/A'
}
else:
failed_symbols.append(symbol.upper())
return comparison_data, failed_symbols
def get_fundamental_data(stock):
"""Fetch fundamental financial statements"""
try:
# Get financial statements
income_stmt = stock.financials
balance_sheet = stock.balance_sheet
cash_flow = stock.cashflow
return {
'income_statement': income_stmt,
'balance_sheet': balance_sheet,
'cash_flow': cash_flow
}
except Exception as e:
return None
def format_financial_data(df, num_columns=4):
"""Format financial data for display"""
if df is None or df.empty:
return None
# Transpose to have dates as rows
df_display = df.T
# Take only the most recent periods
df_display = df_display.head(num_columns)
# Format large numbers to millions/billions
df_formatted = df_display.copy()
for col in df_formatted.columns:
if df_formatted[col].dtype in ['float64', 'int64']:
df_formatted[col] = df_formatted[col].apply(
lambda x: f"${x/1e9:.2f}B" if abs(x) >= 1e9 else f"${x/1e6:.2f}M" if abs(x) >= 1e6 else f"${x:,.0f}" if pd.notna(x) else "N/A"
)
return df_formatted
def screen_stocks(symbols, criteria):
"""Screen stocks based on given criteria"""
screened_stocks = []
failed_symbols = []
for symbol in symbols:
stock, error = get_stock_info(symbol.strip().upper())
if not error and stock:
info = stock.info
# Extract metrics
pe_ratio = info.get('trailingPE')
market_cap = info.get('marketCap')
avg_volume = info.get('averageVolume')
dividend_yield = info.get('dividendYield', 0)
# Apply filters
passes = True
# P/E Ratio filter
if pe_ratio is not None:
if pe_ratio < criteria['min_pe'] or pe_ratio > criteria['max_pe']:
passes = False
elif criteria['min_pe'] > 0: # If min_pe is set but PE is None, exclude
passes = False
# Market Cap filter (convert to billions)
if market_cap is not None:
market_cap_billions = market_cap / 1e9
if market_cap_billions < criteria['min_market_cap'] or market_cap_billions > criteria['max_market_cap']:
passes = False
else:
passes = False
# Average Volume filter (convert to millions)
if avg_volume is not None:
avg_volume_millions = avg_volume / 1e6
if avg_volume_millions < criteria['min_volume']:
passes = False
else:
passes = False
# Dividend Yield filter
if dividend_yield is not None:
dividend_yield_pct = dividend_yield * 100
if dividend_yield_pct < criteria['min_dividend']:
passes = False
elif criteria['min_dividend'] > 0:
passes = False
if passes:
screened_stocks.append({
'Symbol': symbol.upper(),
'Company': info.get('longName', symbol),
'Price': f"${info.get('regularMarketPrice', 0):.2f}",
'P/E': f"{pe_ratio:.2f}" if pe_ratio else 'N/A',
'Market Cap': f"${market_cap/1e9:.2f}B" if market_cap else 'N/A',
'Avg Volume': f"${avg_volume/1e6:.2f}M" if avg_volume else 'N/A',
'Dividend Yield': f"{dividend_yield*100:.2f}%" if dividend_yield else '0.00%'
})
else:
failed_symbols.append(symbol.upper())
return screened_stocks, failed_symbols
def create_matplotlib_chart(hist_data, symbol, chart_type='price'):
"""Create matplotlib charts for PDF export"""
fig, ax = plt.subplots(figsize=(8, 4))
if chart_type == 'price':
ax.plot(hist_data.index, hist_data['Close'], linewidth=2, color='#1f77b4')
ax.set_title(f'{symbol} Stock Price', fontsize=14, fontweight='bold')
ax.set_ylabel('Price ($)', fontsize=10)
elif chart_type == 'volume':
ax.bar(hist_data.index, hist_data['Volume'], color='#ff7f0e', alpha=0.7)
ax.set_title(f'{symbol} Trading Volume', fontsize=14, fontweight='bold')
ax.set_ylabel('Volume', fontsize=10)
elif chart_type == 'rsi':
rsi = calculate_rsi(hist_data)
ax.plot(hist_data.index, rsi, linewidth=2, color='#9467bd')
ax.axhline(y=70, color='r', linestyle='--', alpha=0.5)
ax.axhline(y=30, color='g', linestyle='--', alpha=0.5)
ax.set_title(f'{symbol} RSI (Relative Strength Index)', fontsize=14, fontweight='bold')
ax.set_ylabel('RSI', fontsize=10)
ax.set_ylim(0, 100)
ax.set_xlabel('Date', fontsize=10)
ax.grid(True, alpha=0.3)
plt.xticks(rotation=45)
plt.tight_layout()
# Save to bytes buffer
img_buffer = io.BytesIO()
plt.savefig(img_buffer, format='png', dpi=150, bbox_inches='tight')
plt.close()
img_buffer.seek(0)
return img_buffer
def generate_pdf_report(stock_symbol, stock_info, summary_data, hist_data):
"""Generate comprehensive PDF report for a stock"""
buffer = io.BytesIO()
doc = SimpleDocTemplate(buffer, pagesize=letter)
story = []
styles = getSampleStyleSheet()
# Custom styles
title_style = ParagraphStyle(
'CustomTitle',
parent=styles['Heading1'],
fontSize=24,
textColor=colors.HexColor('#1f77b4'),
spaceAfter=30,
alignment=TA_CENTER
)
heading_style = ParagraphStyle(
'CustomHeading',
parent=styles['Heading2'],
fontSize=16,
textColor=colors.HexColor('#333333'),
spaceAfter=12,
spaceBefore=12
)
# Title page
company_name = stock_info.get('longName', stock_symbol)
story.append(Paragraph(f"Stock Analysis Report", title_style))
story.append(Paragraph(f"{company_name} ({stock_symbol})", styles['Heading2']))
story.append(Spacer(1, 0.2*inch))
story.append(Paragraph(f"Report Generated: {datetime.now().strftime('%B %d, %Y')}", styles['Normal']))
story.append(Spacer(1, 0.5*inch))
# Summary metrics table
story.append(Paragraph("Key Financial Metrics", heading_style))
# Prepare table data
table_data = [['Metric', 'Value']]
for key, value in summary_data.items():
table_data.append([key, str(value)])
# Create table
t = Table(table_data, colWidths=[3.5*inch, 2.5*inch])
t.setStyle(TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#1f77b4')),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('FONTSIZE', (0, 0), (-1, 0), 12),
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
('BACKGROUND', (0, 1), (-1, -1), colors.beige),
('GRID', (0, 0), (-1, -1), 1, colors.black),
('FONTSIZE', (0, 1), (-1, -1), 10),
('ROWBACKGROUNDS', (0, 1), (-1, -1), [colors.white, colors.lightgrey]),
]))
story.append(t)
story.append(PageBreak())
# Price chart
story.append(Paragraph("Price Chart", heading_style))
price_chart = create_matplotlib_chart(hist_data, stock_symbol, 'price')
story.append(Image(price_chart, width=6*inch, height=3*inch))
story.append(Spacer(1, 0.3*inch))
# Volume chart
story.append(Paragraph("Trading Volume", heading_style))
volume_chart = create_matplotlib_chart(hist_data, stock_symbol, 'volume')
story.append(Image(volume_chart, width=6*inch, height=3*inch))
story.append(PageBreak())
# Technical indicators
story.append(Paragraph("Technical Analysis", heading_style))
story.append(Paragraph("RSI (Relative Strength Index)", styles['Heading3']))
rsi_chart = create_matplotlib_chart(hist_data, stock_symbol, 'rsi')
story.append(Image(rsi_chart, width=6*inch, height=3*inch))
story.append(Spacer(1, 0.2*inch))
# Summary and disclaimer
story.append(Spacer(1, 0.3*inch))
story.append(Paragraph("Disclaimer", heading_style))
disclaimer_text = """
This report is for informational purposes only and should not be considered as investment advice.
All data is sourced from Yahoo Finance. Past performance does not guarantee future results.
Please consult with a financial advisor before making investment decisions.
"""
story.append(Paragraph(disclaimer_text, styles['Normal']))
# Build PDF
doc.build(story)
buffer.seek(0)
return buffer
# Main application logic
if analyze_button or stock_symbol:
if not stock_symbol:
st.warning("Please enter a stock symbol")
else:
with st.spinner(f"Fetching data for {stock_symbol}..."):
stock, error = get_stock_info(stock_symbol)
if error:
st.error(error)
else:
# Get stock info and company name
stock_info = stock.info
company_name = stock_info.get('longName', stock_symbol)
st.success(f"Successfully loaded data for {company_name} ({stock_symbol})")
# Create two columns for layout
col1, col2 = st.columns([1, 1])
with col1:
st.header("π Financial Summary")
summary_data = get_financial_summary(stock_info)
if summary_data:
# Display summary as a formatted table
summary_df = pd.DataFrame(list(summary_data.items()), columns=['Metric', 'Value'])
# Ensure all values are strings for proper display
summary_df['Value'] = summary_df['Value'].astype(str)
st.dataframe(summary_df, width='stretch', hide_index=True)
with col2:
st.header("π Key Statistics")
# Additional metrics in a more visual format
current_price = stock_info.get('regularMarketPrice', 0)
prev_close = stock_info.get('previousClose', 0)
if current_price and prev_close:
change = current_price - prev_close
change_percent = (change / prev_close) * 100
if change >= 0:
st.metric("Current Price", f"${current_price:.2f}", f"+${change:.2f} (+{change_percent:.2f}%)")
else:
st.metric("Current Price", f"${current_price:.2f}", f"${change:.2f} ({change_percent:.2f}%)")
# Display additional metrics
market_cap = stock_info.get('marketCap')
if market_cap:
st.metric("Market Cap", f"${market_cap:,}")
pe_ratio = stock_info.get('trailingPE')
if pe_ratio:
st.metric("P/E Ratio", f"{pe_ratio:.2f}")
# Fetch and display historical data
st.header("π Historical Data & Charts")
days = time_ranges[selected_range]
hist_data, hist_error = get_historical_data(stock, days)
if hist_error:
st.error(hist_error)
else:
# Technical Indicators Controls
st.subheader("π Technical Indicators")
col1, col2, col3 = st.columns(3)
with col1:
show_sma20 = st.checkbox("SMA 20", value=False, help="20-day Simple Moving Average")
with col2:
show_sma50 = st.checkbox("SMA 50", value=False, help="50-day Simple Moving Average")
with col3:
show_sma200 = st.checkbox("SMA 200", value=False, help="200-day Simple Moving Average")
# Create tabs for different charts
tab1, tab2, tab3, tab4, tab5 = st.tabs(["Price Chart", "RSI", "MACD", "Volume", "Data Table"])
with tab1:
price_chart = create_price_chart(
hist_data,
stock_symbol,
selected_range,
show_sma20=show_sma20,
show_sma50=show_sma50,
show_sma200=show_sma200
)
st.plotly_chart(price_chart, use_container_width=True)
with tab2:
rsi_chart = create_rsi_chart(hist_data, stock_symbol, selected_range)
st.plotly_chart(rsi_chart, use_container_width=True)
st.info("π RSI > 70 indicates overbought conditions, RSI < 30 indicates oversold conditions")
with tab3:
macd_chart = create_macd_chart(hist_data, stock_symbol, selected_range)
st.plotly_chart(macd_chart, use_container_width=True)
st.info("π MACD crossovers signal potential buy/sell opportunities")
with tab4:
volume_chart = create_volume_chart(hist_data, stock_symbol, selected_range)
st.plotly_chart(volume_chart, use_container_width=True)
with tab5:
st.subheader("Historical Price Data")
# Format the historical data for display
display_data = hist_data.copy()
for col in ['Open', 'High', 'Low', 'Close']:
if col in display_data.columns:
display_data[col] = display_data[col].round(2)
st.dataframe(display_data, width='stretch')
# CSV Download Section
st.header("πΎ Export Data")
col1, col2, col3 = st.columns([1, 1, 1])
with col1:
# Prepare CSV data
summary_df, hist_df = prepare_csv_data(summary_data, hist_data, stock_symbol)
# Convert DataFrames to CSV
summary_csv = summary_df.to_csv(index=False)
historical_csv = hist_df.to_csv(index=False)
st.download_button(
label="π Download Summary Data (CSV)",
data=summary_csv,
file_name=f"{stock_symbol}_summary_{datetime.now().strftime('%Y%m%d')}.csv",
mime="text/csv"
)
with col2:
st.download_button(
label="π Download Historical Data (CSV)",
data=historical_csv,
file_name=f"{stock_symbol}_historical_{selected_range}_{datetime.now().strftime('%Y%m%d')}.csv",
mime="text/csv"
)
with col3:
# Generate PDF report
pdf_buffer = generate_pdf_report(stock_symbol, stock_info, summary_data, hist_data)
st.download_button(
label="π Download PDF Report",
data=pdf_buffer,
file_name=f"{stock_symbol}_report_{datetime.now().strftime('%Y%m%d')}.pdf",
mime="application/pdf"
)
# Fundamental Analysis Section
st.header("π Fundamental Analysis")
with st.spinner("Fetching fundamental data..."):
fundamental_data = get_fundamental_data(stock)
if fundamental_data:
fund_tab1, fund_tab2, fund_tab3 = st.tabs(["Income Statement", "Balance Sheet", "Cash Flow"])
with fund_tab1:
st.subheader("Income Statement")
income_df = format_financial_data(fundamental_data['income_statement'])
if income_df is not None and not income_df.empty:
st.dataframe(income_df, width='stretch')
else:
st.info("Income statement data not available for this stock")
with fund_tab2:
st.subheader("Balance Sheet")
balance_df = format_financial_data(fundamental_data['balance_sheet'])
if balance_df is not None and not balance_df.empty:
st.dataframe(balance_df, width='stretch')
else:
st.info("Balance sheet data not available for this stock")
with fund_tab3:
st.subheader("Cash Flow Statement")
cashflow_df = format_financial_data(fundamental_data['cash_flow'])
if cashflow_df is not None and not cashflow_df.empty:
st.dataframe(cashflow_df, width='stretch')
else:
st.info("Cash flow data not available for this stock")
else:
st.info("Fundamental data not available for this stock")
# Multi-stock comparison section
if compare_button:
if not comparison_symbols:
st.warning("Please enter stock symbols to compare")
else:
# Parse and deduplicate symbols
symbols_list = [s.strip().upper() for s in comparison_symbols.split(',') if s.strip()]
original_count = len(symbols_list)
symbols_list = list(dict.fromkeys(symbols_list)) # Remove duplicates while preserving order
# Notify user if duplicates were removed
if len(symbols_list) < original_count:
duplicates_removed = original_count - len(symbols_list)
st.info(f"βΉοΈ Removed {duplicates_removed} duplicate symbol(s). Comparing {len(symbols_list)} unique stocks.")
if len(symbols_list) < 2:
st.warning("Please enter at least 2 unique stock symbols to compare")
elif len(symbols_list) > 5:
st.warning("Please enter no more than 5 stock symbols to compare")
else:
with st.spinner("Fetching comparison data..."):
# Get comparison metrics
comparison_data, failed_symbols = get_comparison_metrics(symbols_list)
# Display warnings for failed symbols
if failed_symbols:
st.warning(f"β οΈ Unable to fetch data for: {', '.join(failed_symbols)}")
if len(comparison_data) == 0:
st.error("Unable to fetch data for any of the provided symbols. Please check your symbols and try again.")
elif len(comparison_data) < 2:
st.error(f"Only {len(comparison_data)} valid symbol found. Need at least 2 stocks for comparison.")
else:
# Update header with actual count
st.header(f"π Comparing {len(comparison_data)} Stocks: {', '.join(comparison_data.keys())}")
# Display comparison table
st.subheader("π Side-by-Side Metrics Comparison")
comparison_df = pd.DataFrame(comparison_data).T
st.dataframe(comparison_df, width='stretch')
# Get historical data for comparison chart
st.subheader("π Price Performance Comparison")
days = time_ranges[selected_range]
symbols_data = {}
for symbol in comparison_data.keys():
stock, error = get_stock_info(symbol)
if not error:
hist_data, hist_error = get_historical_data(stock, days)
if not hist_error and hist_data is not None:
symbols_data[symbol] = {
'historical': hist_data
}
if len(symbols_data) > 0:
comparison_chart = create_comparison_chart(symbols_data, selected_range)
st.plotly_chart(comparison_chart, use_container_width=True)
st.info("π Chart shows percentage change from the start of the selected time period, allowing easy comparison of relative performance")
# Export comparison data
st.subheader("πΎ Export Comparison Data")
comparison_csv = comparison_df.to_csv()
st.download_button(
label="π Download Comparison Data (CSV)",
data=comparison_csv,
file_name=f"stock_comparison_{datetime.now().strftime('%Y%m%d')}.csv",
mime="text/csv"
)
else:
st.error("Unable to fetch historical data for comparison")
# Stock screener section
if screen_button:
if not screener_symbols:
st.warning("Please enter stock symbols to screen")
else:
symbols_list = [s.strip().upper() for s in screener_symbols.replace('\n', ',').split(',') if s.strip()]
if len(symbols_list) == 0:
st.warning("Please enter at least one stock symbol to screen")
else:
st.header("π Stock Screener Results")
# Prepare criteria dictionary
criteria = {
'min_pe': min_pe,
'max_pe': max_pe,
'min_market_cap': min_market_cap,
'max_market_cap': max_market_cap,
'min_volume': min_volume,
'min_dividend': min_dividend
}
with st.spinner(f"Screening {len(symbols_list)} stocks..."):
screened_results, failed = screen_stocks(symbols_list, criteria)
if failed:
st.warning(f"β οΈ Unable to fetch data for {len(failed)} symbol(s): {', '.join(failed[:5])}" +
(f" and {len(failed)-5} more" if len(failed) > 5 else ""))
if len(screened_results) == 0:
st.info(f"π No stocks matched the screening criteria out of {len(symbols_list) - len(failed)} stocks analyzed")
# Show criteria summary
st.write("**Applied Criteria:**")
criteria_text = []
if min_pe > 0 or max_pe < 100:
criteria_text.append(f"β’ P/E Ratio: {min_pe} - {max_pe}")
if min_market_cap > 0 or max_market_cap < 10000:
criteria_text.append(f"β’ Market Cap: ${min_market_cap}B - ${max_market_cap}B")
if min_volume > 0:
criteria_text.append(f"β’ Avg Volume: β₯ {min_volume}M")
if min_dividend > 0:
criteria_text.append(f"β’ Dividend Yield: β₯ {min_dividend}%")
for criterion in criteria_text:
st.write(criterion)
else:
st.success(f"β
Found {len(screened_results)} stock(s) matching your criteria out of {len(symbols_list) - len(failed)} analyzed")
# Display results table
results_df = pd.DataFrame(screened_results)
st.dataframe(results_df, width='stretch', hide_index=True)
# Export screener results
st.subheader("πΎ Export Screener Results")
screener_csv = results_df.to_csv(index=False)
st.download_button(
label="π Download Screener Results (CSV)",
data=screener_csv,
file_name=f"stock_screener_{datetime.now().strftime('%Y%m%d')}.csv",
mime="text/csv"
)
# Footer information
st.markdown("---")
st.markdown("""
**Data Source:** Yahoo Finance via yfinance library
**Note:** All financial data is for informational purposes only and should not be considered as investment advice.
""")