Created
December 1, 2015 22:26
-
-
Save tomas-rampas/1d04aa97e0b9a93911ff 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
#required libraries | |
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 test environment | |
quotes <- new.env() | |
#load data from local disc, modify this in accordance with your env. | |
tickers<-spl('GBPJPY') | |
file.path<- "G:\\TickData\\" | |
#parsing dates helper function | |
fun <- function(d, t) as.chron(paste(strptime(d, "%Y.%m.%d"), t)) | |
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) | |
} | |
# remove missing values | |
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 = 21) | |
EMA.slow<-EMA(prices,n = 89) | |
#macd | |
MACD <- MACD(prices) | |
#crossover stategy, checking fast and slow MA crosses | |
crossover.pure.strategy <- iif(cross.up(EMA.fast,EMA.slow),1,iif(cross.dn(EMA.fast, EMA.slow), -1 ,NA)) | |
#crossover stategy, checking fast and slow MA crosses and filtered out noise by MACD | |
crossover.filtered.strategy <- iif(cross.up(EMA.fast,EMA.slow) & MACD$macd - MACD$signal > 0,1, | |
iif(cross.dn(EMA.fast, EMA.slow) & MACD$macd - MACD$signal < 0, -1 ,NA)) | |
#helper variable holding list of dates we will use later for plotting | |
effective_dates = '2015-08-15::2015-09-30' | |
#setting weights to neutral | |
quotes$weight[] = NA | |
#calculating weights which are effectively trades triggers | |
quotes$weight[] = crossover.pure.strategy | |
#set model for crossover signals applying pure crossover.strategy | |
models$apply.pure.crossover = bt.run.share(quotes, clean.signal=T, trade.summary = TRUE) | |
#re-setting weights to neutral | |
quotes$weight[] = NA | |
#calculating weights MA crossover filtered by MACD | |
quotes$weight[] = crossover.filtered.strategy | |
#set model for crossover signals applying filtered crossover.strategy | |
models$apply.filtered.crossover = bt.run.share(quotes, clean.signal=T, trade.summary = TRUE) | |
# TP and SL levels its R:R is 2:10! | |
Takeprofit = 10/100 | |
Stoploss = 2/100 | |
#this is simple method for TP/SL trade management | |
#it's taken from SIT web site | |
stop.loss.take.profit <- function(weight, price, tstart, tend, pstop, pprofit) { | |
index = tstart : tend | |
if(weight > 0) { | |
temp = price[ index ] < (1 - pstop) * cummax(price[ index ]) | |
temp = temp | price[ index ] > (1 + pprofit) * price[ tstart ] | |
} else { | |
temp = price[ index ] > (1 + pstop) * cummin(price[ index ]) | |
temp = temp | price[ index ] < (1 - pprofit) * price[ tstart ] | |
} | |
return( temp ) | |
} | |
#re-setting weights to neutral | |
quotes$weight[] = NA | |
#calculating weights of our MA crossover filtered by MACD strategy with Tp/SL levels | |
quotes$weight[] = custom.stop.fn(coredata(crossover.filtered.strategy), coredata(prices), stop.loss.take.profit,pstop = Stoploss, pprofit = Takeprofit) | |
#fill model with signals for our 3rd strategy | |
models$stop.loss.take.profit = 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.pure.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
Hello.
I used to use tick data provided by Dukascopy. It's easy to download through out their JForex platform you will need just DEMO account. Another option is to download data through the web interface but it's much slower then former one.