these studies will help with your first part. i didn't work on the 2nd part, involving the 50%
you don't need a fold loop to check if a sequence of a condition exists.
you can use sum()
test with a daily chart , INTC, and MRK
---------------------------
test code , upper
find all sequences of x green bars on the chart
..cyan dot below candles, sequence of x green bars
..blue dot above candles, a complete sequence happened WITHIN the past y bars
..starts counting after a sequence was found.
if the sequence = 3
and the past bars = 15,
then it will count bars, after a sequence of 3 was found, up to 12. 3+12 = 15
Code:
# greenbars_xqty_past_ybars_00_upper
#https://usethinkscript.com/threads/problem-with-using-scan-and-fold.12917/#post-109696
#post7
def na = double.nan;
def bn = barnumber();
input min_green_seq_qty = 3;
input lookback_bars = 15;
def green = (close > open);
# check if x bars in a row are green , 3
def grnseq = (sum(green, min_green_seq_qty) == min_green_seq_qty);
# check if a seq happened within the lookback_bars. completely, not partially
def barz = lookback_bars - min_green_seq_qty + 1;
def past_grn_bars = (sum(grnseq, barz) > 0);
def vert = 0.1;
input test1_seq = yes;
plot z1 = if test1_seq and grnseq then low*(1-vert) else na;
z1.SetPaintingStrategy(PaintingStrategy.points);
z1.SetDefaultColor(Color.cyan);
z1.setlineweight(2);
input test2_pastxbars = yes;
plot z2 = if test2_pastxbars and past_grn_bars then high*(1+vert) else na;
z2.SetPaintingStrategy(PaintingStrategy.points);
z2.SetDefaultColor(Color.blue);
z2.setlineweight(2);
# count bars after a sequence
def grncnt = if bn == 1 then 0
else if grnseq[1] and !grnseq[0] then 1
else if !grnseq and past_grn_bars > 0 then grncnt[1] + 1
else 0;
# show the count of bars after a sequence
input test3_cnt = yes;
plot z3 = if test3_cnt and grncnt > 0 then grncnt else na;
z3.SetPaintingStrategy(PaintingStrategy.values_above);
z3.SetDefaultColor(Color.white);
#
=================================
lower study
this should be able to be used as a scan (not tested)
add code to only check for a sequence, within the last x bars, only from the last bar
Code:
# greenbars_xqty_past_ybars_00_lower
#https://usethinkscript.com/threads/problem-with-using-scan-and-fold.12917/#post-109696
# post7
declare lower;
def na = double.nan;
def bn = barnumber();
input min_green_seq_qty = 3;
input lookback_bars = 15;
# makes it complex
#def lastbn = HighestAll(If(IsNaN(close), 0, bn));
# this should work to find last bar
def lastbar = (!isnan(close) and isnan(close[-1]));
def lastbn = if lastbar then bn else 0;
def green = (close > open);
# check if x bars in a row are green , 3
def grnseq = (sum(green, min_green_seq_qty) == min_green_seq_qty);
# check if a seq happened within the lookback_bars. completely, not partially
def barz = lookback_bars - min_green_seq_qty + 1;
plot z1 = if (bn == lastbn and sum(grnseq, barz) > 0) then 1 else 0;
#
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Sum