Coursera 笔记

Module 01 · Crowdsourcing Short squeeze Dashboard

Reading: yfinance Library阅读:yfinance 库

Coursera ↗

The yfinance is a Python library with a user-friendly interface for downloading historical market data from Yahoo Finance. It lets you get historical stock prices, dividends, and other financial data for stocks, exchange-traded funds (ETFs), and other securities.
yfinance 是一个具有用户友好界面的 Python 库,用于从 Yahoo Finance 下载历史市场数据。它允许您获取股票、交易所交易基金(ETF)和其他证券的历史股价、股息和其他财务数据。

This example shows code for using yfinance to download historical stock prices.
此示例展示了使用 yfinance 下载历史股价的代码。

import yfinance as yf

# Download historical data for a stock
msft = yf.Ticker("MSFT")
msft_data = msft.history(period="max")

# Display the downloaded data
msft_data.head()

Explanation for the above code:
上述代码的解释:

  • First, import the yfinance library using the alias yf.
    首先,使用别名导入 yfinance 库。
  • Then, create a Ticker object for the Microsoft stock (“MSFT”).
    然后,为微软股票(“MSFT”)创建一个 Ticker 对象。
  • Use the history method of the Ticker object to download the historical data for the stock. The period parameter of the history method specifies when you want to download the data. In this example, it is set to max to download the maximum available historical data.
    使用 Ticker 对象的 history 方法下载该股票的历史数据。 period 方法的 history 参数指定您想要下载数据的时间。在这个例子中,它被设置为 max 以下载最大可用的历史数据。

Here are some of the possible values for the period parameter and what they represent:
以下是 period 参数的一些可能值及其含义:

  • period="1d": Download 1 day of historical data.
    周期="1d":下载 1 天的历史数据。
  • period="5d": Download 5 days of historical data.
    周期="5d":下载 5 天的历史数据。
  • period="1mo": Download 1 month of historical data.
    周期="1mo":下载 1 个月的历史数据。
  • period="3mo": Download 3 months of historical data.
    周期="3mo":下载 3 个月的历史数据。
  • period="6mo": Download 6 months of historical data.
    period="6mo": 下载 6 个月的历史数据。
  • period="1y": Download 1 year of historical data.
    period="1y": 下载 1 年的历史数据。
  • period="2y": Download 2 years of historical data.
    period="2y": 下载 2 年的历史数据。
  • period="5y": Download 5 years of historical data.
    period="5y": 下载 5 年的历史数据。
  • period="10y": Download 10 years of historical data.
    period="10y":下载 10 年的历史数据。
  • period="ytd": Download historical data since the beginning of the current year.
    period="ytd":下载自当前年初的历史数据。
  • period="max": Download all available historical data.
    period="max":下载所有可用的历史数据。

Finally, you print the downloaded data using the head function. This downloaded data will display a Pandas data frame containing Microsoft's historical stock prices and other financial data.
最后,您使用 head 函数打印下载的数据。这些下载的数据将显示一个包含微软历史股价和其他财务数据的 Pandas 数据框。