# [R] Trading Strategy and Bootstrap

Schiele, Erik Erik.Schiele at bnymellon.com
Thu Dec 10 23:30:09 CET 2015

```Hi,

I'm beginning to fool around in R for trading strategy purposes. To keep it simple, I have only played with stock data at this point.

I have created a simple trend following strategy (in blue). Given my statistical background, I am attempting to bootstrap the results and table the same parameters highlighted below with no luck (in green). Any ideas on what I could do differently?

library(quantmod)
library(PerformanceAnalytics)

b <- get(getSymbols('SPY'))["2011::"]
s <- get(getSymbols('GLD'))["2011::"]
b\$sma1 <- SMA(Cl(s) , 1)
s\$sma50 <- SMA(Cl(s) , 50)
s\$position <- ifelse(Cl(s) > s\$sma50 , 1 , -1)
myReturn <- lag(s\$position) * dailyReturn(s)

table.Drawdowns(s\$position, top = 5, digits = 1)
table.Stats(s\$position, ci = 0.95, digits = 2)
table.SpecificRisk(s\$position, b\$sma1, Rf = 0, digits = 2)
table.Correlation(s\$position, b\$sma1)

charts.PerformanceSummary(cbind(dailyReturn(s),myReturn))

N     = 100 # Number of simulations
Loop  = mat.or.vec(N,2,1,1,1)
for (i in 1:N){

# sample with replacement from return distribution of index
s.new = (sample(s, length(s), replace = T, prob = NULL))
# demeaning returns
s.new = s.new-mean(s)
# new price series starting at same value as original series
prices.new = xts(prices[[1]]*exp(cumsum(s.new)))

# define strategies
# mean reversion
s\$sma50.new  = SMA(Cl(s.new) , 50)

# mean reversion
s\$position.new <- ifelse(Cl(s) > s\$sma50.new , 1 , -1)

# replace missing values with zeros
s\$position.new[is.na(s\$position.new)]   = 0

Loop[i,1] = if (mean(s\$position.new)  > mean(s\$sma50.new)) {1}else{0}
}

#Loop

# plots simulated series
returns.new = cbind(s\$sma50.new, cumsum(s\$sma50.new))

chart.CumReturns(returns.new,s\$sma50.new,geometric=F)

Erik Schiele
Vice President
Money Markets Trading, Originations and Sales
101 Barclay St, NY NY 10007 3rd Floor
BNY Mellon Capital Markets, LLC
Main Desk 212-815-8222

This is for informational purposes only; from sources the Firm believes reliable; may not be accurate or complete; is subject to change;  is not a recommendation or offer to buy/sell a financial instrument or adopt any investment strategy; is not legal, tax, credit or accounting advice.  Do not use e-mail to submit any instructions - acceptances are at your risk. The Firm or its affiliates lends to, borrows from and provides other products/services to issuers and others, receives compensation therefore, and periodically has a direct or indirect financial interest in the financial instruments/transactions indicated.  Additional risks may exist that are not referenced. Past performance is not indicative of future returns. Other than CDs or CDARS, financial instruments: are not FDIC insured; are not deposits or other obligations of and are not guaranteed by the Firm or any bank or non-bank affiliate; and involve investment risk including possible loss of principal. The Firm is a wholly owned, indirect non-bank subsidiary of The Bank of New York Mellon Corporation, and a member of FINRA and SIPC, and is solely responsible for its obligations and commitments.

The information contained in this e-mail, and any attachment, is confidential and is intended solely for the use of the intended recipient. Access, copying or re-use of the e-mail or any attachment, or any information contained therein, by any other person is not authorized. If you are not the intended recipient please return the e-mail to the sender and delete it from your computer. Although we attempt to sweep e-mail and attachments for viruses, we do not guarantee that either are virus-free and accept no liability for any damage sustained as a result of viruses.

Please refer to http://disclaimer.bnymellon.com/eu.htm for certain disclosures relating to European legal entities.
[[alternative HTML version deleted]]

```