适用-在移动行之前测试多个条件

时间:2019-07-01 16:12:57

标签: r loops apply

我想使用apply遍历将开放价格和最高价格与限制进行比较的矩阵。

我最初使用while循环,但是它很慢,因此可以应用。

我曾尝试如下对StartingRow进行+1。

Summary <- matrix(data=NA, nrow=1, ncol=1)
Overall <- matrix(data=NA, nrow=1, ncol=2)
Open <- matrix(data=NA, nrow=1, ncol=1)

MSingle <- function(x, StartingRow=1, Limit=0.01, StopLoss=0.01){
  Open = x[1]
  High = x[2]
  Low = x[3]
  #If the difference between High and Open exceeds Limit the function ends.
  if (!is.na(High-Open[StartingRow]) > Limit){
    Summary <<- 1
    Open <<- Open
    Row <<- cbind(Summary, Open)
    Overall <<- rbind(Overall, Row)
  }
  #If the difference between Open and Low exceeds the Stoploss the function ends.
  else if (!is.na(Open[StartingRow]-Low) > StopLoss){
    Summary <<- 0
    Open <<- Open
    Row <<- cbind(Summary, Open)
    Overall <<- rbind(Overall, Row)
  }
  #If neither of these are met then I want to compare the original Open price at time t...
  #...with the high and low prices at time t+1, t+2, and so on, until either of the first two...
  #...conditions is met.
  else{
    StartingRow = StartingRow + 1
  }
}

apply(EUR_USD2, 1, MSingle)

更正:最初是不正确的,但是复制代码时是我的错误,描述的结果来自apply。

矩阵EUR_USD2的示例

       Open    High     Low   Close
[1,] 1.20037 1.20100 1.20037 1.20100
[2,] 1.20083 1.20095 1.20017 1.20030
[3,] 1.20035 1.20043 1.20035 1.20043
[4,] 1.20041 1.20050 1.20031 1.20046
[5,] 1.20049 1.20049 1.20046 1.20048`
[6,] 1.20050 1.20050 1.20048 1.20048
[7,] 1.20050 1.20069 1.20032 1.20048
[8,] 1.20048 1.20054 1.20027 1.20050
[9,] 1.20051 1.20087 1.20047 1.20087
[10,] 1.20082 1.20097 1.20076 1.20094

预期结果:

High[1] = 1.20100
Open[1] = 1.20037
Difference is 0.00063 (which is < Limit)

因此,我想保留相同的Open [1],但移至High [2]。

High[2] = 1.20095
Open[1] = 1.20037

差异为0.00058(这是<限制),依此类推,直到差异大于限制(或小于止损点)为止,此时函数将再次使用Open [2]启动。

申请结果:

     Summary    Open
          NA      NA
Open       1 1.20037
Open       1 1.20083
Open       1 1.20035
Open       1 1.20041
Open       1 1.20049
Open       1 1.20050
Open       1 1.20050
Open       1 1.20048
Open       1 1.20051

但是,此结果只是将同一时期的(高开盘)与限制进行比较。

我想比较高开(限价)与限价。如果超过限制,则满足第一个条件。如果不满足条件,那么我想保留相同的开盘价,但将其与下一个时期的高点进行比较,然后再次对限制进行测试。

然后,我只想申请比较从时段2到限制的开盘价和最高价。

在满足条件之前,开盘价必须保持不变。当前适用的是将高(t = 1)-开盘(t = 1)与限制进行比较,但不将开盘与任何未来期间的高值进行比较。

1 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解您期望的输出(您可以在帖子中添加期望的结果示例),但是与我建议的类似的东西可能会起作用:

s <- "Open High Low Close
1.20037 1.20100 1.20037 1.20100
1.20083 1.20095 1.20017 1.20030
1.20035 1.20043 1.20035 1.20043
1.20041 1.20050 1.20031 1.20046
1.20049 1.20049 1.20046 1.20048
1.20050 1.20050 1.20048 1.20048
1.20050 1.20069 1.20032 1.20048
1.20048 1.20054 1.20027 1.20050
1.20051 1.20087 1.20047 1.20087
1.20082 1.20097 1.20076 1.20094"

EUR_USD2 <- read.delim(textConnection(s), sep = " ")

myfun <- function(x, df, Limit, StopLoss) {

  highComp <- which(df$High - df$Open[x] > Limit)
  highCompMin <- if(length(highComp) == 0) 0 else min(highComp)
  lowComp <- which(df$Open[x] - df$Low > StopLoss)
  lowCompMin <- if(length(lowComp) == 0) 0 else min(lowComp)

  if(highCompMin == 0 & lowCompMin == 0) {
    result <- c(Summary = NA, Open = df$Open[x])
  } else if (highCompMin >= lowCompMin) {
    result <- c(Summary = 1, Open = df$Open[x])
  } else if (lowCompMin > highCompMin) {
    result <- c(Summary = 0, Open = df$Open[x])
  } else {
    result <- c(Summary = NA, Open = df$Open[x])
  }

  return(result)

}

t(sapply(1:nrow(EUR_USD2), function(x) myfun(x, df = EUR_USD2,
                                             Limit = 0.00062, StopLoss = 0.0005)))

### OUTPUT:
#      Summary    Open
# [1,]       1 1.20037
# [2,]       0 1.20083
# [3,]       1 1.20035
# [4,]      NA 1.20041
# [5,]      NA 1.20049
# [6,]      NA 1.20050
# [7,]      NA 1.20050
# [8,]      NA 1.20048
# [9,]      NA 1.20051
# [10,]      0 1.20082

对于每一行,iEUR_USD2$Open[i]与整个HighLow列进行比较,以找到满足比较要求的最小索引,然后根据以下内容设置Summary结果。如果没有值满足比较要求,则将值设置为NA