-
Notifications
You must be signed in to change notification settings - Fork 4
Strategy code template
milktrader edited this page Apr 30, 2014
·
2 revisions
#!/usr/bin/Rscript --vanilla
#
# Golden Cross trading system
# copyright (c) 2009-2014, Golda Krauss
############################# GET DATA ######################################
data(spx)
############################# DEFINE VARIABLES ##############################
port <- 'GoldenCrossPort'
acct <- 'GoldenCrossAcct'
initEq <- 1e6
initDate <- '1969-12-31'
fast <- 50
slow <- 200
############################# INITIALIZE ####################################
currency('USD')
stock('spx',currency='USD', multiplier=1)
initPortf(port, 'spx', initDate=initDate)
initAcct(acct, port, initEq=initEq, initDate=initDate)
initOrders(port, initDate=initDate )
goldencross <- strategy(port)
############################# MAX POSITION LOGIC ############################
addPosLimit(
portfolio=port,
symbol='spx',
timestamp=initDate,
maxpos=100)
############################# INDICATORS ####################################
goldencross <- add.indicator(
strategy = goldencross,
name = 'SMA',
label = 'ma50' )
arguments = list(x=quote(Cl(mktdata)),
n=slow))
goldencross <- add.indicator(
strategy = golencross,
name = 'SMA',
arguments = list(x=quote(Cl(mktdata)),
n=fast),
label = 'ma200' )
############################# SIGNALS #######################################
bumblebee <- add.signal(
strategy = goldencross,
name = 'sigCrossover',
arguments = list(columns=c('ma50','ma200'),
relationship='lt'),
label = 'fast.lt.dn')
bumblebee <- add.signal(
strategy = golden cross,
name = 'sigCrossover',
arguments = list(columns=c('ma50','ma200'),
relationship='gt'),
label = 'fast.gt.up')
############################# RULES #########################################
goldencross <- add.rule(
strategy = goldencross,
name = 'ruleSignal',
arguments = list(sigcol = 'fast.gt.up',
sigval = TRUE,
orderqty = 100,
ordertype = 'market',
orderside = 'long',
osFUN = 'osMaxPos'),
type = 'enter',
label = 'EnterLONG')
goldencross <- add.rule(
strategy = goldencross,
name = 'ruleSignal',
arguments = list(sigcol = 'fast.lt.dn',
sigval = TRUE,
orderqty = 'all',
ordertype = 'market',
orderside = 'long'),
type = 'exit',
label = 'ExitLONG')
############################# APPLY STRATEGY ################################
applyStrategy(goldencross, port, prefer='Open', verbose=FALSE)
############################# UPDATE ########################################
updatePortf(port, 'spx', Date=paste('::',as.Date(Sys.time()),sep=''))
updateAcct(acct)