首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >反向测试R中交易的开仓计数器

问反向测试R中交易的开仓计数器
EN

Stack Overflow用户
提问于 2017-09-29 00:45:48
回答 1查看 166关注 0票数 0

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

如果DifFromFv (偏离公允价值的偏差)达到-10,然后随着DifFromFv的倍数-3 (-13,-16,-19等)增加1,那么我该怎么做呢?当每次DifFromFv从上次变回的'posy‘中恢复+5时,'posy’减少1?简单地说,我买的是一旦DifFromFv达到10分,平均每3分,同时拿出每个人平均5分利润。

例如:

代码语言:javascript
复制
  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等,而不论平均利润是在哪里填写的,如avg #2的目标利润为-11.00而不是-11.60。这既是为了减少现实生活中的错误空间,填充vs数据,也是我非常肯定,应该使这个概念的方法更容易思考。

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-29 12:08:21

下一次请提供一些代码,即使您的解释非常清楚。但是,您没有提到如何处理DifFromFv中的大跳转(例如,如果从-3跳到-18),所以由您决定。

下面是带有注释的代码:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46480211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档