try(rm("order_book.RSI",pos=.strategy),silent=TRUE)
try(rm("account.RSI","portfolio.RSI",pos=.blotter),silent=TRUE)
try(rm("account.st","portfolio.st","stock.str","stratRSI","initDate","initEq",'start_t','end_t'),silent=TRUE)


library('xts')
library('quantstrat')
alldates <- c("2011-01-03","2011-01-04","2011-01-05","2011-01-06","2011-01-07",
"2011-01-10","2011-01-11","2011-01-12","2011-01-13","2011-01-14",
"2011-01-18","2011-01-19","2011-01-20","2011-01-21","2011-01-24",
"2011-01-25","2011-01-26","2011-01-27","2011-01-28","2011-01-31",
"2011-02-01","2011-02-02","2011-02-03","2011-02-04","2011-02-07",
"2011-02-08")
values <- 
c(117.6562,117.6875,117.0156,117.3359,117.9922,118.1172,117.9531,117.9531,
118.2734,118.1719,118.1016,118.2344,117.6562,117.8438,117.8672,118.2422,
117.9688,118.2891,118.5547,118.4141,118.0703,117.6875,117.3984,116.8516,
116.8828,116.3438)

FV = xts(values, as.Date(alldates))
colnames(FV) <- 'FV'

# Initialize a strategy object
stratRSI <- strategy("RSI")

# Add an indicator
stratRSI <- add.indicator(strategy = stratRSI, name = "RSI", arguments = list(price = quote(getPrice(mktdata, prefer='FV'))), label="RSI")

# There are two signals:
# The first is when RSI is greater than 90
stratRSI <- add.signal(strategy = stratRSI, name="sigThreshold",arguments = list(threshold=70, column="RSI",relationship="gt",cross=TRUE),label="RSI.gt.70") # The second is when RSI is less than 10
stratRSI <- add.signal(strategy = stratRSI, name="sigThreshold",arguments = list(threshold=30, column="RSI",relationship="lt",cross=TRUE),label="RSI.lt.30")

# There are two rules:
#'## we would Use osMaxPos to put trade on in layers, or to a maximum position. 

# The first is to sell when the RSI crosses above the threshold
stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = list(sigcol="RSI.gt.70", sigval=TRUE, orderqty=-1000, ordertype='market', orderside='short', pricemethod='market', replace=FALSE, prefer='FV'), type='enter', path.dep=TRUE)
stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = list(sigcol="RSI.lt.30", sigval=TRUE, orderqty='all', ordertype='market', orderside='short', pricemethod='market', replace=FALSE, prefer='FV'), type='exit', path.dep=TRUE)

# The second is to buy when the RSI crosses below the threshold

stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = list(sigcol="RSI.lt.30", sigval=TRUE, orderqty= 1000, ordertype='market', orderside='long', pricemethod='market', replace=FALSE, prefer='FV'), type='enter', path.dep=TRUE)

stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = list(sigcol="RSI.gt.70", sigval=TRUE, orderqty='all', ordertype='market', orderside='long', pricemethod='market', replace=FALSE, prefer='FV'), type='exit', path.dep=TRUE)

currency("USD")
symbols = c("FV")
for(symbol in symbols){ # establish trade-able instruments
    stock(symbol, currency="USD",multiplier=1)
}
initDate='2011-01-02'
initEq=100000
port.st<-'RSI' #use a string here for easier changing of parameters and re-trying

initPortf(port.st, symbols=symbols, initDate=initDate)
initAcct(port.st, portfolios=port.st, initDate=initDate)
initOrders(portfolio=port.st, initDate=initDate)

print("setup completed")

# Process the indicators and generate trades
start_t<-Sys.time()
out<-try(applyStrategy(strategy=stratRSI , portfolios=port.st, parameters=list(n=2), prefer='FV' ) )
end_t<-Sys.time()
print("Strategy Loop:")
print(end_t-start_t)
