Created
August 9, 2011 00:07
-
-
Save josephdunn/1133098 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
require(quantstrat) | |
# clean up | |
rm(list=ls(all=TRUE, pos=.blotter), pos=.blotter) | |
rm(list=ls(all=TRUE, pos=.instrument), pos=.instrument) | |
rm(list=ls(all=TRUE, pos=.strategy), pos=.strategy) | |
# set initial values | |
initDate <- '2011-08-05 06:00:00' | |
initEq <- 10000 | |
# set up instruments | |
currency('USD') | |
symbols <- c('SET') | |
stock('SET', 'USD') | |
# load data | |
SET <- as.xts(read.zoo('SET.csv', header=TRUE, sep=',', tz='')) | |
SPREAD <- as.xts(read.zoo('SPREAD.csv', header=TRUE, sep=',', tz='')) | |
colnames(SET) <- 'SET' | |
colnames(SPREAD) <- 'SPREAD' | |
# initialize a strategy object | |
stratSynth <- strategy('synth') | |
# indicator | |
stratSynth <- add.indicator(strategy = stratSynth, name='merge', arguments=list(x=quote(mktdata), y=SPREAD), label='SPREAD') | |
# buy SET if SPREAD more than 0.5% negative | |
stratSynth <- add.signal(strategy = stratSynth, name='sigThreshold', arguments = list(column='SPREAD', threshold=-0.005, relationship='lt', cross=TRUE), label='buy_SET') | |
stratSynth <- add.rule(strategy = stratSynth, name='ruleSignal', arguments = list(sigcol='buy_SET', sigval=TRUE, symbol='SET', orderqty=100, ordertype='market', orderside='long', pricemethod='market', replace=FALSE), type='enter', path.dep=TRUE, parameters=list(symbol='SET')) | |
# sell SET if SPREAD more than 0.5% positive | |
stratSynth <- add.signal(strategy = stratSynth, name='sigThreshold', arguments = list(column='SPREAD', threshold=0.005, relationship='gt', cross=TRUE), label='sell_SET') | |
stratSynth <- add.rule(strategy = stratSynth, name='ruleSignal', arguments = list(sigcol='sell_SET', sigval=TRUE, symbol='SET', orderqty=100, ordertype='market', orderside='short', pricemethod='market', replace=FALSE), type='enter', path.dep=TRUE) | |
# close any open orders if we're in the middle | |
stratSynth <- add.signal(strategy = stratSynth, name='sigThreshold', arguments = list(column='SPREAD', threshold=-0.005, relationship='gte', cross=TRUE), label='close_buy_SET') | |
stratSynth <- add.rule(strategy = stratSynth, name='ruleSignal', arguments = list(sigcol='close_buy_SET', sigval=TRUE, symbol='SET', orderqty=100, ordertype='market', pricemethod='market', replace=FALSE), type='exit', path.dep=TRUE) | |
stratSynth <- add.signal(strategy = stratSynth, name='sigThreshold', arguments = list(column='SPREAD', threshold=0.005, relationship='lte', cross=TRUE), label='close_sell_SET') | |
stratSynth <- add.rule(strategy = stratSynth, name='ruleSignal', arguments = list(sigcol='close_sell_SET', sigval=TRUE, symbol='SET', orderqty=100, ordertype='market', pricemethod='market', replace=FALSE), type='exit', path.dep=TRUE) | |
portfolio.st <- 'synthPortfolio' | |
account.st <- 'synthAcct' | |
# Initialize portfolio and account | |
initPortf(name=portfolio.st, symbols=symbols, initDate=initDate) | |
initAcct(name=account.st, portfolios=portfolio.st, initDate=initDate, initEq=initEq) | |
initOrders(portfolio=portfolio.st, symbols=symbols, initDate=initDate) | |
# process the indicators and generate trades | |
out <- applyStrategy(strategy=stratSynth, portfolios=portfolio.st) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment