Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
118 lines (97 loc) · 4.92 KB
/
Copy pathmain.py
File metadata and controls
118 lines (97 loc) · 4.92 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
import pandas as pd
from models import StockValuator, BondValuator
def display_menu():
print("\n" + "="*50)
print(" FINANCIAL VALUATION CALCULATOR")
print("="*50)
print("1. Stock Analysis")
print("2. Bond Analysis")
print("3. Exit")
print("="*50)
def stock_analysis_menu():
ticker = input("\nEnter stock ticker (e.g., AAPL): ").strip()
if not ticker: return
print(f"\nFetching data for {ticker}...")
valuator = StockValuator(ticker)
print("... Analyzing quarterly reports ...")
q_df, q_growth, q_count = valuator.get_quarterly_growth()
long_term_cagr, years_count = valuator.get_deep_history_cagr()
print("... Calculating algorithmic discount rate (CAPM) ...")
capm_rate, risk_free, beta = valuator.calculate_capm_discount_rate()
while True:
print(f"\n--- Stock Analysis Menu for {ticker} ---")
print("1. View Growth Analysis (Short vs Long Term)")
print("2. Run Valuation Models (DCF, Graham, Gordon)")
print("3. Return to Main Menu")
choice = input("\nSelect option (1-3): ").strip()
if choice == "1":
print(f"\n{'='*70}\n GROWTH ANALYSIS\n{'='*70}")
label = f"Short Term Momentum ({q_count}-Qtr Avg)" if q_count > 1 else "Short Term Momentum"
print(f"1. {label.upper()}: {q_growth*100:.2f}%")
if q_df is not None:
pd.options.display.float_format = '{:,.2f}'.format
display_q = q_df.copy()
display_q[['Total Revenue', 'Net Income']] /= 1e9
display_q[['Revenue Growth (YoY)', 'Net Income Growth (YoY)']] *= 100
display_q = display_q.fillna('-')
display_q.columns = ['Revenue ($B)', 'Net Inc ($B)', 'Rev Growth %', 'Inc Growth %']
print(display_q[['Revenue ($B)', 'Rev Growth %']])
print(f"\n2. LONG TERM STABILITY ({years_count}-Year CAGR): {long_term_cagr*100:.2f}%")
print(f" (Source: StockAnalysis.com Scraper)")
print(f"{'='*70}")
elif choice == "2":
s_discount = round(capm_rate, 3)
print(f"\n--- Model Assumptions ---")
print(f"1. GROWTH RATE SELECTION")
print(f" [A] Use Short-Term Momentum: {q_growth*100:.2f}%")
print(f" [B] Use {years_count}-Year CAGR: {long_term_cagr*100:.2f}%")
print(f" [C] Enter Custom Rate")
g_choice = input(" Select [A], [B], or [C]: ").strip().upper()
if g_choice == 'A': growth = q_growth
elif g_choice == 'B': growth = long_term_cagr
else:
g_in = input(f" Enter custom rate (e.g., 0.12): ").strip()
growth = float(g_in) if g_in else 0.05
if growth > 0.5: growth /= 100
print(f"\n2. DISCOUNT RATE (Risk)")
print(f" • Risk-Free Rate: {risk_free*100:.2f}%")
print(f" • Beta (Volatility): {beta:.2f}")
print(f" • Calculated CAPM: {s_discount*100:.1f}%")
d_in = input(f" Enter to use {s_discount*100:.1f}%, or type value: ").strip()
discount = float(d_in) if d_in else s_discount
if discount > 0.5: discount /= 100
price = valuator.get_current_price()
dcf = valuator.calculate_dcf_value(growth, discount)
graham = valuator.calculate_graham_number()
gordon = valuator.calculate_gordon_growth_value(discount, 0.03)
print(f"\n{'='*70}\n VALUATION RESULTS\n{'='*70}")
print(f"Market Price: ${price}")
print(f"DCF Intrinsic: ${dcf} (Inputs: g={growth*100:.1f}%, r={discount*100:.1f}%)")
print(f"Graham Number: ${graham}")
print(f"Gordon Growth: ${gordon}")
if dcf:
diff = ((dcf - price) / dcf) * 100
status = "UNDERVALUED" if diff > 0 else "OVERVALUED"
print(f"\nResult: {status} by {abs(diff):.1f}%")
print(f"{'='*70}")
elif choice == "3": break
def bond_analysis_menu():
print("\n--- Bond Analysis ---")
try:
F = float(input("Face Value: "))
r = float(input("Coupon Rate (decimal): "))
n = int(input("Years: "))
i = float(input("Market Rate (decimal): "))
freq = 2 if input("Freq (1=Ann, 2=Semi): ") == '2' else 1
bond = BondValuator(F, r, n, i, freq)
print(f"Price: ${bond.calculate_price()}")
print(f"Duration: {bond.calculate_macaulay_duration()} years")
input("Press Enter...")
except ValueError: print("Invalid Input")
if __name__ == "__main__":
while True:
display_menu()
c = input("\nOption: ").strip()
if c == "1": stock_analysis_menu()
elif c == "2": bond_analysis_menu()
elif c == "3": break