python - Python中标量变量赋值的索引对于浮点错误不清楚
问题描述
我是 Python 新手,想手动编写 VWAP 股票指标。我通过网络下载了一个 JSON,然后尝试创建必要的占位符数组。不确定 totalVolume 数组出了什么问题。
我已将 totalVolume 声明为浮点数组。我仍然在第 81 行遇到标量错误。 totalVolume[i] = volume[i] + totalVolume[i-1] IndexError: invalid index to scalar variable。
import requests
import csv
import os
import threading
import time
import talib
import numpy
import json
from pyalgotrade.technical import vwap
exitFlag = 0
class myThread (threading.Thread):
def __init__(self, threadID, name, counter, url):
# Stock URL https://www.programcreek.com/python/?project_name=doncat99%2FStockRecommendSystem#
#https://www.quantopian.com/posts/sample-talib-macd
#https://www.daytrading.com/vwap
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.counter = counter
def run(self):
print "Starting " + self.name
print_time(self.name, 5, self.counter)
print "Exiting " + self.name
def print_time(threadName, counter, delay):
while counter:
if exitFlag:
threadName.exit()
time.sleep(delay)
print "%s: %s" % (threadName, time.ctime(time.time()))
counter -= 1
def clean(url):
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36", "Referer": "http://example.com"}
r = requests.get(url, headers=headers, timeout=50)
#print(r.content)
data = json.loads(r.content)
#print(data['t'])
close = data['c']
closeArray = numpy.array(close)
#print(close)
RSI = talib.RSI(closeArray, timeperiod=14)
print(RSI[-1])
macd, signal, hist = talib.MACD(closeArray)
msdiff = macd[-1] - signal[-1]
print(msdiff)
getVWAP(data)
def getVWAP(data):
open = numpy.array(data['o'])
low = numpy.array(data['l'])
high = numpy.array(data['h'])
close = numpy.array(data['c'])
volume = numpy.array(data['v'])
print(len(open))
print(len(low))
print(len(high))
print(len(close))
print(len(volume))
typicalPrice = [float]*len(open)
VP = [float]*len(open)
totalVP = [float]*len(open)
VWAP = [float]*len(open)
totalVolume = numpy.zeros(len(open))
for i in range(0, len(open)):
totalVolume[i] = 0
print("A"),
for i in range(0, len(open)):
typicalPrice[i] = (open[i] + high[i] + low[i])/3.0
VP[i] = volume[i] * typicalPrice[i]
if i==0:
totalVP[0] = VP[0]
totalVolume = volume[0]
VWAP[0] = typicalPrice[0]
else:
totalVP[i] = VP[i-1] + volume[i]
totalVolume[i] = volume[i] + totalVolume[i-1]
VWAP[i] = (totalVP[i])/totalVolume[i]
print("@"),
print("Abhi3")
# Create new threads
thread1 = myThread(1, "Thread-1", 1, "")
thread2 = myThread(2, "Thread-2", 2, "")
# Start new Threads
#thread1.start()
#thread2.start()
url1 = "https://tvc4.forexpros.com/b08251ec88a94408cab3716001818cc2/1596166039/56/56/23/history?symbol=18270&resolution=5&from=1595872548&to=1596304608"
url2 = "https://tvc4.forexpros.com/b08251ec88a94408cab3716001818cc2/1596166039/56/56/23/history?symbol=18270&resolution=5&from=1595872548&to=1596304608"
clean(url1)
clean(url2)
print talib.get_functions()
解决方案
def getVWAP(data):
# if data is a pandas dataframe, just use data['o'].values instead
open = numpy.array(data['o'])
low = numpy.array(data['l'])
high = numpy.array(data['h'])
close = numpy.array(data['c'])
volume = numpy.array(data['v'])
# it's better to use logging.debug for debug output
print(len(open), len(low), len(high), len(close), len(volume))
# default dtype is float, but you can be more explicit
typicalPrice = np.zeros(len(open), dtype=float)
# another way to improve this is to use zeros_like,
# which will generalize beyond 1D arrays
VP = np.zeros_like(open)
# but actually, we don't need to initialize these arrays at all
# because we calculate them directly (see below)
# numpy arrays can be added/subtracted/... directly
# it is actually quite a bit faster
typicalPrice = (open + high + low)/3
VP = volume * typicalPrice
totalVolume = np.cumsum(volume)
totalVP = np.cumsum(VP)
VWAP = totalVP/totalVolume
# this line is different. A typo?
VWAP[0] = typicalPrice[0]
推荐阅读
- java - SpringBoot 无法识别 java.lang
- c# - EF6 数据库优先:在没有外键约束的实体之间添加关系
- ruby-on-rails - 为什么 Rails 6 破坏(散列?)分配给名为“video_key”的表单的 hidden_field 的字符串值?
- python - + 是什么意思
+ 在 mysql-python 中表示? - c# - 如何使用 Microsoft Graph 创建 Microsoft Teams 会议作为应用程序?
- mongodb - 将 mongodb 集成到现有 NextJS(TS) 应用程序“找不到模块:无法解析 'dns'”
- c++ - 用虚函数初始化结构
- linux - 如何通过 API 调用为 Binance 上所有可用的 BUSD 购买 BTC?
- r - R和igraph:删除两个值之间权重的边
- r - 用于提取向量百分位数的循环