title: Applying technical trading rule to all combination of inputs
body: <p>I have a Dataframe with columns containing different time series of data. I need to insert these columns into the function below automatically to find the best combination (highest return): </p>
  
  <pre><code>returns &lt;- as.data.frame(rep(0, 4261)) #4261 because that's the length of n1.lc
  returns$n2_5_10 &lt;- rep(0, nrow(returns))
  MSVrule &lt;- function(n1, n2, hold){
    for(i in 13:nrow(n1.lc)){
      if (n1[i] &gt; n2[i] &amp; n1[i-1] &lt; n2[i-1]) {
        returns$n2_5_10[i] &lt;- dt.lc$Settle[i - hold] - dt.lc$Settle[i]
      } else {
        if (n1[i] &lt; n2[i] &amp; n1[i-1] &gt; n2[i-1])
        {returns$n2_5_10[i] &lt;- dt.lc$Settle[i] - dt.lc$Settle[i - hold]
        }
        else{
          NULL
        }
      }
    }
  }
  
  MSVrule(n1.lc$N1_2_5, n2.lc$n2_2_10, 5) 
  </code></pre>
  
  <p>This function doesn't work, leaving <code>returns$n2_5_10[i] 0</code> However, when I specifiy the vectors in the function it works:</p>
  
  <pre><code>hold &lt;- 5
  for(i in 13:nrow(n1.lc)){
      if (n1.lc$N1_2_5[i] &gt; n2.lc$n2_2_10[i] &amp; n1.lc$N1_2_5[i-1] &lt; n2.lc$n2_2_10[i-1]) {
        returns$n2_5_10[i] &lt;- (dt.lc$Settle[i - hold] - dt.lc$Settle[i]) / dt.lc$Settle[i]
      } else {
        if (n1.lc$N1_2_5[i] &lt; n2.lc$n2_2_10[i] &amp; n1.lc$N1_2_5[i-1] &gt; n2.lc$n2_2_10[i-1])
        {returns$n2_5_10[i] &lt;- (dt.lc$Settle[i] - dt.lc$Settle[i - hold]) / dt.lc$Settle[i - hold]
        }
        else{
          NULL
        }
      }
    }
  </code></pre>
  
  <p>The next step would be to automatically apply the function to other combinations of vectors from the <code>n1.lc</code> Dataframe. But I need the function to work first. </p>