适用-在移动行之前测试多个条件 [英] apply - test multiple conditions before moving rows

查看:51
本文介绍了适用-在移动行之前测试多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use apply to iterate over a matrix comparing open and high prices to a limit.

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

I originally used a while loop but it was slow so moved to apply.

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

I have tried to +1 to the StartingRow as below.

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.

CORRECTION: This was initially lapply but it was an error on my part when copying code over, the result described is from apply.

矩阵EUR_USD2的示例

Example of Matrix 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].

Therefore I want to retain the same Open[1] but move to High[2].

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

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

Difference is 0.00058 (which is < Limit) and so on, until the difference is greater than Limit (or less than stoploss) at which point the function starts again but with 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

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

This result however is only comparing the (High-Open) to Limit for the same period.

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

I want to compare High-Open (the difference) to the Limit. If this exceeds the limit then the first condition is met. If the condition is not met then I want to retain the same Open price but compare that to the High of the next period and test again against the Limit.

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

Only then do I want apply to move onto comparing the Open and High from period 2 to Limit.

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

The Open price must remain the same until the condition is met. Currently apply is comparing High(t=1)-Open(t=1) to Limit but does not compare the Open to any future periods High Values.

推荐答案

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

I'm not completely sure I understood the output you are expecting (you could add to your post and example of the exact result you expect), but something similar to what I propose might work:

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.

For each row i compares EUR_USD2$Open[i] with the whole High and Low columns to find the minimum index that fulfills the comparisons and then sets Summary according to the result. The value is set to NA in case no value fulfills the comparisons.

这篇关于适用-在移动行之前测试多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆