来自Sentdex的股票筛选器

问题描述 投票:2回答:1

我试图复制他在2013年作为教程上传的Sentdex股票筛选器(https://www.youtube.com/watch?v=Y4GHgJjIQnk)。不幸的是,从那时起,很多事情发生了变化,因此对他提出的代码进行了微小的调整。我发布的代码部分只是在下面没有正常工作。如果有人感兴趣,可以在上面的链接中找到整个代码。

代码,基本上是原始的代码,有关雅虎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
1个回答
3
投票

问题在于您处理来自Yahoo的响应的方式,更确切地说:

splitSource = sourceCode.split('\n')它没有像您想象的那样,处理输出的最佳和最简单的方法是使用json.loads(),因为Yahoo输出已经在json格式下:

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, json
...

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'
        print(urlToVisit)
        stockFile =[]
        try:
            sourceCode = urllib.request.urlopen(urlToVisit).read().decode()
            sourceCode = json.loads(sourceCode)
            print(sourceCode['chart']['result'][0]['indicators']['quote'][0].keys())
            #dict_keys(['low', 'close', 'open', 'high', 'volume'])
...

只需浏览嵌套字典json.loads返回,您就可以按照自己喜欢的顺序获取任何数据:

stock = "FB"
print('Currently Pulling',stock)
urlToVisit = 'https://query1.finance.yahoo.com/v8/finance/chart/'+stock+'?interval=2m'
print(urlToVisit)
stockFile =[]
sourceCode = urllib.request.urlopen(urlToVisit).read().decode()
sourceCode = json.loads(sourceCode)
resp_ = {}
resp_ = sourceCode['chart']['result'][0]['indicators']['quote'][0]
resp_['timestamp'] = sourceCode['chart']['result'][0]['timestamp']
print(resp_.keys())
#dict_keys(['close', 'open', 'timestamp', 'volume', 'high', 'low'])

以及每个值的时间戳。

© www.soinside.com 2019 - 2024. All rights reserved.