用于交易R的回测试开仓计数器

时间:2017-09-29 00:45:48

标签: r quantitative-finance trading back-testing

开始对一些交易数据进行回溯测试,特别是一个非常基本的均值回归理念,并且无法理解如何处理这个概念。

我怎么会有一个跑步的' posy'一旦DifFromFv(与公允价值的偏差)达到-10并且随后是“后”,则增加1。当DifFromFv延伸-3的倍数(-13,-16,-19等)同时具有“假”时,它增加1。每次DifFromFv从最后一次改变后退+5后,减少1'简单地说,一旦DifFromFv达到10分并平均每3分,我就买入,同时将每个人的平均值拿出5分获利。

E.g:

  DifFromFv posy
     0.00    0
   -10.00    1   #initial clip (target profit -5.00)
   -11.50    1
   -13.00    2   #avg #1 (target profit -8.00)
   -16.60    3   #avg #2 (target profit -11.00)
   -12.30    3    
   -11.00    2   #taking profit on avg #2
   -14.10    2   
    -8.00    1   #taking profit on avg #1
    -7.00    1
    -5.00    0   #taking profit on initial clip

应该注意的是,每个剪辑的获利一致设置为-5,-8,-11等。无论平均值在何处被填充,如平均#2的目标利润在-11.00而不是-11.60所示。这既可以减少现实填充与数据填充中的误差范围,而且我非常确定应该更容易思考这个概念的方法。

提前致谢!

1 个答案:

答案 0 :(得分:0)

下次请提供一些代码,即使你的解释很清楚。 但是,你没有提到你想如何处理DifFromFv中的大跳跃(例如,如果它从-3变为-18),所以我把它留给你。

以下是带注释的代码:

library(plyr)

firstPosy = FALSE

DiffFair <- c(0, -10, -11.5, -13, -16.6, -12.3, -11, -14.1, -8, -7, -5) # Your data here
posy <- c(0)

buyPrices <- c(0) # Stores the prices at which you by your asset
targetProfit <- c(0) # Stores the target profit alongside with the vector above

steps <- c(0) # Stores your multiples of -3 after -10 (-10, -13, -16...)
PNL = 0

for (i in 2:length(DiffFair)) {

  # Case where posy increments for the first time by one

  if (DiffFair[i] <= -10 & DiffFair[i] > -13 & firstPosy == FALSE) {
    firstPosy = TRUE
    posy <- c(posy, 1)
    steps <- c(steps, round_any(DiffFair[i], 10, f = ceiling))
    lastChangePosy = DiffFair[i]
    buyPrices <- c(buyPrices, DiffFair[i])
    targetProfit <- c(targetProfit, -5)
  } 

else if (DiffFair[i] <= -13 & firstPosy == FALSE) {
    firstPosy = TRUE
    lastChangePosy = DiffFair[i]
    steps <- c(steps, round_any(DiffFair[i] + 10, 3, f = ceiling) - 10)
    buyPrices <- c(buyPrices, DiffFair[i])
    targetProfit <- c(targetProfit, -5)
    posy <- c(posy, tail(posy, n=1) + (-round_any(DiffFair[i] + 10, 3, f = ceiling) / 3) + 1)
  }

  # Posy increase

  else if (tail(steps, n=1) > round_any(DiffFair[i] + 10, 3, f = ceiling) - 10 & DiffFair[i] <= -10) {
    posy <- c(posy, posy[i-1] + 1)
    steps <- c(steps, round_any(DiffFair[i] + 10, 3, f = ceiling) -10)
    lastChangePosy = DiffFair[i]

    buyPrices <- c(buyPrices, DiffFair[i])
    targetProfit <- c(targetProfit, tail(targetProfit, n=1) - 3)
  }

  # Posy decrease

 else if (DiffFair[i] >= tail(targetProfit, n=1) & tail(posy, n=1) > 0) {
    if (tail(targetProfit, n=1) == -5) {
      posy <- c(posy, 0)
    }
    else {
      posy <- c(posy, posy[i-1] - 1)
    }
    lastChangePosy = DiffFair[i]

    # Compute PNL and delete the target profit and buy price from the vectors
    PNL = PNL + (DiffFair[i] - tail(buyPrices, n=1))
    buyPrices <- buyPrices[-length(buyPrices)]
    targetProfit <- targetProfit[-length(targetProfit)]
    steps <- steps[-length(steps)]

    if (DiffFair[i] > -10) {
      firstPosy = FALSE
    }

  }

  # Posy doesn't change

  else {
    posy <- c(posy, posy[i-1])
  }

}

print(PNL)