[R-SIG-Finance] EndEquity lower than initEq despite positive p/l

Brian G. Peterson brian at braverock.com
Sun May 8 03:25:50 CEST 2011


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:
> 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:
>         On 5/6/11 8:31 AM, Brian G. Peterson wrote:
>         > Looks like a bug in initAcct.  My suspicion is that if the
>         first row of
>         > the Acct table had the initiEq, all else would work fine.
>         >
>         > We changed some things around there a couple months ago to
>         deal with
>         > multi-currency accounts (accounts with a different
>         measurement currency
>         > than portfolios) and with vectorizing as many of the
>         calculations as
>         > possible.  I'll look at the older code and see what changed.
>         >
>         > Thanks for the report.  As an institutional investor, I
>         basically never
>         > think about account equity.
>         >
>         > I'll try to get back to you later today once I've fixed it.
>         >
>         > Cheers,
>         >
>         >   - Brian
>         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.
>         
>         > On Thu, 2011-05-05 at 17:51 -0700, algotr8der wrote:
>         >> Okay - here is a bogus strategy just to test this -
>         >>
>         >> http://r.789695.n4.nabble.com/file/n3500326/bogusStrategy.R
>         bogusStrategy.R
>         >>
>         >> a) portfolio_1 = IBM, UTX, CAT
>         >>
>         >> b) portfolio_2 = MSFT
>         >>
>         >> Indicators = 10 DSMA, 15 DSMA, 20 DSMA
>         >>
>         >> Rules
>         >>
>         >> a) if 10_DSMA Crosses Above 20_DSMA BUY stocks in
>         portfolio_1 AND COVER
>         >> existing SHORTS in portfolio_1
>         >> b) if 10_DSMA Crosses Below 20_DSMA SELL existing long
>         positions in
>         >> portfolio_1 AND SHORT all stocks in portfolio_1
>         >>
>         >> c) if 15_DSMA Crosses Above 20_DSMA BUY stocks in
>         portfolio_2 AND COVER
>         >> existing SHORTS in portfolio_2
>         >> d) if 15_DSMA Crosses Below 20_DSMA SELL existing long
>         positions in
>         >> portfolio_2 AND SHORT all stocks in portfolio_2
>         >>
>         >> Once you have updated the portfolios and accounts type the
>         following:
>         >>
>         >>> getAccounts(account.st)
>         >> Look at the 'End.Eq' column (this is the last column). You
>         will see that the
>         >> portfolio begins with 0 as the initial value and each day's
>         p/l is added to
>         >> 0 until the last day.
>         >>
>         >> 2010-07-02         -989.49  85836.10
>         >>
>         >> Maybe I am misunderstanding something but it appears to me
>         that the p/l of
>         >> this strategy = 85836.10 and so that amount should be added
>         to the initEq.
>         >>
>         >> In my real strategy I know the p/l is ~34800, which should
>         be added to the
>         >> initEq.
>         >>
>         >>
>         >>
>         >>
>         >>
>         >>
>         >> --
>         >> View this message in context:
>         http://r.789695.n4.nabble.com/EndEquity-lower-than-initEq-despite-positive-p-l-tp3499089p3500326.html
>         >> Sent from the Rmetrics mailing list archive at Nabble.com.
>         >>
>         >> _______________________________________________
>         >> R-SIG-Finance at r-project.org mailing list
>         >> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>         >> -- Subscriber-posting only. If you want to post, subscribe
>         first.
>         >> -- Also note that this is not the r-help list where general
>         R questions should go.
>         
>         _______________________________________________
>         R-SIG-Finance at r-project.org mailing list
>         https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>         -- Subscriber-posting only. If you want to post, subscribe
>         first.
>         -- Also note that this is not the r-help list where general R
>         questions should go.
>         
> 
> 

-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list