title: apply - test multiple conditions before moving rows
body: <p>I want to use apply to iterate over a matrix comparing open and high prices to a limit.</p>
  
  <p>I originally used a while loop but it was slow so moved to apply.</p>
  
  <p>I have tried to +1 to the StartingRow as below.</p>
  
  <pre><code>Summary &lt;- matrix(data=NA, nrow=1, ncol=1)
  Overall &lt;- matrix(data=NA, nrow=1, ncol=2)
  Open &lt;- matrix(data=NA, nrow=1, ncol=1)
  
  MSingle &lt;- function(x, StartingRow=1, Limit=0.01, StopLoss=0.01){
    Open = x[1]
    High = x[2]
    Low = x[3]
    #If the difference between High and Open exceeds Limit the function ends.
    if (!is.na(High-Open[StartingRow]) &gt; Limit){
      Summary &lt;&lt;- 1
      Open &lt;&lt;- Open
      Row &lt;&lt;- cbind(Summary, Open)
      Overall &lt;&lt;- rbind(Overall, Row)
    }
    #If the difference between Open and Low exceeds the Stoploss the function ends.
    else if (!is.na(Open[StartingRow]-Low) &gt; StopLoss){
      Summary &lt;&lt;- 0
      Open &lt;&lt;- Open
      Row &lt;&lt;- cbind(Summary, Open)
      Overall &lt;&lt;- rbind(Overall, Row)
    }
    #If neither of these are met then I want to compare the original Open price at time t...
    #...with the high and low prices at time t+1, t+2, and so on, until either of the first two...
    #...conditions is met.
    else{
      StartingRow = StartingRow + 1
    }
  }
  
  apply(EUR_USD2, 1, MSingle)
  </code></pre>
  
  <p>CORRECTION: This was initially lapply but it was an error on my part when copying code over, the result described is from apply. </p>
  
  <p>Example of Matrix EUR_USD2</p>
  
  <pre><code>       Open    High     Low   Close
  [1,] 1.20037 1.20100 1.20037 1.20100
  [2,] 1.20083 1.20095 1.20017 1.20030
  [3,] 1.20035 1.20043 1.20035 1.20043
  [4,] 1.20041 1.20050 1.20031 1.20046
  [5,] 1.20049 1.20049 1.20046 1.20048`
  [6,] 1.20050 1.20050 1.20048 1.20048
  [7,] 1.20050 1.20069 1.20032 1.20048
  [8,] 1.20048 1.20054 1.20027 1.20050
  [9,] 1.20051 1.20087 1.20047 1.20087
  [10,] 1.20082 1.20097 1.20076 1.20094
  </code></pre>
  
  <p>Intended results:</p>
  
  <pre><code>High[1] = 1.20100
  Open[1] = 1.20037
  Difference is 0.00063 (which is &lt; Limit)
  </code></pre>
  
  <p>Therefore I want to retain the same Open[1] but move to High[2].</p>
  
  <pre><code>High[2] = 1.20095
  Open[1] = 1.20037
  </code></pre>
  
  <p>Difference is 0.00058 (which is &lt; Limit) and so on, until the difference is greater than Limit (or less than stoploss) at which point the function starts again but with Open[2].</p>
  
  <p>Result of apply:</p>
  
  <pre><code>     Summary    Open
            NA      NA
  Open       1 1.20037
  Open       1 1.20083
  Open       1 1.20035
  Open       1 1.20041
  Open       1 1.20049
  Open       1 1.20050
  Open       1 1.20050
  Open       1 1.20048
  Open       1 1.20051
  </code></pre>
  
  <p>This result however is only comparing the (High-Open) to Limit for the same period.</p>
  
  <p>I want to compare High-Open (the difference) to the Limit. If this exceeds the limit then the first condition is met. If the condition is not met then I want to retain the same Open price but compare that to the High of the next period and test again against the Limit.</p>
  
  <p>Only then do I want apply to move onto comparing the Open and High from period 2 to Limit. </p>
  
  <p>The Open price must remain the same until the condition is met. Currently apply is comparing High(t=1)-Open(t=1) to Limit but does not compare the Open to any future periods High Values.</p>