mercredi 30 octobre 2019

How to use a while loop to find the opning and closing points

I'm new here.

I want do something like this: i need to find the index in which the condition is true the first time so to find an opening position this can be either on the positive or negative side (therefore i used the if else statement); so far it works, but hen when i found this position i want to store it in a table, but also use it to to find the next first position/index for which another condition is true (closing point). so in my example the first opening position is 387; to find the closing position i need to the first position for which is true that ratio >= m but also the index is greater than 387.; if i would do this right this position/index would be 1743 and this is what i should/need to use again to start over, what is the minimum value for which the condition of the opening position is true, but the index should be greater than 1743. It's quite a complicated question, but i mainly want to know, how can i arrange that the index 387; that in the else part for determining the closing position this index 387 is used as starting point, and then the 1743 is used to to check whether the first condition top part of the formula is true. This needs to be executed for the whole data set.


i <- starting_from

while (i <= length(retail_stocks$ratio)){
  if ((min(which((retail_stocks$ratio > (m + k*s))  == TRUE))) < (min(which((retail_stocks$ratio < (m - k*s)) == TRUE)))) {
  pos$open[i] <-  min(which(retail_stocks$ratio > (m + k*s)))
  i <- pos$open[i] 
if(min(which(retail_stocks$ratio <= m))){
    pos$close[i] <- (min(which(retail_stocks$ratio <= m)))
    i <- pos$close[i]
} else {
  pos$open[i] <- min(which(retail_stocks$ratio < (m - k*s)))
  #pos$close[i] <- min(which(retail_stocks$ratio >= m)) 
  i <- pos$open[i]
      if(min(which(retail_stocks$ratio >= m))){
    pos$close[i] <- (min(which(retail_stocks$ratio >= m)))
    i <- pos$close[i]
  }
}
}

Aucun commentaire:

Enregistrer un commentaire