使用Python获取股票报价

哈Ha!我向您介绍了Ishan Shah 撰写的文章“ Python中的历史股价数据”的翻译

有关如何使用yfinance获取每日历史股票数据以及如何使用alpha有利于分钟数据的文章。


如您所知,股票是非常波动的工具,在做出任何交易决定之前仔细分析价格行为非常重要。好吧,首先您需要获取数据,而python可以帮助您。

可以使用各种程序包下载Exchange数据。本文将介绍Yahoo Finance和Alpha Vantage。

雅虎财经


首先,尝试yfianance软件包。可以使用pip install yfinance命令安装它。下面的代码显示如何获取2016年至2019年AAPL的数据并在图表上建立调整后的收盘价(股息和拆分的调整后收盘价)。

# Import the yfinance. If you get module not found error the run !pip install yfianance from your Jupyter notebook
import yfinance as yf

# Get the data for 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()

图片

好吧,如果您需要获得多个份额,则需要对代码进行一些补充。DataFrame用于存储值。使用matplotlib软件包和接收到的数据,您可以构建每日获利能力图表。

# 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

for ticker in tickers_list:
    data[ticker] = yf.download(ticker,'2016-01-01','2019-08-01')['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()

图片

对于俄罗斯股票的价值,有一点微妙之处。股票名称上添加了一个点号和大写字母ME。谢谢smartlab提示的专家。

图片

使用Alpha Vantage获得分钟数据


不幸的是,免费版的Yahoo Finance不允许您接收频率低于每日一次的数据。为此,您可以使用Alpha vantage软件包,该软件包使您可以获取诸如1分钟,5分钟,15分钟,30分钟,60分钟之类的间隔。

图片

将来,可以使用pyfolio软件包对这些数据进行分析,创建交易策略并评估有效性。在其中,您可以评估Sharpe比率,Sortino系数,最大下降和许多其他必要指标。

希望我对原始文章的翻译对您有所帮助。该代码已通过验证,一切正常。但是就目前而言,对我来说,问题是在俄罗斯市场上使用Alpha vantage的可能性。

Source: https://habr.com/ru/post/undefined/


All Articles