Pinescript: Count of two conditions and reset

Viewed 269

i have a problem with a code in pinescript. I have no idea how to code this unfortunately.

I have defined a lookback period (32 bars) and want to have a count over that period when two conditions are met.

Below is the code that counts wihtin the lookback period if the first condition met: is_condition_1

cnt = 0
is_condition_1 = false
is_condition_2 = false
if timeframe.isdaily
    is_DD_down_bar = change(close) < close[1] * -0.002 ? true : false
    is_DD_volume_up = change(volumeData) > 0 ? true : false
    is_condition_1 := is_DD_down_bar and is_DD_volume_up ? true : false
    is_condition_2 := crossover(close,ema(close,21))? true : false

    for i = 0 to lookbackPeriod by 1
        if is_condition_1[i]
            cnt := cnt + 1
            cnt

the count from this code is 3 (cnt=3). So far so good.

Now I want to add that once the second condition: is_condition_2 is met, the counter resets to zero and counts from that point onwards if condition one is_condition_1 is met.

Here is a screenshot to visualize it (The "D" is plotted once is_condition_1 is true) :

example picture

The count from the start of the lookbackperiod for is_condition_1 would be =2 until is_condition_2(price crossing over the blue line) is true. This should reset the counter to 0. From there on, the count should start again for is_condition_1. The final result here would be 1.

Does anyone of you guys have an idea how to add this loop to the code? Thanks a lot for having a look.

0 Answers
Related