[R-SIG-Finance] quantstrat & custom indicators

algotr8der algotr8der at gmail.com
Tue May 3 18:57:31 CEST 2011


For a portfolio based approach like the one I described in my previous post
would it make more sense to define a portfolio per symbol and use a for/if
statement to loop through each symbol, create a portfolio for the symbol,
create rues for that symbol (i.e. num shares and direction of trade may
depend on the symbol) and call applyStrategy on that portfolio. The
initAcct() function can be called once all of the trades in each of the
portfolio's have been executed.

Here is an example (I have also attached the .R file for this code). There
seems to be various problems. Firstly, the number of shares executed does
not correspond to that specified in the If condition. For instance, only 348
shares should be traded in CAT (the 2nd symbol in the character vector
'symbols') and the entry position should be short. What happens is that the
opening trade is long 65 shares (which is what was intended for the previous
symbol i.e. IBM). This is followed by a trade that is short 348 shares
(which was intended for CAT). The same type of error progresses through the
remainder of the symbols.

I guess I am seeking guidance on the big picture for portfolio based
approaches as the direction and number of shares will depend on the symbol.

http://r.789695.n4.nabble.com/file/n3492994/basketStrategy.R
basketStrategy.R )
http://r.789695.n4.nabble.com/file/n3492994/error.txt error.txt 

require(quantstrat)
initDate = '2007-07-01'
endDate = '2007-08-30'
startdate = '2007-07-02'
initEq = 500000

symbols <- c("IBM", "CAT", "UTX", "MSFT")
currency("USD")

getSymbols(symbols, from=startdate, to=endDate)

stratBasket <- strategy('default')

stratBasket <- add.indicator(strategy = stratBasket, label="ma10", name =
"SMA", arguments = list(x=quote(Cl(mktdata)), n=10))
stratBasket <- add.signal(strategy = stratBasket, name = "sigCrossover",
arguments= list(column=c("Close", "ma10"), relationship="gt"),
label="close.gt.ma10")
stratBasket <- add.signal(strategy = stratBasket, name = "sigCrossover",
arguments= list(column=c("Close", "ma10"), relationship="lt"),
label="close.lt.ma10")


for (symbol in symbols) {
	if (symbol == 'IBM') {
	
	stock(symbol, currency="USD", multiplier=1)	
	portfolio.st = symbol
	initPortf(name=portfolio.st, symbols=symbol, initDate=initDate,
currency="USD")
	initOrders(portfolio=portfolio.st,initDate=initDate)
		
	numshares <- 65
	entertrade <- c("long")
	exittrade <- c("short")
	stratBasket <- add.rule(strategy = stratBasket, name='ruleSignal',
arguments = list(sigcol="close.gt.ma10",sigval=TRUE, orderqty=numshares,
ordertype='market', orderside=entertrade), type='enter')
	stratBasket <- add.rule(strategy = stratBasket, name='ruleSignal',
arguments = list(sigcol="close.lt.ma10",sigval=TRUE, orderqty=all,
ordertype='market', orderside=exittrade), type='exit')
	out<-try(applyStrategy(strategy=stratBasket , portfolios=portfolio.st))
		
	} else if (symbol == 'CAT') {
		
	stock(symbol, currency="USD", multiplier=1)	
	portfolio.st = symbol
	initPortf(name=portfolio.st, symbols=symbol, initDate=initDate,
currency="USD")
	initOrders(portfolio=portfolio.st,initDate=initDate)
			
	numshares <- -348
	entertrade <- c("short")
	exittrade <- c("long")
	stratBasket <- add.rule(strategy = stratBasket, name='ruleSignal',
arguments = list(sigcol="close.gt.ma10",sigval=TRUE, orderqty=numshares,
ordertype='market', orderside=entertrade), type='enter')
	stratBasket <- add.rule(strategy = stratBasket, name='ruleSignal',
arguments = list(sigcol="close.lt.ma10",sigval=TRUE, orderqty=all,
ordertype='market', orderside=exittrade), type='exit')
	out<-try(applyStrategy(strategy=stratBasket , portfolios=portfolio.st))
		
	} else if (symbol == 'UTX') {
		
	stock(symbol, currency="USD", multiplier=1)		
	portfolio.st = symbol
	initPortf(name=portfolio.st, symbols=symbol, initDate=initDate,
currency="USD")
	initOrders(portfolio=portfolio.st,initDate=initDate)
		
	numshares <- 648
	entertrade <- c("long")
	exittrade <- c("short")
	stratBasket <- add.rule(strategy = stratBasket, name='ruleSignal',
arguments = list(sigcol="close.gt.ma10",sigval=TRUE, orderqty=numshares,
ordertype='market', orderside=entertrade), type='enter')
	stratBasket <- add.rule(strategy = stratBasket, name='ruleSignal',
arguments = list(sigcol="close.lt.ma10",sigval=TRUE, orderqty=all,
ordertype='market', orderside=exittrade), type='exit')
	out<-try(applyStrategy(strategy=stratBasket , portfolios=portfolio.st))
		
	} else if (symbol == 'MSFT') {
		
	stock(symbol, currency="USD", multiplier=1)		
	portfolio.st = symbol
	initPortf(name=portfolio.st, symbols=symbol, initDate=initDate,
currency="USD")
	initOrders(portfolio=portfolio.st,initDate=initDate)
		
	numshares <- -1000
	entertrade <- c("short")
	exittrade <- c("long")
	stratBasket <- add.rule(strategy = stratBasket, name='ruleSignal',
arguments = list(sigcol="close.gt.ma10",sigval=TRUE, orderqty=numshares,
ordertype='market', orderside=entertrade), type='enter')
	stratBasket <- add.rule(strategy = stratBasket, name='ruleSignal',
arguments = list(sigcol="close.lt.ma10",sigval=TRUE, orderqty=all,
ordertype='market', orderside=exittrade), type='exit')
	out<-try(applyStrategy(strategy=stratBasket , portfolios=portfolio.st))
		
	}
}

account.st <- c("basket")
initAcct(account.st, portfolios=symbols, initDate=initDate, initEq=initEq)



--
View this message in context: http://r.789695.n4.nabble.com/quantstrat-custom-indicators-tp3491259p3492994.html
Sent from the Rmetrics mailing list archive at Nabble.com.



More information about the R-SIG-Finance mailing list