Best way to have a State Condition work with Switch Variable

I have been looking at doing backtesting on stocks when the index prices are extended from their moving average, but only when the index is in a bullish condition.

It seems the script below is only isolating when the index is in its trending mode and not considering the price extension.

What’s the best way to think about how to get a state condition to work with a CrossesAbove / Below variable?

NDX = GETDATA(CODE=NDX:WI);
MA10 = MA(NDX, BARS=10, STYLE=Weighted);
MA100 = MA(NDX, BARS=100);
Trend = MA10 > MA100;

Extension = SCRIPT(SCRIPTNAME=NDX 10 Day MA Extension);
Buy = Extension CrossesBelow -2.0;
Neutral = Extension CrossesBelow 2.0;
 
V1 = SWITCH(Buy, Neutral);
V2 = SWITCH(Neutral, Buy);

V3 = If ( Trend and V1 ), 1, if( Trend and V2, 0 ) ;
V3


**

Hi Eric,

The Switch function is either on or off, so it can only have two states. To have a 3rd might be accomplished with a nested IF statement. What’s the formula for the NDX 10 Day MA Extension script?

Thanks Darren.

Variable V3 is meant to be either On or Off (within the confines of a trend condition).

The NDX Extension script is defined as:

NDX = GETDATA(CODE=NDX:WI);
C = CLOSE(NDX);
MA10 = MA(NDX, BARS=50, STYLE=Exponential);

Extension = ( C / MA10 - 1 ) * 100;
Score = ZSCORE(Extension);

Appreciated the assistance as always sir.

EK

Hi Eric,

I think the Nested IF is wrong for V3, the second item tries to be 0 but nested ifs need 3 states, 1 for each item, then 0 if neither are true.

I think this may work better:

V3 = If(Trend and V1, 1, If(Trend and V2, -1, 0));

Trend + V1 = 1

Trend + V2 = -1

If Neither is met 0