TradeBot Check
Trading involves a risk of losing money. Nothing on this site is financial advice and past results do not guarantee future results. Risk disclosure
Guide · 2026-11-09

How to download historical stock prices to a CSV file with Python (yfinance)

Before you can test an idea you need price history. With a few lines of Python you can download it for free and keep it in a CSV file you can open in a spreadsheet or feed to a backtester. This guide uses yfinance, a popular open source library.

What you need

  • Python installed on your computer.
  • yfinance, free and open source (Apache-2.0). Install it with pip install yfinance.

Step by step

  1. Pick a symbol. Use the ticker as shown on Yahoo Finance, for example MSFT.
  2. Download one month of history. The official quick start uses:
    import yfinance as yf
    dat = yf.Ticker("MSFT")
    df = dat.history(period="1mo")
  3. Save it as CSV: df.to_csv("msft.csv"). The file has one row per day with open, high, low, close and volume.
  4. Several symbols at once. The docs show yf.download(['MSFT', 'AAPL', 'GOOG'], period='1mo'), which returns one table you can also save with to_csv.
  5. Refresh it on a schedule with cron or your task scheduler, so the file stays up to date.

Ideas for what to do next

  • Open the CSV in a spreadsheet and chart the closing price.
  • Draw candlesticks with volume and moving averages from the same data using mplfinance.
  • Feed the data to Backtesting.py to test a simple rule in simulation.
  • Get a Telegram message when a price crosses a level, as in our daily price summary guide.
  • Add economic context such as rates or inflation series with the FRED API.

Other ways to do it

If you want a data provider with its own account and documented terms, compare Twelve Data, EODHD and Tiingo. For stock quotes, fundamentals and calendars through an API key, see Finnhub, Alpha Vantage and Massive. Check each provider's official page for current limits. To go from prices to ratios and statements, see FinanceToolkit.

Good to know

yfinance is not affiliated with or endorsed by Yahoo. Its documentation says it is intended for research and educational purposes and that the Yahoo Finance API is for personal use only, so read Yahoo's terms. Because it is not an official API, it can stop working when Yahoo changes something. Use it for learning and personal projects, and any example you test is illustrative: it never promises returns. See also how to backtest a strategy properly.

See more ideas

Browse the use cases or the tool directory. Custom bots are coming soon: tell me when it opens.

Before you start

What does yfinance cost and what licence does it use?

Free (open source). Licence: Apache-2.0. Prices change, so check the official website before you decide.

What should I know before using yfinance?

Not affiliated with or endorsed by Yahoo. Its own documentation says the tool is intended for research and educational purposes and that the Yahoo Finance API is for personal use only: read their terms. Since it is not an official API, it can stop working whenever Yahoo changes something.

What does Backtesting.py cost and what licence does it use?

Free (open source). Licence: AGPL-3.0. Prices change, so check the official website before you decide.

What should I know before using Backtesting.py?

AGPL-3.0 license: if you use it inside a web service offered to others, you must publish your code.

What does Tiingo cost and what licence does it use?

Starter free; Power $30/mo or $300/yr for individuals ($50/mo or $499/yr for internal commercial use). Licence: Commercial API. Prices change, so check the official website before you decide.

What should I know before using Tiingo?

Fundamental history is 5 years on free (15+ on Power); fundamental data through the API is a separate add-on: contact Tiingo. Commercial use costs more than personal use.

Tools in this guide

  • yfinance: Python library to download prices and history from Yahoo Finance; widely used for research and learning.
  • Backtesting.py: Lightweight Python library to backtest a strategy on one asset with very little code.
  • Tiingo: Affordable financial data API with 30+ years of end-of-day prices on 110,000+ securities, fundamentals, crypto and news.
  • FinanceToolkit: Open-source Python toolkit that turns company data into financial statements, ratios, technical indicators and risk metrics, with every formula written in readable code.
  • mplfinance: Python package of matplotlib utilities to plot candlestick, OHLC, Renko and Point and Figure charts with volume and moving averages from a pandas DataFrame.

Compare your options

Want a bot like this when custom bots open? Get notified · more bot ideas

Coming soon

Coming soon: custom bots

We are preparing a service to code, backtest and automate trading and statistics bots to your own rules. It is not available yet: follow our Telegram channel to know when it opens.

Tools mentioned in this guide

More guides