Last active
May 23, 2016 11:20
-
-
Save tomas-rampas/0013cd166d16576a2f8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(quantmod) | |
library(zoo) | |
library(chron) | |
#following section loads "Systematic Investor Toolbox" | |
setInternet2(TRUE) | |
con <- gzcon(url('https://github.com/systematicinvestor/SIT/raw/master/sit.gz', 'rb')) | |
source(con) | |
close(con) | |
#creates new environemnt | |
quotes <- new.env() | |
#load data from local disc: | |
tickers<-spl('GBPJPY') | |
file.path<- "G:\\TickData\\" | |
fun <- function(d, t) as.chron(paste(strptime(d, "%Y.%m.%d"), t))#parsing dates helper function | |
for(n in tickers) { | |
quotes[[n]] = as.xts(read.zoo(read.csv( | |
file=paste(file.path, n, '_H1_UTC+0_00_noweekends_fxmtf.csv', sep=''), | |
header=F, col.names=c("Date", "Time", "Open","High","Low","Close","Volume")), header=T, index=1:2, FUN=fun)) | |
# fill missing values | |
quotes[[n]] = na.locf(quotes[[n]], fromLast=TRUE) | |
} | |
bt.prep(quotes, align='remove.na') | |
#create prices and prepare models | |
prices <- quotes$prices | |
models <- list() | |
#technical indicators, exp. moving averages | |
EMA.fast<-EMA(prices,n = 8) | |
EMA.slow<-EMA(prices,n = 89) | |
#crossover stategy, checking fast and slow MA crosses | |
crossover.strategy <- iif(cross.up(EMA.fast,EMA.slow),1,iif(cross.dn(EMA.fast, EMA.slow), -1 ,NA)) | |
#helper variable holding list of dates we will use later for plotting | |
effective_dates = '2015-08-15::2015-10-30' | |
#setting weights to neutral | |
quotes$weight[] = NA | |
#claculating weights which are effectively trades triggers | |
quotes$weight[] = crossover.strategy | |
#set model for crossover signals and apply our crossover.strategy | |
models$apply.crossover = bt.run.share(quotes, clean.signal=T, trade.summary = TRUE) | |
#we'd like to see our MAs plotted later | |
extra.plot.fn <- function() { | |
plota.lines(EMA.fast, col='red') | |
plota.lines(EMA.slow, col='blue') | |
} | |
#plot strategy, using dates range defines above and extra plots | |
bt.stop.strategy.plot(quotes, models$apply.crossover, dates = effective_dates, layout=F, main = 'Crossover', extra.plot.fn = extra.plot.fn, plotX = T) | |
#plot performance | |
strategy.performance.snapshoot(models, T) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment