# Measure bars between MA turn signals

Hi,

I’m having trouble lining up the calculations below.I’m trying to count the number of bars between turns in the MA over the past three IsUp and IsDown runs. I want to ensure the past few runs have been of a reasonable length for a watchlist.

When I use a Show View, the value of the calculation for each of previous runs appears on a different date, so they can never be all true on the same bar. I’m sure it’s something simple, but I just can’t get it.

Thanks for any help

MinUpDays = 1 ;
MinDownDays = 1 ;

// Define Trailing Moving Average
BuyMA = MA(DAY(), STYLE=Weighted, CALC=HLC, BARS=20) ;
ShortMA = MA(DAY(), STYLE=Weighted, CALC=Close, BARS=5) ;

// Sustained rise after turn signal
MoveUp2 = ShortMA IsUp ;
BuyTrend = MoveUp1 and MoveUp2 ;

// Find the turns
Turn1 = MA Turns ; // This must be a TurnUp as previous test is BuyMA IsUp
PrevTurn0 = TIMESINCESIGNAL(Turn1) ; // Turn Up
PrevTurn1 = TIMESINCESIGNAL(Turn1[1]) ; // Turn Down
PrevTurn2 = TIMESINCESIGNAL(Turn1[2]) ; // Turn Up
PrevTurn3 = TIMESINCESIGNAL(Turn1[3]) ; // Turn Down
PrevTurn4 = TIMESINCESIGNAL(Turn1[4]) ; // Turn Up
PrevTurn5 = TIMESINCESIGNAL(Turn1[5]) ; // Turn Down
PrevTurn6 = TIMESINCESIGNAL(Turn1[6]) ; // Turn Up
// Measure length of trends
LastDown1 = PrevTurn1 - PrevTurn0 ; // Days in last down trend
LastDown2 = PrevTurn3 - PrevTurn2 ; // Days in down trend [1]
LastDown3 = PrevTurn5 - PrevTurn4 ; // Days in down trend [2]
LastUp1 = PrevTurn2 - PrevTurn1 ; // Days in last up trend
LastUp2 = PrevTurn4 - PrevTurn3 ; // Days in last up trend [1]
LastUp3 = PrevTurn6 - PrevTurn5 ; // Days in last up trend [2]
// Ensure trends are long enough
TrendDownLen1 = LastDown1 > MinDownDays ;
TrendDownLen2 = LastDown2 > MinDownDays ;
TrendDownLen3 = LastDown3 > MinDownDays ;
TrendDownLen = TrendDownLen1 and TrendDownLen2 and TrendDownLen3 ;

TrendUpLen1 = LastUp1 > MinUpDays ;
TrendUpLen2 = LastUp2 > MinUpDays ;
TrendUpLen3 = LastUp3 > MinUpDays ;
TrendUpLen = TrendUpLen1 and TrendUpLen2 and TrendUpLen3 ;

TrendLength = TrendDownLen and TrendUpLen ;

TrendLength

Hi Kim,

The first thing I notice in your script, which while not showing as an error, is I believe is one issue. That is the line:

Turn1 = MA Turns ; // This must be a TurnUp as previous test is BuyMA IsUp

"MA" is not valid variable or function. MA is a function that requires parameters as you have provided for your "BuyMA" and "ShortMA"

If you try making “Turn1” the output of your script by changing the last linse of your script to

//TrendLength

Turn1

You'll find the Script Manager gives you the dreaded "Please enter a valid script..." message.

Decide what you want in place of “MA” and see how you get on.

Cheers

Trevor

The Auld Tyma at

Well spotted!!! But sadly, doesn’t fix the problem. I changed it to “Turn1 = BuyMA Turns”, which is what it was meant to be, but it still isn’t giving me the result I expect.

I’ve looked through the rest of the script for similar “obvious” errors and can’t see any - which doesn’t mean they aren’t there!

All ideas gratefully accepted!!

Hi Kim,

I’ve only had a quick look at this but are you sure you want all the 3 trend lengths to be true at the same time?

If you output all the lengths using this at the end of the script

```plot1 = TrendDownLen1;
plot2 = TrendDownLen2;
plot3 = TrendDownLen3;
plot2.Colour = Red;
plot3.Colour = Blue;
```

Then you will get the following and I do not see a time where the three overlap (see below). so the line
TrendDownLen = TrendDownLen1 and TrendDownLen2 and TrendDownLen3 ;
will never be true.

The trick when a script does not work is to output each variable and double check that it is giving you what you expected.

Hope that helps

Mathew

Hi Mathew,

Thanks. I had done as you suggested and that was the essence of my problem, that I couldn’t get them to line up.

What I’m looking to find are stocks where the number of bars between each of the last three runs between “TurnsUp” and “TurnsDown” is greater than ‘x’. As you can see in the attachment, I’m not good at Paint but hopefully it gives you an idea of what I’m looking for in the script.

Cheers

Kim

Hi Kim,

I’ve had another quick look and there is nothing obvious which jumps out at me. Unless someone else can spot something, the only thing I can suggest is to get one of the guys to do a scripting consult. More information at www.optuma.com/consults

All the best

Mathew

Hi Mathew,

It’s good to know there’s nothing obvious!

Hopefully someone else can spot the error as it seems like a fairly basic thing to want to do. If not, then the consult sounds like a good idea.

Thanks for looking

Cheers

Kim