python研究股价_如何使用Python下载股价数据?

雅虎金融是获取股票数据的免费来源之一。您可以使用pandas数据读取器获取数据,也可以使用yfinance库获取数据。从yfinance库获取数据的方法如下所示。# Import the yfinance. If you get module not found error the run !pip install yfiannce from your Jupyter notebook

import yfinance as yf

# Get the data of the stock AAPL

data = yf.download('AAPL','2016-01-01','2019-08-01')

# Import the plotting library

import matplotlib.pyplot as plt

%matplotlib inline

# Plot the close price of the AAPL

data['Adj Close'].plot()

plt.show()

量子

Wiki是quandl上获取3000多只美国股票数据的免费来源之一。这是社区维护的数据。最近它被停止维护,但是,它是一个很好的免费资源来回溯测试你的策略。

要获取数据,您需要从quandl获取免费的API密钥,并用API密钥替换下面代码中的。# Import the quandl package

import quandl

# Get the data from quandl

data = quandl.get("WIKI/KO", start_date="2016-01-01", end_date="2018-01-01", api_key=)

# Plot the close pr

import matplotlib.pyplot as plt

data.Close.plot()

# Show the plot

plt.show()

注:

Quandl要求NumPy(v1.8或更高版本)和熊猫(v0.14或更高版本)工作。

要获取API密钥,请注册一个免费的Quandl帐户。然后,您可以在Quandl帐户设置页面上找到您的API密钥。

获取多个股票的数据# Define the ticker list

import pandas as pd

tickers_list = [‘AAPL’, ‘WMT’, ‘IBM’, ‘MU’, ‘BA’, ‘AXP’]

# Import pandas

data = pd.DataFrame(columns=tickers_list)

# Fetch the data

import yfinance as yf

for ticker in tickers_list:

data[ticker] = yf.download(ticker, ‘2015–1–1’, ‘2019–1–1’)[‘Adj Close’]

# Print first 5 rows of the data

data.head()

# Plot all the close prices

((data.pct_change()+1).cumprod()).plot(figsize=(10, 7))

# Show the legend

plt.legend()

# Define the label for the title of the figure

plt.title(“Adjusted Close Price”, fontsize=16)

# Define the labels for x-axis and y-axis

plt.ylabel(‘Price’, fontsize=14)

plt.xlabel(‘Year’, fontsize=14)

# Plot the grid lines

plt.grid(which=”major”, color=’k’, linestyle=’-.’, linewidth=0.5)

plt.show()

如果您正在查看分钟级别或基本数据,那么this页可能会有所帮助。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值