[R-SIG-Finance] A quick custom data question
Brian G. Peterson
brian at braverock.com
Sat Apr 8 12:13:04 CEST 2017
On 04/08/2017 03:58 AM, Michael Chen wrote:
> Hi,
>
>
> I didn't get a satisfactory answer after searching the archives. I just
> like to know how do we access market data other than OHLC? for example:
>
>
>
> head(J.DCE) Open High Low Close Volume hmmState
> 2010-01-04 2886.193 2886.193 2811.193 2833.193 0 1
> 2010-01-05 2886.193 2886.193 2811.193 2833.193 0 1
> 2010-01-06 2886.193 2886.193 2811.193 2833.193 0 1
> 2010-01-07 2886.193 2886.193 2811.193 2833.193 0 1
> 2010-01-08 2886.193 2886.193 2811.193 2833.193 0 1
> 2010-01-11 2886.193 2886.193 2811.193 2833.193 0 1
>
>
>
> In my quantstrat set up, I simply set up this as an indicator:
>
>
> add.indicator(strategy.st, name = "getHMM",
> arguments = list(x = quote(getPrice( mktdata, prefer =
> "hmmState"))),
> label = "HMMState")
>
>
> getHMM is a simple function I wrote to pass on the data as an indicator,
> anyhow the error occurs at the getting data part:
>
>
>> test <- applyIndicators(strategy.st, OHLC(J.DCE))
> Show Traceback
> Rerun with Debug
> Error in getPrice(mktdata, prefer = "hmmState") : subscript out of
> bounds, no price was discernible from the data
>
>
> The data was converted to an xts for quantstrat. I am probably making a
> simple error somewhere, would someone please point it out to me,
> thanks. Also, is there a better way to or a more correct way to access
> custom data easily for quantstrat indicators/signals??
The list doesn't terribly like HTML posts.
Anyway, it seems likely that your problem is here:
OHLC(J.DCE)
when you call applyIndicators.
You're only passing in the OHLC columns.
just test applyIndicators with
applyIndicators(strategy.st, mktdata=J.DCE)
and getPrice should be able to find your column in your getHMM function.
Even simpler, since the data is already there, and being passed into to
the apply* functions with the original data is to simply refer to the
column in whatever other indicator needs it, or in your signal
functions. I don't see a need for a custom indicator function at all.
In any case, from your non-reproducible example, and just reading the
code, the main problem appears to be your call to OHLC(), which will
strip your other columns. Skip that, and things should be fine.
Regards,
Brian
--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock
More information about the R-SIG-Finance
mailing list