<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#330000">
Dear Community,<br>
I've been experimenting with quantstrat for a few months and I
wanted to implement a simple momentum based ETF rotational strategy.
I'm stuck at this point and would need advice or pls redirect me to
the right threads/forums/etc if it is covered somewhere else. To put
simple, the question is: how to implement a _portfolio level total
equity_ aware order sizing function for a strategy with multiple
assets? The questions seems to me very obvious but I could not find
an example covering this issue, most of the dynamic order sizing
examples are based on the _initial equity_; excuse me if I have
missed something.<br>
<br>
I'll describe the problem in a simplified manner. Let's assume I
want to calculate CAGR for a momentum strategy with two assets (e.g.
SHY, SPY, initial equity is 10k USD); the strategy would always buy
the one with the most return in the previous 3 months (I have a
custom indicator for that); rules are fired ones a month and are
sized based on the total available equity: I want every order to
have the size of the (InitialEquity + PortfolioProfit) / ClosePrice.<br>
<br>
The problem is that I can't write a total equity-aware order sizing
function, because the rules are fired in the portfolio per symbol
and not per period. The updatePortfolio function inside of the order
sizing function for the SHY (first symbol) does not process the
returns of SPY (second symbol), the strategy processes returns from
SPY later.<br>
<br>
Is it possible to change the order of the rules or a completely
different approach is necessary?<br>
<br>
I'll attach a complete and working sample code if needed, but I
think a few lines of code will just make it. Here is the order
sizing function and the two rules:<br>
<br>
<br>
<br>
<i>osTotalEq <- function(timestamp,orderqty, portfolio, symbol,
ruletype, ...)</i><i><br>
</i><i>{</i><i><br>
</i><i> ClosePrice <- as.numeric(Cl(mktdata[timestamp,]))</i><i><br>
</i><i> tempPortfolio <- getPortfolio(portfolio.st)</i><i><br>
</i><i> dummy <- updatePortf(Portfolio=portfolio.st,
Dates=paste('::',as.Date(timestamp),sep=''))</i><i> </i><br>
<i><i> ### --> when the rule is fired in case of SHY (first
symbol in the portfolio), trading.pl will only contain P&L
from trading SHY but from SPY</i> </i><i> <br>
trading.pl <-
sum(getPortfolio(portfolio.st)$summary$Net.Trading.PL)</i><i> <br>
</i><i>
assign(paste("portfolio.",portfolio.st,sep=""),tempPortfolio,pos=.blotter)</i><i><br>
</i><i> total.equity <- initEq + trading.pl</i><i><br>
</i><i> orderqty <- total.equity/ClosePrice</i><i><br>
</i><i> return(orderqty)</i><i><br>
</i><i>}</i><br>
<br>
<i>stratRANK <- add.rule(strategy = stratRANK, name='ruleSignal',
arguments = list(sigcol="Rank.gt.N", sigval=TRUE, orderqty='all',
ordertype='market', orderside='long', pricemethod='market',
replace=FALSE), type='exit', path.dep=TRUE)</i><i><br>
</i><i><br>
</i><i>stratRANK <- add.rule(strategy = stratRANK,
name='ruleSignal', arguments = list(sigcol="Rank.lte.N",
sigval=TRUE, orderqty=1000, ordertype='market', orderside='long',
pricemethod='market', replace=FALSE, osFUN='osTotalEq'),
type='enter', path.dep=TRUE)</i><br>
<br>
<br>
<br>
<br>
Thanks in advance,<br>
Greg
</body>
</html>