[R-SIG-Finance] !SPAM: Re: quantstrat: order sizing question

km krish.maheswaran at gmail.com
Tue Jun 29 04:21:32 CEST 2010


Hi Brian:

Thanks for the response. The warning and code are below.

Cheers,
Krish





Warning messages:
1: In max(i) : no non-missing arguments to max; returning -Inf


#-------------------------------------------------------------
#Code Below
#------------------------------------------------------------

library(quantstrat)

#set initial parameters

myPortfolioName <- "longtrend"
myPortfolioStartingDate <- "1997-12-31"
myPortfolioCurrency <- "USD"
myPortfolioStartingEquity <- 1000000
myPortfolioOrderQuantity <- 100
myPortfolioSymbols <- c("XLF")
myStrategyName <- myPortfolioName





#----------------------------------------------------------------------------------

#set currency
currency(myPortfolioCurrency) 
#set tickers
symbols <- myPortfolioSymbols
#set each tickers financial instrument & multiplier
for(symbol in symbols){ stock(symbol, currency="USD", multiplier=1)}
#must specify the time class explicitely
getSymbols(symbols,src='yahoo', index.class=c("POSIXt","POSIXct"),
from='1998-01-01')


#convert data into monthly data - this is not necessary - build strategy so
it can handle all frequencies
for(symbol in symbols){
	x <-get(symbol)
	x<-to.monthly(x,indexAt='lastof',time=TRUE)
	colnames(x)<-gsub("x",symbol,colnames(x))
	assign(symbol,x)
}

#initialize portfolio containers
initPortf(name=myPortfolioName, symbol=symbols, initPosQty=0,
initDate=myPortfolioStartingDate)
initAcct(name=myPortfolioName, portfolios=myPortfolioName,
initDate=myPortfolioStartingDate, initEq=myPortfolioStartingEquity)
initOrders(portfolio=myPortfolioName, initDate=myPortfolioStartingDate)

#set strategy object
s <- strategy(myStrategyName)

#associate strategy object with indicators, signals, and rules

fixedOrderQty <- myPortfolioOrderQuantity

s <- add.indicator(strategy=s, name="SMA",
arguments=list(x=quote(Cl(mktdata)), n=10), label="SMA10")

s <- add.signal(strategy=s, name="sigCrossover",
arguments=list(data=quote(mktdata), columns=c("Close", "SMA10"),
relationship="gt"), label="C1.gt.SMA")
s <- add.signal(strategy=s, name="sigCrossover",
arguments=list(data=quote(mktdata), columns=c("Close", "SMA10"),
relationship="lt"), label="C1.lt.SMA")

#-------------------------------------
myOS <- function(orderqty,...){

      orderqty <- orderqty[1]*orderqty[2]
      return(orderqty)
}
#------------------------------------------

s <- add.rule(s, name='ruleSignal', arguments = list(data=quote(mktdata),
sigcol="Cl.gt.SMA", sigval=TRUE, orderqty=c(1000,2), ordertype='market',
orderside='long', pricemethod='market'), osFUN="myOS", type='enter',
path.dep=TRUE)

s <- add.rule(strategy=s, name="ruleSignal",
arguments=list(data=quote(mktdata), sigcol="C1.lt.SMA", sigval=T,
orderqty="all", ordertype="market", orderside=NULL, threshold=NULL),
type="exit")


#apply strategy
# Process the indicators and generate trades
start_t<-Sys.time()
out<-try(applyStrategy(strategy='s' , portfolios=myPortfolioName))
end_t<-Sys.time()
print("Strategy Loop:")
print(end_t-start_t)

-- 
View this message in context: http://r.789695.n4.nabble.com/quantstrat-order-sizing-question-tp2271635p2271666.html
Sent from the Rmetrics mailing list archive at Nabble.com.



More information about the R-SIG-Finance mailing list