[R-SIG-Finance] Duplicated indexes in blotter

Joshua Ulrich josh.m.ulrich at gmail.com
Wed Sep 24 13:58:38 CEST 2014


Hi Mark,

On Fri, Sep 19, 2014 at 5:13 PM, Mark Knecht <markknecht at gmail.com> wrote:
> In the attached code I have 3 transactions on the same date for the
> symbol IVOG. When I go to roll up position values this is leading to
> duplicate indexes in my xts objects and NA's in other columns, such as
> in tempPos.Value:
>
> 2014-05-05  55182 45420.0
> 2014-05-06  54702 45135.0
> 2014-05-07  63945 68212.5
> 2014-05-07  73080      NA
> 2014-05-07  82215      NA
> 2014-05-08  81747 68025.0
> 2014-05-09  81891 68040.0
> 2014-05-12  83448 68985.0
>
> Am I doing something improper in the way I'm entering the transactions
> or is this a bug? It's pretty typical that I'll put in a limit order
> and it will get filled in pieces with each portion having a different
> price.
>
The only thing you might consider improper is that you're using the
same timestamp for each fill, even though they obviously don't have
the same timestamp.

> I could of course figure out by hand what the average price was and
> then have a single transaction but I'd like to have what's saved in
> the account match what actually happened.
>
That argues for keeping the time of the transactions.  You're not
required to mark the book on the same frequency as the transactions.
For example, you can run a backtest on 1-second data, but mark the
book on 1-minute data.

> In general I've been fixing these as they pop up using code like:
>
> tempNet.Txn.Value[is.na(tempNet.Txn.Value)] = 0
>
An alternative would be to specify fill=0 in your call to merge.xts
(called by cbind):
tempPos.Value <- merge(tempPos.Value, tempVal$posPL$Pos.Value, fill=0)

Though I think fill=na.locf makes more sense for Pos.Value...

> but I'm finally tired enough of doing that I thought I'd ask if I'm
> doing something wrong.
>
It's not clear to me what you expect the behavior to be.  You enter
multiple transactions on the same timestamp, and blotter respects
that; it even keeps them in the same order.

If you want tempPos.Value to only have the last observation for each
day, you could use something like:
tpVal <- apply.daily(tempPos.Value, last)

> Additionally, I thought one of the things xts was supposed to ensure
> against was duplicate indexes so maybe this is me using some older
> version? I'm mostly CRAN oriented so if I need to load something else
> please let me know.
>
Neither zoo nor xts (on CRAN or R-Forge) prevent duplicate index values.

> Thanks,
> Mark
>
>
<snip code>

Best,
--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com



More information about the R-SIG-Finance mailing list