Python 无法在numpy数组中循环
我真的很困惑,似乎找不到下面代码的答案。我不断得到以下错误:Python 无法在numpy数组中循环,python,arrays,math,numpy,Python,Arrays,Math,Numpy,我真的很困惑,似乎找不到下面代码的答案。我不断得到以下错误: File "C:\Users\antoniozeus\Desktop\backtester2.py", line 117, in backTest if prices >= smas: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 现在,正如您将在下面看到的代码一样,
File "C:\Users\antoniozeus\Desktop\backtester2.py", line 117, in backTest
if prices >= smas:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
现在,正如您将在下面看到的代码一样,我正在尝试一步一步地比较两个numpy数组,以尝试在满足条件后生成一个信号。这是基于苹果股票数据
从指数[0]开始,然后从[1]开始,每次从一个点开始,如果我的价格大于或等于smas(移动平均值),则会产生一个信号。代码如下:
def backTest():
#Trade Rules
#Buy when prices are greater than our moving average
#Sell when prices drop below or moving average
portfolio = 50000
tradeComm = 7.95
stance = 'none'
buyPrice = 0
sellPrice = 0
previousPrice = 0
totalProfit = 0
numberOfTrades = 0
startPrice = 0
startTime = 0
endTime = 0
totalInvestedTime = 0
overallStartTime = 0
overallEndTime = 0
unixConvertToWeeks = 7*24*60*60
unixConvertToDays = 24*60*60
date, closep, highp, lowp, openp, volume = np.genfromtxt('AAPL2.txt', delimiter=',', unpack=True,
converters={ 0: mdates.strpdate2num('%Y%m%d')})
## FIRST SMA
window = 10
weights = np.repeat(1.0, window)/window
'''valid makes sure that we only calculate from valid data, no MA on points 0:21'''
smas = np.convolve(closep, weights, 'valid')
prices = closep[9:]
for price in prices:
if stance == 'none':
if prices >= smas:
print "buy triggered"
buyPrice = closep
print "bought stock for", buyPrice
stance = "holding"
startTime = date
print 'Enter Date:', startTime
if numberOfTrades == 0:
startPrice = buyPrice
overallStartTime = date
numberOfTrades += 1
elif stance == 'holding':
if prices < smas:
print 'sell triggered'
sellPrice = closep
print 'finished trade, sold for:',sellPrice
stance = 'none'
tradeProfit = sellPrice - buyPrice
totalProfit += tradeProfit
print totalProfit
print 'Exit Date:', endTime
endTime = date
timeInvested = endTime - startTime
totalInvestedTime += timeInvested
overallEndTime = endTime
numberOfTrades += 1
#this is our reset
previousPrice = closep
def backTest():
#贸易规则
#当价格高于我们的移动平均线时买入
#当价格低于或移动平均线时卖出
投资组合=50000
贸易通讯=7.95
站姿=‘无’
买价=0
售价=0
以前的价格=0
总利润=0
交易次数=0
startPrice=0
开始时间=0
结束时间=0
totalInvestedTime=0
总体开始时间=0
总时间=0
unixConvertToWeeks=7*24*60*60
unixConvertToDays=24*60*60
日期,closep,highp,lowp,openp,volume=np.genfromtxt('AAPL2.txt',分隔符=',',unpack=True,
转换器={0:mdates.strpdate2num(“%Y%m%d”)}
##第一SMA
窗口=10
权重=np。重复(1.0,窗口)/窗口
“有效确保我们仅根据有效数据进行计算,0:21点无MA”
smas=np.卷积(闭合,权重,“有效”)
价格=收盘价[9:]
价格中的价格:
如果站姿==“无”:
如果价格>=SMA:
打印“购买触发”
买入价=收盘价
打印“买入股票”,买入价格
stance=“保持”
开始时间=日期
打印“输入日期:”,开始时间
如果numberOfTrades==0:
startPrice=买入价
总开始时间=日期
交易次数+=1
elif站姿==“保持”:
如果价格
我想你是说
if price >= smas
你有
if prices >= smas
一次比较整个列表。你有numpy数组--sma
是一个数组的输出,我相信prices
也是一个数组。使用numpy时,
arr>other\u arr将返回一个没有定义好真值的
ndarray`
您可能想将
price
与smas
中的单个元素进行比较,尽管我不确定哪一个元素(或者np.convolve
将返回哪一个元素,它可能只有一个元素).我想这是一个很好的观点。现在price和sma都有相同长度的元素。我的目标是逐字比较每个元素中的第一个元素,并继续前进,直到我的if语句变为真,然后继续前进,直到我的elif变为真……在SMA变为numpy数组后,是否可以将其转换为不同的格式?在列表和numpy数组中执行相同的逻辑时,我会遇到麻烦吗?@antonio_zeus——列表“按字典顺序”进行比较。e、 g.比较第一个元素,然后是第二个元素,然后是第三个元素,直到其中一个元素不同,并且排序是不同元素比较的结果……您认为我应该为smas创建一个循环,从这个意义上说,我们只比较第X个元素吗?如果是的话,我会怎么做?对不起,我弄糊涂了!!您似乎在代码中有一个bug:在for price in prices
循环中,如果prices>=sma,您就有了——这不起作用,因为prices
是一个列表或数组,因此无法进行比较。我想你是想在那里写price
。