# [R-SIG-Finance] Preventing active-neutral-active bouncing signals by staying neutral

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jul 8 21:05:12 CEST 2009

```The rules are not clear but maybe this code will help in
constructing whatever it is. newstate takes the current state
and action and produces the new state from those two such
that the state is the signed number of signals in the current
direction (since the last signal in the other direction).

newstate <- function(state, action) {
if (action == 0) state
else if (action * state > 0) state <- state+action
else action
}
Reduce(newstate, coredata(x), 0, acc = TRUE)

On Wed, Jul 8, 2009 at 6:38 AM, <Murali.MENON at fortisinvestments.com> wrote:
> Folks,
>
> I have a series of active (+1, -1) and neutral (0) signals, and I want
> to prevent bouncing from active to neutral to active (same sign). E.g.,
> if I am -1 yesterday and neutral today, I will neutralise, but if the
> signal becomes -1 again tomorrow, I want to remain neutral; if it does
> remain -1 the following day as well, I will accept that and go short.
>
> So if I have:
>
>> x <- zoo(matrix(c(1,0,1,1,0,-1,0,1,-1,0,1,1),ncol=2), as.Date(1:6))
>> x
> 1970-01-02  1  0
> 1970-01-03  0  1
> 1970-01-04  1 -1
> 1970-01-05  1  0
> 1970-01-06  0  1
> 1970-01-07 -1  1
>
> in the first column, I'll neutralise my position on 03-Jan-70, ignore
> the new active signal on 04-Jan-70, and go long again until 05-Jan-1970;
> neutral again on 06-Jan-70, but will go short on 07-Jan-70, because the
> signal is opposite to what it was before I neutralised.
>
> Likewise, in column 2, I'll go neutral on 05-Jan-1970 but accept the
> long signal the very next day because it is opposite to my previous
> position.
>
> How best to convert this zoo into a new zoo with ZEROES in place of
> active signals where I have determined I want to continue to remain
> neutral?
>
> In general, I may want to remain neutral for 'n' consecutive days, and
> then only accept a signal in the same direction as I started with after
> I get 'm' successive identical active signals; but I will accept a
> signal in the opposite direction at once if it appears after the 'n'
> neutrals.
>
> Is there a way to code this generalisation?
>
> (All I could think of was rather roundabout: determine the dates of
> inception of each signal (separately for +1s and -1s), and if the
> difference between these dates is n-1, then neutralise the signal on the
> latter date. But this doesn't take care of the 'm' successive active
> signals before activating.)
>
> Cheers,
> Murali
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>

```