使用Pandas Datareader从多个来源读取股票 [英] Reading stocks from multiple sources using Pandas Datareader

查看:644
本文介绍了使用Pandas Datareader从多个来源读取股票的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有6只股票的清单.我已经设置了代码以引用列表中的股票名称,而不是使用股票名称中的硬编码……首先使用SPY位置0.列表下方的代码将返回昨天的股票收盘价.

I have a list of 6 stocks. I have set up my code to reference the stock name from the list vs hard coding in the stock name ... first with SPY which is in position 0. The code below the list will return yesterday's closing price of stock.

我的问题是:如何遍历列表中的每只股票,以便打印出所有6只股票的收盘价?

My question is: how do I loop the code through each stock in the list so that I print out the closing price for all 6 stocks?

我认为我需要使用循环,但是我不理解它们.

I think I need to use loops but I don't understand them.

有什么想法吗? 代码:

Any ideas? CODE:

#import packages

import pandas_datareader.data as web
import datetime as dt

#create list of stocks to reference later

stocks = ['SPY', 'QQQ', 'IWM', 'AAPL', 'FB', 'GDX']

#define prior day close price

start = dt.datetime(2010, 1, 1)
end = dt.datetime(2030, 1, 27)
ticker = web.DataReader(stocks[0], 'google', start, end)
prior_day = ticker.iloc[-1] 
PDL = list(prior_day)
prior_close = PDL[3]
#print the name of the stock from the stocks list, and the prior close price

print(stocks[0])
print('Prior Close')
print(prior_close)

退货:

SPY
Prior Close
249.08

推荐答案

您可以只使用for循环

for stock in stocks:
    start = dt.datetime(2010, 1, 1)
    end = dt.datetime(2030, 1, 27)
    ticker = web.DataReader(stock, 'google', start, end)
    prior_day = ticker.iloc[-1] 
    PDL = list(prior_day)
    prior_close = PDL[3]

    print(stock)
    print('Prior Close')
    print(prior_close)

这篇关于使用Pandas Datareader从多个来源读取股票的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆