Creating a date range/region with script

I'd like to able to define a continuous period of time (or a date region) using script only. The start and end of the date region are each based on their own specific conditions. All bars in between the start and end evaluate to true. Such a concept could be used to define a bear market for example. Skeleton pseudo code as follows:

MovAvg = MA(BARS=9, CALC=Close);

StartCondition = CLOSE(0) < MovAvg[0] and CLOSE(1) < MovAvg[1];

EndCondition = CLOSE(0) > MovAvg[0] and CLOSE(1) > MovAvg[1];

BearMarketStart = StartCondition;

BearMarketEnd = SIGNALAFTER(BearMarketStart, EndCondition);

isBarInBearMarket = IF(Bar is between BearMarketStart and BearMarketEnd, TRUE, FALSE);

Has anyone done something similar? The above correctly defines the BearMarketStart and BearMarketEnd variables, but fails in that the isBarInBearMarket variable only evaluates to true for one bar after BearMarketStart even though there might be a dozen or more bars between BearMarketStart and BearMarketEnd.

Many thanks in advance for any ideas or assistance.

Hi Dean,

Can you please provide a screen shot example of what it is you are wanting the criteria to find / highlight for reference?

Sure thing, thanks Matthew.

Optuma-Screenshot.pdf (112 KB)

Hi Dean,

Based on your screen shot the following script should provide the result you are after:

 

V1 = CLOSE() < MA(BARS=50, CALC=Close) ; 
V1a = ACC(V1, RANGE=Look Back Period, BARS=2) == 2; 
V2 = CLOSE() > MA(BARS=50, CALC=Close) ;
V2a = ACC(V2, RANGE=Look Back Period, BARS=2)  == 2;
OFFSET(SWITCH(V1a,V2a), OFFSET=1)

 

The switch function will swap between 1 (True) and 0 (False) when the last 2 closes cross below a MA or above the MA.

The shaded green zone shows where the script returns a true result…

Ex4

Wow, Matthew thank you so much. That was a nice concise solution you provided. Very smart indeed. The key function that was the 'aha! moment' for me was the SWITCH function. It is so useful. Thanks again, really appreciate your help.