how to get a loop to run once

Hello, I am trying to get this line of code to run only once. What its doing right now is calculating all the differences between the high price of the day and the buy price.

I'm not sure if i pasted this right or how to accurately paste a dataframe here, but basically the buyprice is the price we decide to buy a particular currency.

The buy difference is a calculation that says "hey, if the high of any of the next rows is higher than 0.1 from my buyprice, what is that difference?"

The thing is i need this only to execute once, then i want it to go to the next buyprice and do the same.

What is currently happening is it is calculating the difference from the first buy price to all of the rows in the data frame and that is not what i am looking for.

I tried to put in a variable called execute to stop it from running over and over again but that didn't seem to fix the issue.

Please help.

 Open   High    Low  Close                       triangle action buyprice buyprofit buydifference
1   1999-05-28 07:30:00 1.5972 1.5982 1.5964 1.5978        0      0   0.0000         0        0.0000
2   1999-05-28 08:00:00 1.5975 1.5979 1.5959 1.5973      -10      0   0.0000         0        0.0000
3   1999-05-28 08:30:00 1.5972 1.5994 1.5971 1.5982               0   0.0000         0        0.0000
4   1999-05-28 09:00:00 1.5984 1.5999 1.5981 1.5986               1   0.0000         0        0.0000
5   1999-05-28 09:30:00 1.5988 1.5999 1.5983 1.5994       10          1.5988         0        0.0000
6   1999-05-28 10:00:00 1.5996 1.6000 1.5979 1.5982                   0.0000         0        0.0012
7   1999-05-28 10:30:00 1.5983 1.5985 1.5962 1.5972      -10     -1   0.0000         0        0.0000
8   1999-05-28 11:00:00 1.5974 1.5981 1.5962 1.5975                   0.0000         0        0.0000
9   1999-05-28 11:30:00 1.5977 1.5998 1.5970 1.5997                   0.0000         0        0.0010
10  1999-05-28 12:00:00 1.5997 1.6020 1.5992 1.6014                   0.0000         0        0.0032
11  1999-05-28 12:30:00 1.6019 1.6047 1.6014 1.6041       10          0.0000         0        0.0059
12  1999-05-28 13:00:00 1.6041 1.6047 1.6005 1.6012                   0.0000         0        0.0059
data$buydifference <- 0
data
RandomNumber <- 5000
execute = 1
for (c in 2:nrow(data)){
  if (data$buyprice[c] > 0){
   # data$buydifference[c] <- "Happy"
    for (x in 1:RandomNumber){     
      if ((data$High[c+x] - data$buyprice[c] > 0.001) && (execute = 1)) {         
          execute <- 0
          data$buydifference[c+x] = data$High[c+x] - data$buyprice[c]                
      }     
    }   
  }  
}

My first guess is to build the buydifference column separately.

n <- nrow(data)
buydifference <- rep(0, n) #initialize a vector

for(row in 1:n-1){
#compute this one buy difference
buy_difference[row] <- max(data$high[row:n] - data$buyprice[row:n], 0)
}

# add the new column
data <- cbind(data, buydifference)

Hello I ran that code and it gave me a bunch of zeros.

Here is a visual of what i am trying to do.

The red represents where I get the buy price, the blue represents where the buy price is at least 0.001 from a buy price. The green represents where I am looking to record where the difference is at least +0.001 away from the buy price.

So basically it needs to loop through where the buy price is >0 and figure out when the high of row x+n (where x is where the buy price is recorded) is greater than 0.001 from the buy price.

n <- nrow(data)
buydifference <- rep(0, n) #initialize a vector

for(row in 1:n-1){
#compute this one buy difference
buy_difference[row] <- max(data$High[row:n] - data$Open[row] - 0.001, 0)
}

# add the new column
data <- cbind(data, buydifference)

Hello dsollberger,

I analyzed your code and i am not an expert but wouldn't your line of code check the high value of each subsequent row and subtract it by the open price of row n?

I was hoping only to have it look at the buyprice and if the buyprice is any value greater than zero, it should loop through as such:

buyprice = 1.1234
is the difference between the next high and the buyprice greater than 0.001? if yes, stop and look for the next buyprice thats greater than zero, if not is the difference between the next high at n+1 greater than the buyprice? if yes stop and look at the next buy price, if not n+2,n+3 until it is. Am I on the same page with you or am I the one that missed the boat on this one?

How did I do ? (is there a reward :stuck_out_tongue:)



df <- data.frame(
  row = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L),
  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
  ),
  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
  ),
  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
  ),
  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
  )
)


df$buy_price_origin_row <- NA
df$buy_price <- NA
df$sell_high_price_row <- NA
df$pos_difference <- NA

processprices <- function(df) {
  first <- TRUE
  for (row in 1:(nrow(df) - 1)) {
    if (first) {
      buy_price_candidate <- df[row, "Open"]
      first <- FALSE
    }

    for (innerloop in (row + 1):(nrow(df) - 1)) {
      if (df[innerloop, "High"] - buy_price_candidate > 0.001) {
        df[row, "buy_price"] <- buy_price_candidate
        df[row, "buy_price_origin_row"] <- row

        df[row, "sell_high_price_row"] <- innerloop
        df[row, "sell_high_price"] <- df[innerloop, "High"]
        df[row, "pos_difference"] <- df[innerloop, "High"] - buy_price_candidate
        first <- TRUE
        break
      }
    }
  }
  return(df)
}


processprices(df)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.