title: Backtesting Trading Strategy in R using quantmod: Function and for loop within a Function
body: <p>I am using R, quantmod and Performanceanalystics packages. As part of a backtesting strategy, I am trying to create a signal/holdings vector that tells me whether I should buy/sell/hold a stock, based on the value of RSI. If RSI&lt;30, buy (so holdings increases by 1), if RSI is between 30 &amp; 50, don't do anything (so holdings remain the same as yesterday). If RSI>=50, sell everything (so holdings become zero). Thereafter, use the dailyReturn() function from Performanceanalytics to calculate and generate a graph of returns. </p>
  
  <p>Note that RSI() is a function that takes "price" and "day", and dailyReturn() function also takes "price"</p>
  
  <p>I have been able to do perfectly fine with the following code below. </p>
  
  <pre><code>library(quantmod)
  library(PerformanceAnalytics)
  getSymbols("IBM", src= "yahoo", from = "2000-01-01", to ="2015-09-25")
  
  rsi&lt;-RSI(Cl(IBM),14)
  rsi&lt;-lag(rsi,1)
  rsi[is.na(rsi)] &lt;- 0 
  
  holdings1 &lt;-c() #initialize the vector
  holdings1[1:14+1]&lt;-0  #Not holding any shares of IBM from 1st to the 15th day
  for (i in 14+2: length(Cl(IBM))){ #assume start buying from the 16th day onwards since we are using RSI where n=14
   if (rsi[i]&lt;30){ #if RSI&lt;30, we buy one share of IBM
    holdings1[i]&lt;-holdings1[i-1]+1  
    } else if (rsi[i]&gt;50){ # if RSI&gt;50, we sell all holdings
    holdings1[i]&lt;-0
   } else 
     holdings1[i]&lt;- holdings1[i-1] # remains the same: if 30&lt;=RSI&lt;=50 we don't do anything, so same holdings as prior
   }
  size1&lt;-reclass(holdings1,Cl(IBM))
  ret1&lt;-dailyReturn(Cl(IBM))*size1
  charts.PerformanceSummary(ret1)
  </code></pre>
  
  <p>But I am required to create a function called "size1()" that takes in "price" and "day" (Prof says, and I don't do computing). When I try to that, RStudio tells me "Error in lag(rsi, 1) : object 'rsi' not found". Why is that? Is it not legal to create a function or a vector in a function? Or should I structure my code in a different way from the first one above? The code with function(price,day) is below: </p>
  
  <pre><code>library(quantmod)
  library(PerformanceAnalytics)
  getSymbols("IBM", src= "yahoo", from = "2000-01-01", to ="2015-09-25") #download IBM, from the stipulated range of dates
  
  size1&lt;-function(price,day){
    ris&lt;-RSI(price,day)
    ris&lt;-lag(rsi,1)
    rsi[is.na(rsi)] &lt;- 0
    holdings1&lt;-c()
    holdings1[1:day+1]&lt;-0
   for (i in day+2: length(price)){ #assume start buying from the 15th day onwards since we are using RSI, n=14
    if (rsi[i]&lt;30){ #if RSI&lt;30, we buy one share of IBM
      holdings1[i]&lt;-holdings1[i-1]+1  
    } else if (rsi[i]&lt;50){ # if 30&lt;RSI&lt;50, we don't buy or sell, so that the holdings does not change
      holdings1[i]&lt;-holdings1[i-1]
    } else 
      holdings1[i]&lt;-0 # sell all if RSI&gt;50
    size&lt;-reclass(holdings1,price)
    }
  }
  
  ret1&lt;-dailyReturn(Cl(IBM))*size1(Cl(IBM),14)
  charts.PerformanceSummary(ret1) 
  </code></pre>