【发布时间】:2015-11-29 07:54:22
【问题描述】:
编辑2: 感谢您的回复,我已经进一步了解:
buyChange = np.where(np.diff(buy>0)!=0)
sellChange = np.where(np.diff(sell>0)!=0)
现在我有 2 个索引数组,一个在买入逻辑交叉处,一个在卖出逻辑交叉处:
购买:(数组([ 3, 5, 8, 9, 14, 15, 17, 19, 20, 26, 27, 33、39、41、46、47、51、60、61、62、70、71、 75、76、77、78、80、81、83、88、90、97、100...
卖出:(数组([ 22, 34, 54, 63, 85, 88, 89, 102, 103, 110, 111...
我现在需要的是代表买卖对的索引对。当买入越过那是一对的开始,然后卖出数组中第一个较高的数字是该买入/卖出对中的第二个。然后在最后一个卖出之上的买入数组中的下一个最高值是下一对的开始。对于上述数组,它将是 (3,22)(26,34)(39,54)(60,63)(70,85)(88,89)。然后我可以在这段代码中使用这些索引来找到我将在回溯测试中交易的相应开盘价:
价格 = o[buyChange[0][index]+2]
\EDIT2
编辑: 我发现这个函数可以找到数组中为负数的部分,但现在我必须找到该部分开头的那一天,即数组从正数变为负数的那一天。有人可以帮忙吗?
buys = np.where( buy < 0 )
类似下面的东西不起作用,但这是我想要实现的想法:
buys = np.where( buy < 0 and buy[-1] > 0 )
或:
buys = np.where( buy[1:] < 0 and buy[:-1] > 0 )
/编辑
我有一个 for 循环,它遍历股票文件中的数组和确定某行是买入还是卖出的 if 语句。我正在寻找一种方法来矢量化此操作,可能是创建一个仅包含买入/卖出天数的列表,然后我可以计算我的回报。实际上是信号日之后的日子列表,因为我在第二天开盘交易。这是我的程序的主要内容:
for stock in files:
d,c,h,l,o,v = getData(str(root+'\\'+stock)) # creates numpy arrays of columns in file
s = sma(c,SMA,stockLen) # creates array of simple moving average
sL = sma(c,longSMA,stockLen) # creates array of longer simple moving average
#below is code that i'm trying to replace with vectorization:
for day in range(stockLen):
if c[day] < s[day] and stance == 'none':
stance = 'holding'
buyPrice = o[day+1]
if c[day] > sL[day] and stance == 'holding':
sellPrice = o[day+1]
tradeProfit = pctChange(buyPrice,sellPrice)
pctPerYear.append((250/holdingTime)*tradeProfit)
stance = 'none'
我已经到了拥有 2 个数组的地步,当我想买入时买入为负数,当我想卖出时为正数,但我不知道如何将“if 语句”逻辑放入数组中使用低效的 for 循环。
这里是买/卖数组:
buy = c[:]-s[:] #creates array that goes negative when closing price is below sma.
sell = c[:]-sL[:] #creates array that goes positive when closing price is above long sma.
感谢任何可以帮助我的人!
编辑:
这是一个sn-p数据,它的date,close,high,low,open,volume。每只股票大约 2500 行。
date,close,high,low,open,volume
20110718,43.40,43.68,42.93,43.07,25844
20110719,42.65,43.37,42.38,43.37,32334
20110720,43.11,43.11,42.06,42.46,22072
20110721,43.25,43.60,43.06,43.28,24965
【问题讨论】:
标签: python arrays numpy quantitative-finance