【问题标题】:Numpy, vectorizing selection of rows that meet buy/sell logic in quantitative analysisNumpy,矢量化在定量分析中满足买入/卖出逻辑的行选择
【发布时间】: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


    【解决方案1】:

    从 Pandas 文档中查看此链接。

    http://pandas.pydata.org/pandas-docs/stable/enhancingperf.html

    您可能还会发现此链接在短期内很有用:

    http://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html

    对于上面的代码,您是否尝试过以如下形式实现验证:

    DATA['Return'] = np.log(DATA['close'] / DATA['close'].shift(1))
    

    【讨论】:

    • 如果您能提供一些数据示例,我们很乐意进一步提供帮助。
    • 我添加了数据sn-p。感谢 pandas 链接,我将研究将 pandas 与 cython 一起使用,文档说它在 for 循环上工作得很快,所以这可能是正确的解决方案,而不是试图消除最后一个 for 循环。
    • 移动平均线使用的天数是多少? (我正在尝试使用从 2015 年 1 月 1 日到 2015 年 11 月 6 日的 LLOY.L 数据来完成这项工作,这对练习来说足够了吗?(221 天​​)你如何得出持有时间?(它是实际的还是派生的?通过卖出信号?您是否还要创建日志退货?
    • 我正在努力使我从本教程中学到的程序更高效。(vid 33)link 现在我正在使用 5 和 15 SMA 进行测试,但这可能不是' t 一个好的交易算法,我只是想让基础工作,所以我可以修改和调整它。我用一行“if stand == 'holding': holdingTime += 1 得到持有时间。我不知道究竟什么是日志回报,但我得到了百分比利润,将其附加到列表中,然后求和(list)/len(list) 在所有股票都跑完之后。- 30 分钟前的谜题
    【解决方案2】:

    如果你只想找到向量改变符号的点,你可以这样做:

    change_points = np.where(np.diff(buy>0)!=0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多