<span class="Apple-style-span" style="font-family: Verdana, Geneva, Helvetica, Arial, sans-serif; font-size: 13px; "><span>In your code I changed </span><br><br><span>updateAcct(name=<a href="http://account.st">account.st</a>, Dates='2007-07-01::2010-07-03') #old </span><br>
<br><span>to start on the startDate instead of initDate: </span><br><br><span>updateAcct(name=<a href="http://account.st">account.st</a>, Dates='2007-07-02::2010-07-03') #new </span><br><br></span><div><span class="Apple-style-span" style="font-family: Verdana, Geneva, Helvetica, Arial, sans-serif; font-size: 13px; ">The updateEndEq function is finding the location of the first row and then subtracting 1 which returns</span></div>
<div><span class="Apple-style-span" style="font-family: Verdana, Geneva, Helvetica, Arial, sans-serif; font-size: 13px; "><br></span></div><div><span class="Apple-style-span" style="font-family: Verdana, Geneva, Helvetica, Arial, sans-serif; font-size: 13px; ">integer(0)</span></div>
<div><span class="Apple-style-span" style="font-family: Verdana, Geneva, Helvetica, Arial, sans-serif; font-size: 13px; "><br></span></div><div><span class="Apple-style-span" style="font-family: Verdana, Geneva, Helvetica, Arial, sans-serif; font-size: 13px; ">Then when it calls getEndEq with that value, 0 is returned.</span></div>
<div><span class="Apple-style-span" style="font-family: Verdana, Geneva, Helvetica, Arial, sans-serif; font-size: 13px; "><br><span>So, in the updateEndEq function I changed </span><br><br><span>if (is.null(Dates)) </span><br>
<span> Dates = time(Account$summary) #old </span><br><br><span>to: </span><br><br><span>if (is.null(Dates)) </span><br><span> Dates = time(Account$summary)[-1] #new </span><br><br><span>Now your code works. See attached</span><br>
</span><div><div><span class="Apple-style-span" style="font-family: Verdana, Geneva, Helvetica, Arial, sans-serif; font-size: 13px; "><br><span>I don't know if that fix makes sense or if it will cause problems other places in quantstrat, but it seems to work here.</span></span><div>
<font class="Apple-style-span" face="Verdana, Geneva, Helvetica, Arial, sans-serif"><br></font></div><div><font class="Apple-style-span" face="Verdana, Geneva, Helvetica, Arial, sans-serif">Garrett</font></div><div><span></span><font class="Apple-style-span" face="Verdana, Geneva, Helvetica, Arial, sans-serif"><br>
</font><div class="gmail_quote">On Fri, May 6, 2011 at 2:24 PM, s <span dir="ltr"><<a href="mailto:algotr8der@gmail.com">algotr8der@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
On 5/6/11 8:31 AM, Brian G. Peterson wrote:<br>
> Looks like a bug in initAcct. My suspicion is that if the first row of<br>
> the Acct table had the initiEq, all else would work fine.<br>
><br>
> We changed some things around there a couple months ago to deal with<br>
> multi-currency accounts (accounts with a different measurement currency<br>
> than portfolios) and with vectorizing as many of the calculations as<br>
> possible. I'll look at the older code and see what changed.<br>
><br>
> Thanks for the report. As an institutional investor, I basically never<br>
> think about account equity.<br>
><br>
> I'll try to get back to you later today once I've fixed it.<br>
><br>
> Cheers,<br>
><br>
> - Brian<br>
Thank you Brian for taking a look at this. I appreciate your time.<br>
<br>
This problem feeds into other functionalities, which you probably<br>
already know but I thought I would provide more details just in case.<br>
<br>
For example:<br>
<br>
charts.PerformanceSummary(ROC(getAccount(<a href="http://account.st" target="_blank">account.st</a>)$summary$End.Eq)[-1])<br>
<br>
What happens is that the call to log(x), where x is the daily equity<br>
value produces -Inf for Log(0) and NaN for log(-negative values). As a<br>
result the log(x) values you see below are fed to chart.TimeSeries where<br>
it fails.<br>
<br>
exiting from: checkData(R)<br>
Error in plot.window(xlim, ylim, xaxs = "r", log = logaxis) :<br>
need finite 'ylim' values<br>
<br>
debug: {<br>
if (type == "discrete") {<br>
roc <- x/lag(x, n, na.pad = na.pad) - 1<br>
}<br>
if (type == "continuous") {<br>
roc <- diff(log(x), n, na.pad = na.pad)<br>
}<br>
reclass(roc, x)<br>
}<br>
<br>
----snippet of log(end equity) below ---<br>
<br>
2008-09-30 -Inf<br>
2008-10-01 -Inf<br>
2008-10-02 -Inf<br>
2008-10-03 -Inf<br>
2008-10-06 -Inf<br>
2008-10-07 -Inf<br>
2008-10-08 -Inf<br>
2008-10-09 -Inf<br>
2008-10-10 8.191097<br>
2008-10-13 NaN<br>
2008-10-14 NaN<br>
2008-10-15 NaN<br>
2008-10-16 NaN<br>
2008-10-17 NaN<br>
2008-10-20 NaN<br>
2008-10-21 NaN<br>
2008-10-22 NaN<br>
<br>
----snippet of end equity below ----<br>
<br>
2008-09-30 0.00<br>
2008-10-01 0.00<br>
2008-10-02 0.00<br>
2008-10-03 0.00<br>
2008-10-06 0.00<br>
2008-10-07 0.00<br>
2008-10-08 0.00<br>
2008-10-09 0.00<br>
2008-10-10 3608.68<br>
2008-10-13 -3053.23<br>
2008-10-14 -2154.78<br>
2008-10-15 -2518.47<br>
2008-10-16 -705.72<br>
2008-10-17 -671.87<br>
2008-10-20 -20.29<br>
2008-10-21 -1837.16<br>
2008-10-22 -4495.50<br>
<br>
This leads me to think about the situation where initial equity is set<br>
correctly but draw downs have wiped out the equity and are again<br>
producing negative end equity values. In real life this wouldnt be an<br>
issue as your trading would end if that were to happen. But for purposes<br>
of backtesting I could set an initial equity value that could be easily<br>
wiped out by draw downs. Not sure if you want to address that or leave<br>
it up to the user to set initial equity values that are appropriate for<br>
the size of their trading.<br>
<div><div></div><div class="h5">> On Thu, 2011-05-05 at 17:51 -0700, algotr8der wrote:<br>
>> Okay - here is a bogus strategy just to test this -<br>
>><br>
>> <a href="http://r.789695.n4.nabble.com/file/n3500326/bogusStrategy.R" target="_blank">http://r.789695.n4.nabble.com/file/n3500326/bogusStrategy.R</a> bogusStrategy.R<br>
>><br>
>> a) portfolio_1 = IBM, UTX, CAT<br>
>><br>
>> b) portfolio_2 = MSFT<br>
>><br>
>> Indicators = 10 DSMA, 15 DSMA, 20 DSMA<br>
>><br>
>> Rules<br>
>><br>
>> a) if 10_DSMA Crosses Above 20_DSMA BUY stocks in portfolio_1 AND COVER<br>
>> existing SHORTS in portfolio_1<br>
>> b) if 10_DSMA Crosses Below 20_DSMA SELL existing long positions in<br>
>> portfolio_1 AND SHORT all stocks in portfolio_1<br>
>><br>
>> c) if 15_DSMA Crosses Above 20_DSMA BUY stocks in portfolio_2 AND COVER<br>
>> existing SHORTS in portfolio_2<br>
>> d) if 15_DSMA Crosses Below 20_DSMA SELL existing long positions in<br>
>> portfolio_2 AND SHORT all stocks in portfolio_2<br>
>><br>
>> Once you have updated the portfolios and accounts type the following:<br>
>><br>
>>> getAccounts(<a href="http://account.st" target="_blank">account.st</a>)<br>
>> Look at the 'End.Eq' column (this is the last column). You will see that the<br>
>> portfolio begins with 0 as the initial value and each day's p/l is added to<br>
>> 0 until the last day.<br>
>><br>
>> 2010-07-02 -989.49 85836.10<br>
>><br>
>> Maybe I am misunderstanding something but it appears to me that the p/l of<br>
>> this strategy = 85836.10 and so that amount should be added to the initEq.<br>
>><br>
>> In my real strategy I know the p/l is ~34800, which should be added to the<br>
>> initEq.<br>
>><br>
>><br>
>><br>
>><br>
>><br>
>><br>
>> --<br>
>> View this message in context: <a href="http://r.789695.n4.nabble.com/EndEquity-lower-than-initEq-despite-positive-p-l-tp3499089p3500326.html" target="_blank">http://r.789695.n4.nabble.com/EndEquity-lower-than-initEq-despite-positive-p-l-tp3499089p3500326.html</a><br>
>> Sent from the Rmetrics mailing list archive at Nabble.com.<br>
>><br>
>> _______________________________________________<br>
>> <a href="mailto:R-SIG-Finance@r-project.org">R-SIG-Finance@r-project.org</a> mailing list<br>
>> <a href="https://stat.ethz.ch/mailman/listinfo/r-sig-finance" target="_blank">https://stat.ethz.ch/mailman/listinfo/r-sig-finance</a><br>
>> -- Subscriber-posting only. If you want to post, subscribe first.<br>
>> -- Also note that this is not the r-help list where general R questions should go.<br>
<br>
_______________________________________________<br>
<a href="mailto:R-SIG-Finance@r-project.org">R-SIG-Finance@r-project.org</a> mailing list<br>
<a href="https://stat.ethz.ch/mailman/listinfo/r-sig-finance" target="_blank">https://stat.ethz.ch/mailman/listinfo/r-sig-finance</a><br>
-- Subscriber-posting only. If you want to post, subscribe first.<br>
-- Also note that this is not the r-help list where general R questions should go.<br>
</div></div></blockquote></div><br></div></div></div></div>