循环直到将来的值高于先前的固定值(max nrow)

时间:2017-07-27 04:15:00

标签: r

好的,这里。现在代码设置为signal == 1,存储比较价格。它将比较价格存储在同一行signal == 1

目标是signal == 1。存储下一个比较价格+ 1 row below signal == 1

new.df <- data.frame(new.df,response=0)
    input <- new.df$close.detrend.n11 # Input column for code
    signal <- new.df$detrend.signal.n11
    nlines <- 10 # Time Stop

state <- "off"
for (i in 1:nrow(new.df)) { # loop through data
  if (state == "off") { # off state, loop does nothing until signal = 1
    if (signal[i] == 0) {
      next
    } else { # signal = 1 encountered, if not a 0 it will do whats below...
      comparison_price <- input[i] # save current price for comparing ### Set to +1 row below signal ==1 row)
      n_comparisons <- 0              # keep track of how many comparisons 
      state <- "on"                   # change state to "on"
    }
  } else if (state == "on") { # Above turned on as found signal = 1
    new.df$response[i] <- 1 
    if (input[i] > comparison_price & n_comparisons >= 1) { # found higher price and line travel >=1, travel after +1 line to find nearest future highest price
      state <- "off" # Turns off as found higher price, now does nothing until signal == 1
    } else if (n_comparisons == 10) { # If no higher price and comparisons >=1, turn off
      state <- "off"  # Turns off as max limit reached
      n_comparisons <- 0   # Sets count to 0
      if (signal[i] == 1) {  # Find signal match
        comparison_price <- input[i] # Set price for comparing  ### Set to +1 row below signal ==1 row)
        n_comparisons <- 0 # Sets count to 0
        state <- "off"    # Turn off 
      }
    } else if (n_comparisons == nlines) { # hit comparison limit
      state <- "off"  # Turns off as max limit reached
      n_comparisons <- 0   # Sets count to 0
      if (signal[i] == 1) { # Finds signal ==1
        comparison_price <- input[i] # Set price for comparing ### Set to +1 row below signal ==1 row)
        n_comparisons <- 0 # Set count to 0
        state <- "on" # Turn On
      }
    } else { # price less or equal to comparison price
      n_comparisons <- n_comparisons + 1
    }
  }
}

以下虚拟数据:

下面

new.df

  detrend.signal.n11 response close.detrend.n11
1                   0        0       0.002044539
2                   0        0      -0.022593487
3                   1        0      -0.031842265
4                   1        1      -0.065392575
5                   0        1      -0.043699817
6                   0        1      -0.014110718
7                   0        0      -0.021899531
8                   0        0      -0.013908376
9                   0        0      -0.019580252
10                  0        0       0.023034983
11                  0        0       0.014598769
12                  0        0       0.013928860
13                  0        0       0.008568669
14                  0        0       0.020220697
15                  0        0      -0.003770356
16                  0        0      -0.021588957
17                  0        0      -0.018637185
18                  0        0      -0.007193684
19                  0        0      -0.013691624
20                  0        0      -0.020903833
21                  1        0      -0.036922613
22                  0        1      -0.011845136
23                  0        1       0.001115208
24                  0        0       0.018121000
25                  0        0       0.027648296
26                  0        0       0.016920882
27                  0        0       0.006321574
28                  0        0      -0.012052026
29                  0        0      -0.017340348
30                  0        0      -0.029062592
31                  0        0      -0.012560974
32                  0        0      -0.029126952
33                  0        0      -0.026220867
34                  0        0       0.007337385
35                  0        0       0.009356641
36                  0        0       0.027851405
37                  0        0       0.044130597
38                  0        0       0.036630768
39                  0        0       0.028685373
40                  0        0       0.030813270
41                  0        0       0.020690203
42                  0        0       0.014865516
43                  0        0      -0.001644471
44                  0        0       0.011208823
45                  0        0       0.009698927

1 个答案:

答案 0 :(得分:1)

这是解决方案的骨架

prices <- round(cumsum(c(1400, rnorm(99, 20, 40))), 2)
signal <- sample(c(0, 1), 100, replace = TRUE, prob = c(0.9, 0.1))
df <- data.frame(
    price = prices,
    signal = signal,
    response = 0
)

state <- "off"
for (i in 1:nrow(df)) { # loop through data
    if (state == "off") { # off state, loop does nothing until signal = 1
        if (df$signal[i] == 0) {
            next
        } else { # signal = 1 encountered
            comparison_price <- df$price[i] # save current price for comparing
            n_comparisons <- 0              # keep track of how many comparisons
            state <- "on"                   # change state to "on"
        }
    } else if (state == "on") {
        df$response[i] <- 1
        if (df$price[i] > comparison_price) { # found higher price
            state <- "off"
            if (df$signal[i] == 1) {
                comparison_price <- df$price[i]
                n_comparisons <- 0
                state <- "on"
            }
        } else if (n_comparisons == 10) { # hit comparison limit
            state <- "off"
            n_comparisons <- 0
            if (df$signal[i] == 1) {
                comparison_price <- df$price[i]
                n_comparisons <- 0
                state <- "on"
            }
        } else { # price less or equal to comparison price
            n_comparisons <- n_comparisons + 1
        }
    }
}

df

关键是在遍历数据时保持某种状态,这里我使用了简单的"on"/"off"状态。在点击1信号后,搜索未来更高的值仅变为"on"。在10次比较失败后,它也会自动关闭。因为它没有处理它在1行信号的同一行上找到更高值的情况,它会在这样的行上自行关闭,但你可以添加代码将其重新打开。

编辑:

索引i表示此代码中的行号,for循环遍历1, 2, ..., n行数据的值n。因此,如果您想要下一行,请使用i + 1

if (signal[i] == 1) {  # Find signal match
        comparison_price <- input[i + 1] # Set price for comparing  ### Set to +1 row below signal ==1 row)
        n_comparisons <- 0 # Sets count to 0
        state <- "off"    # Turn off 
}

但是,如果您在数据的最后一行发现signal == 1,则没有下一行,因此您需要弄清楚如何处理。