Skip to content

EndEquity lower than initEq despite positive p/l

6 messages · G See, Brian G. Peterson, S

S
#
On 5/6/11 8:31 AM, Brian G. Peterson wrote:
Thank you Brian for taking a look at this. I appreciate your time.

This problem feeds into other functionalities, which you probably
already know but I thought I would provide more details just in case.

For example:

charts.PerformanceSummary(ROC(getAccount(account.st)$summary$End.Eq)[-1])

What happens is that the call to log(x), where x is the daily equity
value produces -Inf for Log(0) and NaN for log(-negative values). As a
result the log(x) values you see below are fed to chart.TimeSeries where
it fails.

exiting from: checkData(R)
Error in plot.window(xlim, ylim, xaxs = "r", log = logaxis) :
  need finite 'ylim' values

debug: {
    if (type == "discrete") {
        roc <- x/lag(x, n, na.pad = na.pad) - 1
    }
    if (type == "continuous") {
        roc <- diff(log(x), n, na.pad = na.pad)
    }
    reclass(roc, x)
}

----snippet of log(end equity) below ---

2008-09-30     -Inf
2008-10-01     -Inf
2008-10-02     -Inf
2008-10-03     -Inf
2008-10-06     -Inf
2008-10-07     -Inf
2008-10-08     -Inf
2008-10-09     -Inf
2008-10-10 8.191097
2008-10-13      NaN
2008-10-14      NaN
2008-10-15      NaN
2008-10-16      NaN
2008-10-17      NaN
2008-10-20      NaN
2008-10-21      NaN
2008-10-22      NaN

----snippet of end equity below ----

2008-09-30     0.00
2008-10-01     0.00
2008-10-02     0.00
2008-10-03     0.00
2008-10-06     0.00
2008-10-07     0.00
2008-10-08     0.00
2008-10-09     0.00
2008-10-10  3608.68
2008-10-13 -3053.23
2008-10-14 -2154.78
2008-10-15 -2518.47
2008-10-16  -705.72
2008-10-17  -671.87
2008-10-20   -20.29
2008-10-21 -1837.16
2008-10-22 -4495.50

This leads me to think about the situation where initial equity is set
correctly but draw downs have wiped out the equity and are again
producing negative end equity values. In real life this wouldnt be an
issue as your trading would end if that were to happen. But for purposes
of backtesting I could set an initial equity value that could be easily
wiped out by draw downs.  Not sure if you want to address that or leave
it up to the user to set initial equity values that are appropriate for
the size of their trading.
1 day later
#
In your code I changed

updateAcct(name=account.st, Dates='2007-07-01::2010-07-03') #old

to start on the startDate instead of initDate:

updateAcct(name=account.st, Dates='2007-07-02::2010-07-03') #new

The updateEndEq function is finding the location of the first row and then
subtracting 1 which returns

integer(0)

Then when it calls getEndEq with that value, 0 is returned.

So, in the updateEndEq function I changed

if (is.null(Dates))
        Dates = time(Account$summary)   #old

to:

if (is.null(Dates))
        Dates = time(Account$summary)[-1]  #new

Now your code works.  See attached

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.

Garrett
On Fri, May 6, 2011 at 2:24 PM, s <algotr8der at gmail.com> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110507/3c403839/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: algotr8der_bugfix.r
Type: application/octet-stream
Size: 7293 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110507/3c403839/attachment.obj>
#
Garrett,

Thank you for your patch.  I have applied it as svn r601 on R-Forge.  It
is safe enough as far as it goes, though there is more work to do to
really eliminate the issues.

You correctly figured out that a Dates range on the startDate rather
than the initDate does not cause the problem.  Your patch fixes the
issue if Dates is NULL (the default).  I'll need to do a little more
work to detect if the Dates range that is passed in includes the first
index, and remove it.  But this is a good start.

Thanks again,

   - Brian
On Sat, 2011-05-07 at 20:16 -0500, G See wrote:

  
    
3 days later
S
#
I was wondering if this fix has been included in the latest download for
blotter available on r-forge:

http://r-forge.r-project.org/src/contrib/blotter_0.8.tar.gz

It says the last change was: 2011-05-08 05:02:59+02 | Rev.: 601

I downloaded this package as well as the latest quantstrat package (v603)
and installed them manually but ran into the same problem.

Appreciate the guidance.



--
View this message in context: http://r.789695.n4.nabble.com/EndEquity-lower-than-initEq-despite-positive-p-l-tp3499089p3516083.html
Sent from the Rmetrics mailing list archive at Nabble.com.
#
yes, the fix made it in.  Change your strategy to not specify the Dates
grouping at all.  Let the function take care of itself.  Or take a look
at the 'pairs_trade' demo in quantstrat contributed by Garrett See for a
more sophisticated treatment.

Regards,

   - Brian
On Wed, 2011-05-11 at 15:56 -0700, algotr8der wrote:

  
    
#
Slightly newer version of my demo attached
On Wed, May 11, 2011 at 6:10 PM, Brian G. Peterson <brian at braverock.com>wrote:

            
Garrett
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110511/951e663d/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: quantstrat_pair_trade_6b.r
Type: text/x-r
Size: 11223 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110511/951e663d/attachment.bin>