Simple EMA Trading Strategy on Bitcoin

Example QBit to show a simple EMA trading strategy on Bitcoin.



library(quantmod)
library(PortfolioAnalytics)
library(lubridate)

backtest_years <- 5
from_date <- Sys.Date() - years(backtest_years)
btcusd <- getSymbols("BTC-USD", auto.assign = FALSE, from = from_date)

simple_backtest <- function(ema_length) {
  close_no_na <- na.locf(Cl(btcusd))
  ema <- EMA(close_no_na, ema_length)
  sig <- close_no_na > ema
  ret <- ROC(close_no_na, type = "discrete")
  ret_strat <- ret * lag(sig, 1)
  ret_all <- cbind(ret_strat, ret)
  colnames(ret_all) <- c(sprintf("EMA(%s)", ema_length), "BuyHold")
  charts.PerformanceSummary(ret_all)
  
  rbind( Return.annualized(ret_all),
         StdDev.annualized(ret_all),
         SharpeRatio.annualized(ret_all)
        )
}

simple_backtest(200)

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.