【发布时间】:2019-04-13 16:27:13
【问题描述】:
我正在尝试复制他在 2013 年作为教程上传的 Sentdex 股票筛选器 (https://www.youtube.com/watch?v=Y4GHgJjIQnk)。不幸的是,从那时起很多事情都发生了变化,因此对他提出的代码进行了细微的调整。我只在下面发布了无法正常工作的代码部分。如果有人感兴趣,可以通过上面的链接获得完整的代码。
代码,基本上是原始代码,除了一些关于 yahoo url 方法的小例外,如下:
import urllib.request
import time
import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
import mpl_finance
from mpl_finance import candlestick_ohlc
import matplotlib
import pylab
...
def graphData(stock,MA1,MA2):
'''
Use this to dynamically pull a stock:
'''
try:
print('Currently Pulling',stock)
urlToVisit = 'https://query1.finance.yahoo.com/v8/finance/chart/'+stock+'?interval=2m'
stockFile =[]
try:
sourceCode = urllib.request.urlopen(urlToVisit).read().decode()
splitSource = sourceCode.split('\n')
for eachLine in splitSource:
splitLine = eachLine.split(',')
if len(splitLine)==6:
if 'values' not in eachLine:
stockFile.append(eachLine)
except Exception as e:
print(str(e), 'failed to organize pulled data.')
except Exception as e:
print(str(e), 'failed to pull pricing data')
try:
date, closep, highp, lowp, openp, volume = np.loadtxt(stockFile,delimiter=',', unpack=True, converters={0: bytespdate2num('%Y%m%d')})
我得到的输出如下:
Currently Pulling ABT
UserWarning: loadtxt: Empty input file: "[]"
date, closep, highp, lowp, openp, volume = np.loadtxt(stockFile,delimiter=',', unpack=True, converters={0: bytespdate2num('%Y%m%d')})
list assignment index out of range
Currently Pulling ABBV
main loop list assignment index out of range
...等所有相关导入后指定的 sp500 列表中的所有代码。
知道有什么问题吗?我试图从中提取信息的链接如下:https://query1.finance.yahoo.com/v8/finance/chart/AAPL?interval=2m
【问题讨论】:
标签: python numpy yahoo-finance