samedi 16 janvier 2021

R: How to stop a for statement when a if condition is met

I have a time series of prices and I am trying to create a trading strategy. I need to stop the trade as soon as the price_cap is met. I have data stored in a database and I need to create a list that indicates all the long positions (list called long). I have tried the following, but even if the condition is met, the list continues to store value:

long = list()
for (i in 5:length(index)) {

  long[i] = data$target_price[i]
  
    if (data$target_price > price_cap){
    
      break
    
    }
  
  print(long)
  
}

Any idea?

Thank you so much.

Aucun commentaire:

Enregistrer un commentaire