9.4 Buy and Hold
We want to compare buy and hold strategy.
We first get the data
getSymbols("SPY", from=from, to=to, adjust=TRUE)
9.4.1 Step 1: Initialization
rm.strat("buyHold")
#Initial Setup
initPortf("buyHold", "SPY", initDate = initDate)
initAcct("buyHold", portfolios = "buyHold",
initDate = initDate, initEq = initEq)
9.4.2 Steps 2-4: Applying trading rule
Since buy and sell are not given by trading rule, we directly add transaction to it.
We first add the transaction to buy at the beginning:
FirstDate <- first(time(SPY))
# Enter order on the first date
BuyDate <- FirstDate
equity <- getEndEq("buyHold", FirstDate)
FirstPrice <- as.numeric(Cl(SPY[BuyDate,]))
UnitSize = as.numeric(trunc(equity/FirstPrice))
addTxn("buyHold", Symbol = "SPY",
TxnDate = BuyDate, TxnPrice = FirstPrice,
TxnQty = UnitSize, TxnFees = 0)
We first add the transaction to sell at the end:
LastDate <- last(time(SPY))
# Exit order on the Last Date
LastPrice <- as.numeric(Cl(SPY[LastDate,]))
addTxn("buyHold", Symbol = "SPY",
TxnDate = LastDate, TxnPrice = LastPrice,
TxnQty = -UnitSize , TxnFees = 0)
9.4.3 Step 5: Evaluation
We update our portfolio, account, and equity:
updatePortf(Portfolio = "buyHold")
updateAcct(name = "buyHold")
updateEndEq(Account = "buyHold")
After updates, we can plot the trading position.
chart.Posn("buyHold", Symbol = "SPY")
Now we are ready to compare buy-hold and the simple trading rule.
# Compare strategy and market
rets <- PortfReturns(Account = account.st)
rets.bh <- PortfReturns(Account = "buyHold")
returns <- cbind(rets, rets.bh)
charts.PerformanceSummary(
returns, geometric = FALSE,
wealth.index = TRUE,
main = "Strategy vs. Market")