Files
stock/stockapp/sample/get_yh_kline.py
2024-10-09 11:46:30 +08:00

29 lines
630 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import yfinance as yf
import pandas as pd
code = 'KDP'
# 获取AAPL的股票数据
stock = yf.Ticker(code)
# 获取过去十年的日K线数据前复权
hist_data = stock.history(period="10y", auto_adjust=True)
print (hist_data['Close'].resample('Y').last().pct_change)
# 打印数据前几行
print(hist_data.head())
# 保存到CSV文件
hist_data.to_csv(f"{code}_10year_data_adjust.csv")
# 获取过去十年的日K线数据不复权
hist_data = stock.history(period="10y", auto_adjust=False)
# 打印数据前几行
print(hist_data.head())
# 保存到CSV文件
hist_data.to_csv(f"{code}_10year_data.csv")