Discounted cash flow valuation · Python
DCF Automated Code
An interactive Python model. The user enters a ticker and their yearly rate assumptions, and it pulls live financials and returns a full DCF valuation — WACC, growth, intrinsic value and verdict — with no spreadsheet.
- Python
- NumPy
- yfinance
- Valuation
- Automation
- Role
- Sole developer — conceived, built and tested it end to end
- Year
- Winter 2025
- Language
- Python
- Libraries
- yfinance · NumPy · tabulate · Matplotlib
- User inputs
- Ticker name · yearly rates
- Data source
- Yahoo Finance
User inputs
Ticker + rates
entered when the program runs
Tunable assumptions
12
keyword arguments with defaults
Default forecast
10years
Time saved per company
75%
vs. gathering the data by hand
Interactive — Live model
Clamped to the model’s growth caps
Computed from market data in the real model
- WACC
- 9.10%
- EV / base FCF
- 18.7×
- From terminal
- 57%
WACC must exceed terminal growth — the model raises a ValueError here.
| Year | FCF | PV |
|---|
Problem
Building a discounted cash flow model by hand is slow: you have to gather the financial data, build the model, and apply assumptions — then do it all again for the next company.
Just gathering the inputs took 2–3 minutes per company, pulling numbers from several different websites before any modeling could start. There had to be an easier way: pull all of the data at once, quickly, for any company.
The project started as a valuation of FWONK (Formula One Group) and was expanded into a general tool that works for any publicly traded company.
Constraints
- Free, public data only. Everything comes from Yahoo Finance through
yfinance, so field names vary between companies and values can be missing. - Noisy cash-flow history. A single unusual year — or a negative free cash flow — can wreck a naive growth estimate (and negative values break logarithms).
- The math has hard limits. The Gordon growth terminal value is only defined when WACC exceeds the terminal growth rate.
- Simple for the user. Anyone should be able to run it by typing a ticker and their rate assumptions, without editing code or a spreadsheet.
Approach
The program takes user input. When it runs, it asks for the ticker name and the yearly rates the valuation uses (such as the effective tax rate), plus whether to print the year-by-year projection. Every input is validated, and the program re-prompts until it gets a usable value.
From there, a single entry point, dcf_model(), runs a five-stage pipeline:
- Fetch — pull price, diluted shares, total debt, cash, beta, market cap and the cash-flow statement. Free cash flow per year = operating cash flow + capital expenditure (capex is reported negative). Row lookup is case-insensitive and tolerant of naming differences.
- Growth — fit a log-linear regression through every positive FCF year instead of using only the endpoints, so one outlier year can’t dominate. The slope is converted to an annual rate and clamped to
[growth_cap_min, growth_cap_max](−10 % … +15 % by default). Users can bypass it withgrowth_override. - WACC — cost of equity from CAPM, after-tax cost of debt from the risk-free rate plus a credit spread, weighted by market debt and equity rather than a fixed assumption.
- DCF — project FCF over the forecast horizon, discount each year, add a discounted Gordon-growth terminal value, subtract net debt, divide by shares.
- Report — formatted assumption and summary tables, an optional year-by-year table, a verdict (under/over-valued by X %) and a price-vs-value chart.
cost_of_equity = rfr + β · mrp
after_tax_cod = (rfr + implied_spread) · (1 − effective_tax)
WACC = E/V · cost_of_equity + D/V · after_tax_cod
FCF_t = FCF_0 · (1 + g)^t t = 1 … N
TV = FCF_N · (1 + g_terminal) / (WACC − g_terminal)
EV = Σ FCF_t / (1 + WACC)^t + TV / (1 + WACC)^N
value / share = (EV − net debt) / diluted shares
The calculator above runs this exact math in your browser — move the sliders to see how sensitive the result is to growth, beta and terminal growth.
Analysis & Results
Example run: NVIDIA (NVDA)
| Input / output | Value |
|---|---|
| Current price | USD 201.68 |
| Beta | 2.33 |
| Effective tax rate (user input) | 25% |
| Forecast horizon | 20 years |
| Cost of equity · WACC | 15.78% · 15.75% |
| FCF growth (from history, capped) | 15.0% |
| Terminal growth | 2.50% |
| Base FCF (most recent) | $96.68B |
| PV of projected FCF · PV of terminal value | $1.81T · $656.69B |
| Enterprise value · equity value | $2.46T · $2.52T |
| Intrinsic value per share | USD 103.48 |
| Verdict | Overvalued by ≈ 48.7% |
NVDA’s high beta (2.33) pushes the cost of equity, and with it the WACC, to about 15.8%. Even with FCF growing at the 15% cap for 20 years, the discounted value comes out near half the market price. Run on April 18, 2026; market data as of that date.
Sensitivity
With the default 10-year horizon, most of the enterprise value comes from the terminal value (≈ 57% in the demo above), so terminal growth and WACC move the answer most. Over the 20-year horizon in the NVDA run, the terminal value is only ≈ 27% of enterprise value, so the result leans on the projected cash flows and the growth rate instead.
Outcome
- Time saved: about 75% per company compared with gathering the data by hand.
- Real use: I use the model myself to guide my money-management decisions, and it has helped me grow my investments by 110%.
A DCF is an estimate built on assumptions, not financial advice.
What I’d do next
- Compare stocks against each other. Value several tickers in one run (for example, everyone in a sector) and rank them by the gap between intrinsic value and price, WACC and growth.
- Show a range, not a single number. Add a WACC × terminal-growth sensitivity table and bull / base / bear scenarios.
- Be clear about time horizon. A DCF values a company over years, so it suits long-term investing, not day trading. The report should say so, and could flag stocks where short-term price momentum disagrees with the long-run value.
- Backtest it. Run the model on past data and check how its “undervalued” calls performed afterwards.
- Cross-check with relative valuation. Compare P/E and EV/EBITDA against peers alongside the DCF.
- Make it shareable. Wrap it in a small web app so others can enter a ticker without running Python.
Gallery — 2 figures
FIG. 01NVDA run — assumptions and 20-year FCF projection. FIG. 02DCF summary and plot_dcf() — overvalued by 48.7%.