测试一些python获取股票数据的方法2023.7.18

2023-08-16 21:45:40 浏览数 (1)

1、可以yfinance

https://github.com/ranaroussi/yfinance

https://aroussi.com/post/python-yahoo-finance

代码语言:javascript复制
import yfinance as yf
import datetime

# start=datetime.datetime(2023, 6, 1)
# end=datetime.datetime(2023, 7, 17)

# data = yf.download('AAPL', start=start, end=end)
# print(data)

tickers = ['AAPL', 'IBM', 'MSFT', 'GOOG']
all_data = {}

for ticker in tickers:
    all_data[ticker] = yf.download(ticker, start='2023-7-1', end='2023-7-17')
    print(all_data) 

2、

代码语言:javascript复制
import yfinance as yf

# 定义开始和结束日期
start_date = '2019-09-10'
end_date = '2019-10-09'

# 使用yfinance从Yahoo Finance获取股票数据
df = yf.download('GE', start=start_date, end=end_date)

# 打印获取到的数据的前5行
print(df.head())

3、

代码语言:javascript复制
import pandas_datareader as pdr
pdr.get_data_fred('GS10')

4、

代码语言:javascript复制
import yfinance as yf
data = yf.download("SPY AAPL", start="2017-01-01", end="2017-04-30")
print(data)

5、

代码语言:javascript复制
import yfinance as yf
data = yf.download("SPY AAPL", start="2017-01-01", end="2017-04-30",
                   group_by="ticker")
print(data)

6、

代码语言:javascript复制
from pandas_datareader import data as pdr

import yfinance as yf
yf.pdr_override() # <== that's all it takes :-)

# download dataframe using pandas_datareader
data = pdr.get_data_yahoo("SPY", start="2017-01-01", end="2017-04-30")
print(data)

7、获取贵州茅台

代码语言:javascript复制
import yfinance as yf
data = yf.download("600519.ss", start="2023-07-01", end="2023-07-30")
print(data)

8、能获取上证最新数据

代码语言:javascript复制
import yfinance as yf
data = yf.download("688300.ss", start="2023-07-01", end="2023-07-30")
print(data)

9、不行了

代码语言:javascript复制
import pandas_datareader as web 
#载入数据,雅虎网中的601318.ss股票,从2020-01-01到2020-03-18的数据
start_date='2023-01-01'
end_date='2023-03-18'
data=web.data.DataReader('603888.ss','yahoo',start_date,end_date)
data.head()

10、

代码语言:javascript复制
import yfinance as yf
import plotly.graph_objects as go
 
# 检索AAPL历史数据
symbol = "AAPL"
ticker = yf.Ticker(symbol)
data = ticker.history(period="1mo")
 
# 创建candlestick chart
fig = go.Figure(data=[go.Candlestick(x=data.index,
                                     open=data['Open'],
                                     high=data['High'],
                                     low=data['Low'],
                                     close=data['Close'])])
 
# 自定义图表布局
fig.update_layout(title=f"{symbol} Candlestick Chart (1 Month)",
                  yaxis_title="Price",
                  xaxis_rangeslider_visible=False)
 
# 显示图表
fig.show()

11、

代码语言:javascript复制
import yfinance as 财经
import plotly.graph_objects as 图像对象
from plotly.io import write_html

# 检索AAPL历史数据
股票符号 = "AAPL"
股票数据 = 财经.Ticker(股票符号)
历史数据 = 股票数据.history(period="1mo")

# 创建蜡烛图
图表 = 图像对象.Figure(data=[图像对象.Candlestick(x=历史数据.index,
                                                open=历史数据['Open'],
                                                high=历史数据['High'],
                                                low=历史数据['Low'],
                                                close=历史数据['Close'])])

# 自定义图表布局
图表.update_layout(title=f"{股票符号} 蜡烛图 (1 Month)",
                  yaxis_title="价格",
                  xaxis_rangeslider_visible=False)

# 保存图表为HTML网页
write_html(图表, '蜡烛图.html')

12、

代码语言:javascript复制
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 18 16:07:21 2023

@author: Administrator
"""
import yfinance as 财经
import plotly.graph_objects as 图像对象
from plotly.io import write_html

# 检索AAPL历史数据
股票符号 = "600519.ss"
股票数据 = 财经.Ticker(股票符号)
历史数据 = 股票数据.history(period="1mo")

# 创建蜡烛图
图表 = 图像对象.Figure()

图表.add_trace(图像对象.Candlestick(x=历史数据.index,
                                   open=历史数据['Open'],
                                   high=历史数据['High'],
                                   low=历史数据['Low'],
                                   close=历史数据['Close'],
                                   increasing_line_color='red', 
                                   decreasing_line_color='green'))

# 添加开盘价的数值
图表.add_trace(图像对象.Scatter(x=历史数据.index, y=历史数据['Open'],
                               mode='markers text',
                               text=历史数据['Open'].round(2),
                               textposition='bottom center'))

# 添加最高价的数值
图表.add_trace(图像对象.Scatter(x=历史数据.index, y=历史数据['High'],
                               mode='markers text',
                               text=历史数据['High'].round(2),
                               textposition='top center'))

# 添加最低价的数值
图表.add_trace(图像对象.Scatter(x=历史数据.index, y=历史数据['Low'],
                               mode='markers text',
                               text=历史数据['Low'].round(2),
                               textposition='bottom center'))

# 添加收盘价的数值
图表.add_trace(图像对象.Scatter(x=历史数据.index, y=历史数据['Close'],
                               mode='markers text',
                               text=历史数据['Close'].round(2),
                               textposition='top center'))

# 自定义图表布局
图表.update_layout(title=f"{股票符号} 蜡烛图 (1 Month)",
                  yaxis_title="价格",
                  xaxis_rangeslider_visible=False)

# 保存图表为HTML网页
write_html(图表, '蜡烛图.html')

0 人点赞