[R-SIG-Finance] Error in `[.xts`(one, trim:length(two), ) : subscript out of bounds

Joshua Ulrich jo@h@m@ulrich @ending from gm@il@com
Wed Jul 11 14:33:18 CEST 2018


On Tue, Jul 10, 2018 at 3:14 PM, Stephen Choularton
<stephen using organicfoodmarkets.com.au> wrote:
> Hi
>
> I am getting this error even though I'm pretty sure I am not out of bounds:
>
> Error in `[.xts`(one, trim:length(two), ) : subscript out of bounds
> In addition: Warning message:
> In trim:length(two) :
>   numerical expression has 305 elements: only the first used
>
> I have tried all sorts of things but clearly I am not figuring out what is
> going wrong.
>
> I am developing code to recover prices from IB and test for cointegration.
>
> There are two files attached:
>
> * checkPairFromIB
>
> * metals.csv
>
> It all worked fine until I discovered gold and silver returned data of
> different lengths:  Gold had 320 days of data and silver 305.  The calls for
> data at lines 25 and 26 are identical so I don't know why.  Can anyone shed
> light on this?
>
I see a few problems in your code.  Note that I cannot replicate your
error, but I assume 'one' and 'two' are xts objects with OHLCV data.

The calls to length() will return the total number of _elements_ in
the xts objects, not the number of rows (or observations).  You likely
want the latter.

You define 'trim' as 'one - two'.  That is, you've subtracted two xts
objects.  Therefore, 'trim' contains the price differences of 'one'
minus 'two'.  Your code seems to treat 'trim' as the difference in the
number of observations between 'one' and 'two'.

The calls to `[.xts` treat the first argument as the locations of the
rows you want returned.  You can probably see why this is an error.
Creating a sequence that starts at "an xts object of price differences
between two securities" and ends at "the number of elements in an xts
object" does not make sense.

Since you only need close prices for the call to 'ca.jo()', I suggest
you do an inner join of the close prices of the two xts objects.

closePrices <- merge(one$Close, two$Close, join = "inner")
jotest <- ca.jo(closePrices, ...)

> I then tried to trim the longer table by disposing of the earlier 15 lines
> (see lines 44 to 55 of the code).  This is where the error messages came up.
> Can anyone point out what I am doing wrong?
>
> Thanks.
>
> Stephen
>
> --
> -----------------------------------------------------------------------------------------------------------------------------------
> Stephen Choularton PhD, FIoD
> 0413 545 182
>
>
> _______________________________________________
> R-SIG-Finance using 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.
>



-- 
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com
R/Finance 2018 | www.rinfinance.com



More information about the R-SIG-Finance mailing list