Skip to content

Instantly share code, notes, and snippets.

@josephdunn
Created February 15, 2013 21:49
Show Gist options
  • Save josephdunn/4963829 to your computer and use it in GitHub Desktop.
Save josephdunn/4963829 to your computer and use it in GitHub Desktop.
suppressMessages(require(quantstrat))
Sys.setenv(TZ='America/New_York')
# define variables
sym <- 'AAPL'
initDate <- '2013-01-22'
endDate <- '2013-02-08'
port <- 'z'
acct <- 'monopoly'
initEq <- 100000
# get data
getSymbols(sym, from=initDate, to=endDate)
# initialize
currency('USD')
stock(sym, currency='USD', multiplier=1)
initPortf(port, sym, initDate=initDate)
initAcct(acct, port, initEq=initEq, initDate=initDate)
initOrders(port, initDate=initDate)
z <- strategy(port)
# max position logic
addPosLimit(portfolio=port,
symbol=sym,
timestamp=initDate,
maxpos=100)
# indicators
# signals
z <- add.signal(strategy = z,
name = 'sigThreshold',
arguments = list(column='Close',
threshold=500,
relationship='lt',
cross=TRUE),
label = 'up')
# rules
z <- add.rule(strategy = z,
name = 'ruleSignal',
arguments = list(sigcol = 'up',
sigval = TRUE,
orderqty = 100,
ordertype = 'market',
orderside = 'long',
osFUN = 'osMaxPos'),
type = 'enter',
label = 'EnterLONG')
# apply strategy
applyStrategy(z, port, verbose=FALSE)
# update portfolio
updatePortf(port, sym, Date=paste('::', as.Date(Sys.time()), sep=''))
updateAcct(acct)
## assorted data
book <- getOrderBook(port)
stats <- tradeStats(port)
rets <- PortfReturns(acct)
txns <- getTxns(port, sym)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment