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]
}
}
}
}