R 多行引用条件(用于循环)

R 多行引用条件(用于循环),r,for-loop,financial,trading,R,For Loop,Financial,Trading,使用for循环引用多行中发生的条件时遇到问题 想法如下。有一个dataframe,列为LastPrice和KCT。要将SignalBinary列添加到数据帧,如果 1) LastPrice[j]>KCT[j]在任意3个连续行中& 2) LastPrice[j+1]-第j+1至j+1+10行(即下面的10行)中的任何一行中的LastPrice[j+1+3]>12 然后要在SignalBinary[i]中记录1 df <- data.frame(nrow =20, ncol =2) df &l

使用for循环引用多行中发生的条件时遇到问题

想法如下。有一个dataframe,列为LastPrice和KCT。要将SignalBinary列添加到数据帧,如果

1) LastPrice[j]>KCT[j]在任意3个连续行中&

2) LastPrice[j+1]-第j+1至j+1+10行(即下面的10行)中的任何一行中的LastPrice[j+1+3]>12

然后要在SignalBinary[i]中记录1

df <- data.frame(nrow =20, ncol =2)
df <- data.frame(LastPrice = c(1221,1220,1220,1217,1216,1218,1216,1216,1217,1220,1219,1218,1220,1216,1217,1218,1218,1207,1206,1205), KCT = c(1218,1218,1219,1218,1221,1217,1217,1216,1219,1216,1217,1216,1219,1217,1218,1217,1217,1217,1219,1217))
df$SignalBinary <-for(j in1:20){for(i in1:10){ifelse (df$LastPrice[j]> df$KCT[j]& df$LastPrice[j+1]> df$KCT[j+1]& df$LastPrice[j+2]> df$KCT[j+2]& df$LastPrice[j+i]- df$LastPrice[j+i+3]>12,1,0)}}
df$KCT[j+2]&df$LastPrice[j+i]-df$LastPrice[j+i+3]>12,1,0)}
根据这些数据,我们希望代码在第10行和第11行中记录1,在其余行中记录0。但我做错了什么。运行代码不会给出错误消息,但不会创建df$SignalBinary。运行df$SignalBinary表示NULL

顺便说一句,其目的是将代码应用于大型价格数据库,以对二进制信号进行统计


希望有人能帮忙。非常感谢

有一点是不正确的,那就是您没有从
ifelse
语句中返回任何内容(如果条件(未)满足,则当前有1和0作为操作)。我认为(但不要引用我的话)我已经用一种更简单的方法解决了您的问题,没有使用嵌套的
进行
循环

df <- data.frame(nrow = 20, ncol = 2)
df <- data.frame(LastPrice = c( 1221, 1220, 1220, 1217, 1216,  1218 , 1216, 1216, 1217, 1220,     1219, 1218, 1220, 1216, 1217, 1218, 1218, 1207, 1206, 1205), KCT = c( 1218, 1218, 1219, 1218, 1221,  1217 , 1217, 1216, 1219, 1216, 1217, 1216, 1219, 1217, 1218, 1217, 1217, 1217, 1219, 1217))

df$SignalBinary <- as.numeric(df$LastPrice >= df$KCT & 
                      c(rep(FALSE ,3), diff(df$LastPrice, lag=3) >= 3)) 

df已解决!发布解决方案。比我想象的要复杂得多。必须将StrongMove的大小从12更改为3,否则根据我在本例中提供的数据,将不会得到任何信号

#Data
df <- data.frame(LastPrice = c( 1221, 1220, 1220, 1217, 1216,  1218 , 1216, 1216, 1217, 1220, 1219, 1218, 1220, 1216, 1217, 1218, 1218, 1207, 1206, 1205), KCT = c( 1218, 1218, 1219, 1218, 1221,  1217 , 1217, 1216, 1219, 1216, 1217, 1216, 1219, 1217, 1218, 1217, 1217, 1217, 1219, 1217))

#Define inputs
StrongMoveWindow = 10     # up to this far below the current row
StrongMoveDur = 3         # check row against another this far down
StrongMoveSize = 3        # for a difference at least this big
PvsKCTDur = 3

#Set variables and define loop boundaries
base_rows = 1:(nrow(df) - StrongMoveDur)  # can't check more than this
candidate_max = pmin(base_rows + StrongMoveWindow, nrow(df) - StrongMoveDur) # for a given base row, this is the maximum row to start checking against
df$StrongMove = rep(NA, nrow(df))
df$SignalBinary = rep(NA, nrow(df)) # pre-allocate a vector of results

#Make StrongMove variable
for (i in seq_along(base_rows)) {
  df$StrongMove[i] = as.numeric(
    any(
      df$LastPrice[(i + 1):candidate_max[i]] - 
        df$LastPrice[((i + 1):candidate_max[i]) + StrongMoveDur] > StrongMoveSize))}

#Make ContPvsKCT variable
library(data.table)
setDT(df)
df[, SingPvsKCT := as.integer(LastPrice > KCT)]
df[, ContPvsKCT := do.call(pmin, shift(SingPvsKCT, 0:(PvsKCTDur-1), type="lead"))]

#Make SignalBinary variable
df$SignalBinary <- ifelse (df$ContPvsKCT == 1 & df$StrongMove == 1, 1, 0)
#数据

df这里有一个问题,当您访问df$LastPrice[j+i]时,考虑i=20和j=10。您正在尝试从只有20行的数据集中访问第30行。如果else()没有对此抛出错误,那么您是对的。不幸的是,我不知道如何解决这个问题。非常感谢你的帮助!!像这样使用“diff”会在前面的一行和三行之间产生差异。我需要一行和前面三行中的每一行之间的差异。可以将diff函数重复3次,但这里给出的简化问题与3次比较。事实上,我们希望与至少20个进行比较,在某些情况下甚至更多,所以不要认为有办法绕过for循环。此外,您还忽略了第三个(更复杂的)条件(即仅在前面的10行中查找差异,而不是在每一行中)。无论如何,谢谢你,给了我一些思考的食物。