[R-SIG-Finance] Customized indicator for quantstrat
Brian G. Peterson
brian at braverock.com
Sat Dec 15 19:31:48 CET 2012
On 12/15/2012 12:30 PM, Robert A'gata wrote:
> Thank you Soren and Brian. Explicitly renaming output column name of
> devCalc to "dev" seems to work. Now I can run applyStrategy until end of
> data. The problem now is that, after looking at transactions, it keeps
> buying whenever dev > 0.2. I would like to only buy 1 when there is no
> position. Likewise, when dev < -0.2, I would like to sell one only when
> there is no position. Do you know how to fix this? Thank you again.
See examples that make use of osMaxPos, e.g. bbands, luxor, rsi.
> On Sat, Dec 15, 2012 at 6:40 AM, Brian G. Peterson <brian at braverock.com>wrote:
>> On 12/15/2012 04:26 AM, me wrote:
>>> On Fri, 14 Dec 2012 23:56:08 -0500
>>> "Robert A'gata" <rhelpacc at gmail.com> wrote:
>>>> I am trying to learn how to create customized indicator. Mostly I
>>>> have my own recipe which TTR does not cover. I only know that the
>>>> indicator function is supposed to return xts. But I couldn't get it
>>>> to work on my example. I attached the file here. The customized
>>>> indicator function is called "devCalc" which calculates deviation
>>>> between current price and a given VWAP. Then normalized with
>>>> volatility. I named the indicator as "dev" in add.indicator's label.
>>>> R complains that no "dev" found when I try to use it to define my
>>>> signal. Any help would be appreciated. Thank you.
>>> I think I have encountered this problem before. For some reason, you
>>> have to make sure that the 'devCalc' function that calculates your
>>> indicator returns an xts object *with the correct column names*. In your
>>> case, I think you should do something like this.
>>> colnames(res) <- 'dev'
>>> For some reason, the label="dev" argument in the declaration below is
>>> not enough.
>>> stratVwap <- add.indicator(strategy=**stratVwap,name="devCalc",
>> applyIndicators will append the label in all cases, I believe.
>> It seemed from Robert's original post and reported error that he probably
>> used name='dev' rather than name='devCalc', since R reported that it could
>> not find a function *named* 'dev'.
More information about the R-SIG-Finance