[R-SIG-Finance] quantstrat and nonstandard column names in the price series

Brian G. Peterson brian at braverock.com
Wed Feb 9 21:33:47 CET 2011


On 02/09/2011 01:08 PM, Vladimir Egorin wrote:
> Here is a hopefully small standalone example (a hacked script from one of the
> demos.).
> Running it produces an error:
> [1] "setup completed"
> Error in .xts(e, .index(e1), .indexCLASS = indexClass(e1), .indexFORMAT =
> indexFormat(e1),  :
>
>    index length must match number of observations

There were a few problems with your example.  A corrected version is 
attached.

The first and most serious was that you were doing this in the add.rule 
commands:

data=quote(getPrice(mktdata, prefer='FV'))

This basically took your signal columns *out* of the data object, 
returning only the price column.  That would never work, as there is no 
column for ruleSignal to match on.

The prefer='FV' argument belongs more reasonably in ... in 
applyStrategy.  I also placed it in the arguments= list for ruleSignal 
in add.rule, for clarity, but it shouldn't be strictly necessary there.

A second, and more minor error, is in the construction of your index.

You used POSIXct for daily data, which creates problems even after 
fixing the data object in the calls to ruleSignal.  I modified your 
construction to use as.Date for the index, so that the periodicity 
checks and handling will work properly inside quantstrat.

With these changes, your example works, and generates trades.

 > out<-try(applyStrategy(strategy=stratRSI , portfolios=port.st, 
parameters=list(n=2), prefer='FV' ) )
[1] "2011-01-07 FV -1000 @ 117.9922"
[1] "2011-01-13 FV 1000 @ 118.2734"
[1] "2011-01-13 FV -1000 @ 118.2734"
[1] "2011-01-19 FV 1000 @ 118.2344"
[1] "2011-01-19 FV -1000 @ 118.2344"
[1] "2011-01-20 FV 1000 @ 117.6562"
[1] "2011-01-20 FV 1000 @ 117.6562"
[1] "2011-01-25 FV -1000 @ 118.2422"
[1] "2011-01-25 FV -1000 @ 118.2422"
[1] "2011-01-27 FV 1000 @ 118.2891"
[1] "2011-01-27 FV -1000 @ 118.2891"
[1] "2011-02-01 FV 1000 @ 118.0703"
[1] "2011-02-01 FV 1000 @ 118.0703"

examine the 'out' variable to see what gets added at each step of 
indicators and signals, and the order book to see the orders generated 
from the signals.

> In general, how would you specify the name of the underlying when
> adding rules, in case, for example, the signal is on the underlying X
> and the order is on underlying Y.

In strategies like this, you'd probably be writing custom indicator 
and/or signal functions that took parameters such as Prod1, Prod2 and 
knew the structure of your data, such as columns named for Prod1 and 
Prod2.

The standard functions are there to provide for common, simple, 
strategies, but for proprietary indicators you'll still need proprietary 
functions. ;)

Regards,

   - Brian

-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: vladimir.R
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110209/e2a925d0/attachment.pl>


More information about the R-SIG-Finance mailing list