Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #rm(list=ls())
- require(quantstrat)
- suppressWarnings(rm("order_book.macd",pos=.strategy))
- suppressWarnings(rm("account.macd","portfolio.macd",pos=.blotter))
- suppressWarnings(rm("accnt","p","ticker","stratMACD","initDate","initEq",'start_t','end_t'))
- ticker='QQQ'
- getSymbols("QQQ", src='yahoo', index.class=c("POSIXt","POSIXct"), from='2012-01-01')
- #Loads QQQ DATA
- initDate = '1990-01-02'
- currency(c('USD'))
- stock(ticker,'USD')
- p='macd'
- accnt='macd'
- qty = 10000
- initPortf(p,symbols=ticker, initDate=initDate)
- initAcct(accnt,portfolios=p, initDate=initDate)
- initOrders(portfolio=p,initDate=initDate)
- strat<-p
- # define the strategy
- strategy(strat, store=TRUE)
- # *********************************************************
- # ATR Functions
- #profit
- test1 <- function(HLC, n=2, maType, mmstp=2){test1 <- 2 * ATR(HLC=HLC, n=n, maType=maType)}
- #stop loss
- # must use the [,1] in order to apply the multiplication only to the "tr" column. without this,
- # the entire frame gets multiplied by -1. Also, this prevents creating another trueHigh and
- # trueLow column
- test2 <- function(HLC, n=2, maType, mmstp=2){test2 <- -1* ATR(HLC=HLC, n=n, maType=maType)[,1]}
- # *********************************************************
- # INDICATORS
- # ATR used for stop (see notes above)
- add.indicator(strat, name = "test1", arguments = list(HLC=quote(HLC(mktdata)[,1:3]), n=2, maType="SMA", mmstp=2))
- # must use the [,c(1,3,5)] to get the real HLC data. When the first ATR indicator is run, it populates trueHigh and trueLow
- # data fields. the code doesn't know which column to look at for "low" and "high", since there are multiple instances of it
- # now due to the first run of the function on HLC data. Thus, this bracketing tells it what columns to look for. if you
- # had another function to the same call, you may need to run a "head(HLC(mktdata))" to understand what the columns look like
- # before the 3rd indicator call. this solution is from: https://stat.ethz.ch/pipermail/r-sig-finance/2012q4/011103.html
- add.indicator(strat, name = "test2", arguments = list(HLC=quote(HLC(mktdata)[,c(1,3,5)]), n=2, maType="SMA", mmstp=2))
- #Highest close of the last n days
- add.indicator(strat,name="runMax", arguments=list(x=quote(Cl(mktdata)),n=50,cumulative=FALSE), label="RmxrCompare")
- # *********************************************************
- # SIGNALS
- # BUY if highest close of last n days is greater than today's close
- add.signal(strat,name='sigComparison', arguments=list(columns=c("Close","RmxrCompare"), relationship="gte"),label='long') # Signal to BUY
- # *********************************************************
- # RULES
- #Enters LONG position
- add.rule(strat, name = 'ruleSignal', arguments=list(sigcol='long', sigval=TRUE, orderside='long', ordertype='market',
- orderqty=qty, orderset='ocolong'),
- type='enter', label='EnterLONG', storefun=FALSE)
- # Stop Loss for LONG position. Stops if beyond some multiple of ATR (see notes above)
- add.rule(strat,name='ruleSignal', arguments = list(sigcol='long',sigval=TRUE, orderqty='all', ordertype='stoplimit',
- orderside='long', threshold='test1.ind.tr', tmult=FALSE, orderset='ocolong'),
- type='risk',label='stoplimitexit-LONG',storefun=FALSE)
- #end rules
- ####
- #### ***************************************************************************************************************************************************************
- applyStrategy(strat, portfolios=p,prefer='Open', verbose=FALSE)
- updatePortf(p, Symbols=ticker, ,Dates=paste('::',as.Date(Sys.time()),sep=''))
- print("Complete")
- print("********")
- print("********")
- print("********")
- print(head(mktdata))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement