首頁  >  文章  >  後端開發  >  使用Python獲取股票數據的最佳方法是什麼?

使用Python獲取股票數據的最佳方法是什麼?

WBOY
WBOY轉載
2023-08-26 13:41:052861瀏覽

使用Python獲取股票數據的最佳方法是什麼?

在本文中,我們將學習使用 Python 取得股票資料的最佳方法。

yfinance Python 庫將用於從雅虎財經檢索當前和歷史股票市場價格資料。

安裝雅虎財經(yfinance)

取得股票市場數據的最佳平台之一是雅虎財經。只需從雅虎財經網站下載資料集並使用 yfinance 庫和 Python 編程即可存取它。

您可以在 pip 的幫助下安裝 yfinance,您所要做的就是打開命令提示字元並鍵入以下命令顯示語法:

文法

pip install yfinance

yfinance 函式庫最好的部分是,它可以免費使用,而且不需要 API 金鑰

如何取得目前股票價格資料

我們需要找到可用於資料擷取的股票代碼。我們將展示 以下範例中 GOOGL 的當前市場價格和先前收盤價。

範例

以下程式回傳市場價格值、前收盤價值、股票代碼 使用 yfinance 模組的值 -

import yfinance as yf
ticker = yf.Ticker('GOOGL').info
marketPrice = ticker['regularMarketPrice']
previousClosePrice = ticker['regularMarketPreviousClose']
print('Ticker Value: GOOGL')
print('Market Price Value:', marketPrice)
print('Previous Close Price Value:', previousClosePrice)

輸出

執行時,上述程式將產生以下輸出 -

Ticker Value: GOOGL
Market Price Value: 92.83
Previous Close Price Value: 93.71

如何取得股票價格的歷史資料

透過給出開始日期、結束日期和程式碼,我們可以獲得完整的歷史價格資料。

範例

以下程式傳回開始日期和結束日期之間的股票價格資料 -

# importing the yfinance package
import yfinance as yf

# giving the start and end dates
startDate = '2015-03-01'
endDate = '2017-03-01'

# setting the ticker value
ticker = 'GOOGL'

# downloading the data of the ticker value between
# the start and end dates
resultData = yf.download(ticker, startDate, endDate)

# printing the last 5 rows of the data
print(resultData.tail())

輸出

執行時,上述程式將產生以下輸出 -

[*********************100%***********************] 1 of 1 completed
            Open      High     Low       Close     Adj Close Volume
Date
2017-02-22 42.400002 42.689499 42.335499 42.568001 42.568001 24488000
2017-02-23 42.554001 42.631001 42.125000 42.549999 42.549999 27734000
2017-02-24 42.382500 42.417999 42.147999 42.390499 42.390499 26924000
2017-02-27 42.247501 42.533501 42.150501 42.483501 42.483501 20206000
2017-02-28 42.367500 42.441502 42.071999 42.246498 42.246498 27662000

上面的範例將檢索2015-03-01到2017-03-01的股票價格資料。

如果您想同時從多個程式碼中提取數據,請以空格分隔的字串形式提供代碼。

轉換資料進行分析

Date 是資料集的索引,而不是上面範例中資料集的欄位。在對其執行任何資料分析之前,必須將此索引轉換為列。以下是如何做到這一點 -

範例

以下程式將列名稱新增至開始日期和結束日期之間的股票資料 -

import yfinance as yf

# giving the start and end dates
startDate = '2015-03-01'
endDate = '2017-03-01'

# setting the ticker value
ticker = 'GOOGL'

# downloading the data of the ticker value between
# the start and end dates
resultData = yf.download(ticker, startDate, endDate)

# Setting date as index
resultData["Date"] = resultData.index

# Giving column names
resultData = resultData[["Date", "Open", "High","Low", "Close", "Adj Close", "Volume"]]

# Resetting the index values
resultData.reset_index(drop=True, inplace=True)

# getting the first 5 rows of the data
print(resultData.head())

輸出

執行時,上述程式將產生以下輸出 -

[*********************100%***********************] 1 of 1 completed
   Date      Open       High     Low       Close     Adj Close  Volume

0 2015-03-02 28.350000 28.799500 28.157499 28.750999 28.750999 50406000
1 2015-03-03 28.817499 29.042500 28.525000 28.939501 28.939501 50526000
2 2015-03-04 28.848499 29.081499 28.625999 28.916500 28.916500 37964000
3 2015-03-05 28.981001 29.160000 28.911501 29.071501 29.071501 35918000
4 2015-03-06 29.100000 29.139000 28.603001 28.645000 28.645000 37592000

以上轉換後的資料與我們從雅虎財經取得的資料是相同的

將取得的資料儲存在CSV檔案中

to_csv()方法可用來將DataFrame物件匯出到CSV檔案。以下程式碼將幫助您匯出CSV檔案中的數據,因為上面轉換的資料已經在pandas 資料框中。

# importing yfinance module with an alias name
import yfinance as yf

# giving the start and end dates
startDate = '2015-03-01'
endDate = '2017-03-01'

# setting the ticker value
ticker = 'GOOGL'

# downloading the data of the ticker value between
# the start and end dates
resultData = yf.download(ticker, startDate, endDate)

# printing the last 5 rows of the data
print(resultData.tail())

# exporting/converting the above data to a CSV file
resultData.to_csv("outputGOOGL.csv")

輸出

執行時,上述程式將產生以下輸出 -

[*********************100%***********************] 1 of 1 completed
            Open      High     Low       Close     Adj Close  Volume

Date
2017-02-22 42.400002 42.689499 42.335499 42.568001 42.568001 24488000
2017-02-23 42.554001 42.631001 42.125000 42.549999 42.549999 27734000
2017-02-24 42.382500 42.417999 42.147999 42.390499 42.390499 26924000
2017-02-27 42.247501 42.533501 42.150501 42.483501 42.483501 20206000
2017-02-28 42.367500 42.441502 42.071999 42.246498 42.246498 27662000

可視化資料

yfinance Python 模組是最容易設定、收集資料和執行資料分析活動的模組之一。使用 Matplotlib、Seaborn 或 Bokeh 等軟體包,您可以視覺化結果並捕獲見解。

您甚至可以使用 PyScript 直接在網頁上顯示這些視覺化效果。

結論

在本文中,我們學習如何使用Python yfinance模組來取得最佳股票資料。此外,我們還學習如何取得指定時間段內的所有股票數據,如何透過新增自訂索引和列進行數據分析,以及如何將這些數據轉換為 CSV 檔案。

以上是使用Python獲取股票數據的最佳方法是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除