代码之家  ›  专栏  ›  技术社区  ›  Andrew Bannerman

循环,直到未来值高于之前的固定值(最大nrow)

r
  •  0
  • Andrew Bannerman  · 技术社区  · 9 年前

    好了,开始吧。现在代码设置为on 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 回复  |  直到 9 年前
        1
  •  1
  •   shians    9 年前

    这是一个解决方案的框架

    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" 状态对未来更高价值的搜索只会转向 "on" 击中1信号后。在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