EDIT2:谢谢你的回复,我得到了更多的信息:
buyChange = np.where(np.diff(buy>0)!=0)
sellChange = np.where(np.diff(sell>0)!=0)
现在我有两个索引数组,一个是购买逻辑交叉的索引,另一个是卖出逻辑交叉的索引数组:
购买:(数组:[ 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+2]
[电]EDIT2 2
编辑:我已经找到了这个函数,它可以在数组中找到负的部分,但是现在我必须找到该部分开头的一天,也就是数组从正到负的那一天。有人能帮忙吗?
buys = np.where( buy < 0 )
下面这样的东西不起作用,但这是我想要实现的想法:
buys = np.where( buy < 0 and buy[-1] > 0 )
或者:
buys = np.where( buy[1:] < 0 and buy[:-1] > 0 )
/EDIT
我有一个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'
我已经到了有两个数组的地步,当我想要买卖的时候,买进会变成负值,当我想卖出的时候,我会变成正数,但是我不知道如何在不使用低效率的for循环的情况下,把'if语句‘逻辑放入数组中。
以下是买卖数组:
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.
感谢任何能帮助我的人!
编辑:这是一个数据片段,它的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发布于 2015-11-29 08:05:35
查看Pandas文档中的这个链接。
http://pandas.pydata.org/pandas-docs/stable/enhancingperf.html
在短期内,您可能还会发现这个链接很有用:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html
对于上面的代码,您是否尝试过以以下形式实现vetorisation:
DATA['Return'] = np.log(DATA['close'] / DATA['close'].shift(1))发布于 2015-11-30 15:30:52
如果您只想找到向量更改符号的点,您可以这样做:
change_points = np.where(np.diff(buy>0)!=0)https://stackoverflow.com/questions/33980613
复制相似问题