Thanks! I've simplified your data to create a 3-column data frame
- Your datetime (renamed tradetime)
- An "indicator" composed of random numbers
- A field "action" based on the indicator in the immediately preceding row
Does this clarify your problem?
suppressPackageStartupMessages(library(dplyr))
dat <- structure(list(datetime = structure(c(927891000, 927892800, 927894600,
927896400, 927898200, 927900000, 927901800, 927903600, 927905400,
927907200, 927909000, 927910800, 927912600, 927914400, 927916200,
927918000, 927919800, 927921600, 927923400, 927925200, 927927000,
927928800, 927930600, 927932400, 927934200, 927936000, 927937800,
927939600, 927941400, 927943200, 928123200, 928125000, 928126800,
928128600, 928130400, 928132200, 928134000, 928135800, 928137600,
928139400, 928141200, 928143000, 928144800, 928146600, 928148400,
928150200, 928152000, 928153800, 928155600, 928157400), class = c("POSIXct",
"POSIXt"), tzone = ""), Open = c(1.5972, 1.5975, 1.5972, 1.5984,
1.5988, 1.5996, 1.5983, 1.5974, 1.5977, 1.5997, 1.6019, 1.6041,
1.6012, 1.6009, 1.6005, 1.6012, 1.6036, 1.6022, 1.6019, 1.6007,
1.6025, 1.6021, 1.6028, 1.6012, 1.6003, 1.6013, 1.6015, 1.601,
1.601, 1.6014, 1.6029, 1.6024, 1.6023, 1.6021, 1.6018, 1.602,
1.602, 1.6023, 1.6021, 1.6018, 1.6025, 1.6027, 1.602, 1.6024,
1.6024, 1.6027, 1.6028, 1.6026, 1.6029, 1.6035), High = c(1.5982,
1.5979, 1.5994, 1.5999, 1.5999, 1.6, 1.5985, 1.5981, 1.5998,
1.602, 1.6047, 1.6047, 1.602, 1.601, 1.6013, 1.6037, 1.6039,
1.6023, 1.6025, 1.6029, 1.604, 1.6031, 1.6031, 1.6019, 1.6017,
1.6015, 1.6015, 1.6018, 1.6023, 1.6014, 1.6029, 1.6024, 1.6023,
1.6021, 1.6018, 1.6025, 1.6025, 1.6024, 1.6023, 1.6025, 1.603,
1.6027, 1.6026, 1.6024, 1.603, 1.6036, 1.6033, 1.604, 1.6037,
1.6039), Low = c(1.5964, 1.5959, 1.5971, 1.5981, 1.5983, 1.5979,
1.5962, 1.5962, 1.597, 1.5992, 1.6014, 1.6005, 1.6002, 1.5991,
1.5996, 1.6003, 1.601, 1.6002, 1.6004, 1.5997, 1.6007, 1.6014,
1.5999, 1.6007, 1.6001, 1.6007, 1.6006, 1.601, 1.601, 1.6014,
1.6029, 1.6022, 1.6022, 1.6016, 1.6016, 1.6015, 1.602, 1.602,
1.6021, 1.6018, 1.6022, 1.6018, 1.602, 1.6022, 1.602, 1.6021,
1.602, 1.602, 1.6028, 1.6024), Close = c(1.5978, 1.5973, 1.5982,
1.5986, 1.5994, 1.5982, 1.5972, 1.5975, 1.5997, 1.6014, 1.6041,
1.6012, 1.6007, 1.6007, 1.6011, 1.6037, 1.6021, 1.6015, 1.6013,
1.6019, 1.6022, 1.6024, 1.6009, 1.6011, 1.6015, 1.6012, 1.6013,
1.6018, 1.6022, 1.6014, 1.6029, 1.6024, 1.6022, 1.6017, 1.6018,
1.6019, 1.6024, 1.6022, 1.6023, 1.6023, 1.6022, 1.6021, 1.6025,
1.6022, 1.6025, 1.6029, 1.6028, 1.6027, 1.6037, 1.6034), bull = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA), count = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA), triangle = c("0", "-10", "", "", "10",
"", "-10", "", "", "", "10", "", "", "", "", "10", "", "", "-10",
"", "", "10", "-10", "", "10", "-10", "", "", "10", "-10", "10",
"", "", "-10", "", "", "10", "-10", "", "", "", "-10", "10",
"-10", "", "10", "", "-10", "10", ""), engulf = c("0", "0", "0",
"buy @ next open", "", "", "sell @ next open", "", "", "", "",
"", "", "", "", "", "", "", "", "", "buy @ next open", "", "",
"", "", "", "sell @ next open", "buy @ next open", "", "", "",
"", "", "", "", "buy @ next open", "", "", "", "", "", "", "",
"", "", "buy @ next open", "", "", "", "buy @ next open")), row.names = c(NA,
50L), class = "data.frame")
set.seed(42)
indicator <- rnorm(50)
abbr <- dat %>% select(tradetime = datetime)
abbr <- cbind(abbr, indicator)
abbr <- abbr %>% mutate(action = ifelse(abs(lag(indicator < 1.5)), "no action", NA))
abbr <- abbr %>% mutate(action = ifelse(lag(indicator <= -1.5), "sell", action))
abbr <- abbr %>% mutate(action = ifelse(lag(indicator >= 1.5), "buy", action))
abbr
#> tradetime indicator action
#> 1 1999-05-28 04:30:00 1.37095845 <NA>
#> 2 1999-05-28 05:00:00 -0.56469817 no action
#> 3 1999-05-28 05:30:00 0.36312841 no action
#> 4 1999-05-28 06:00:00 0.63286260 no action
#> 5 1999-05-28 06:30:00 0.40426832 no action
#> 6 1999-05-28 07:00:00 -0.10612452 no action
#> 7 1999-05-28 07:30:00 1.51152200 no action
#> 8 1999-05-28 08:00:00 -0.09465904 buy
#> 9 1999-05-28 08:30:00 2.01842371 no action
#> 10 1999-05-28 09:00:00 -0.06271410 buy
#> 11 1999-05-28 09:30:00 1.30486965 no action
#> 12 1999-05-28 10:00:00 2.28664539 no action
#> 13 1999-05-28 10:30:00 -1.38886070 buy
#> 14 1999-05-28 11:00:00 -0.27878877 no action
#> 15 1999-05-28 11:30:00 -0.13332134 no action
#> 16 1999-05-28 12:00:00 0.63595040 no action
#> 17 1999-05-28 12:30:00 -0.28425292 no action
#> 18 1999-05-28 13:00:00 -2.65645542 no action
#> 19 1999-05-28 13:30:00 -2.44046693 sell
#> 20 1999-05-28 14:00:00 1.32011335 sell
#> 21 1999-05-28 14:30:00 -0.30663859 no action
#> 22 1999-05-28 15:00:00 -1.78130843 no action
#> 23 1999-05-28 15:30:00 -0.17191736 sell
#> 24 1999-05-28 16:00:00 1.21467470 no action
#> 25 1999-05-28 16:30:00 1.89519346 no action
#> 26 1999-05-28 17:00:00 -0.43046913 buy
#> 27 1999-05-28 17:30:00 -0.25726938 no action
#> 28 1999-05-28 18:00:00 -1.76316309 no action
#> 29 1999-05-28 18:30:00 0.46009735 sell
#> 30 1999-05-28 19:00:00 -0.63999488 no action
#> 31 1999-05-30 21:00:00 0.45545012 no action
#> 32 1999-05-30 21:30:00 0.70483734 no action
#> 33 1999-05-30 22:00:00 1.03510352 no action
#> 34 1999-05-30 22:30:00 -0.60892638 no action
#> 35 1999-05-30 23:00:00 0.50495512 no action
#> 36 1999-05-30 23:30:00 -1.71700868 no action
#> 37 1999-05-31 00:00:00 -0.78445901 sell
#> 38 1999-05-31 00:30:00 -0.85090759 no action
#> 39 1999-05-31 01:00:00 -2.41420765 no action
#> 40 1999-05-31 01:30:00 0.03612261 sell
#> 41 1999-05-31 02:00:00 0.20599860 no action
#> 42 1999-05-31 02:30:00 -0.36105730 no action
#> 43 1999-05-31 03:00:00 0.75816324 no action
#> 44 1999-05-31 03:30:00 -0.72670483 no action
#> 45 1999-05-31 04:00:00 -1.36828104 no action
#> 46 1999-05-31 04:30:00 0.43281803 no action
#> 47 1999-05-31 05:00:00 -0.81139318 no action
#> 48 1999-05-31 05:30:00 1.44410126 no action
#> 49 1999-05-31 06:00:00 -0.43144620 no action
#> 50 1999-05-31 06:30:00 0.65564788 no action
Created on 2020-01-22 by the reprex package (v0.3.0)