RihoKuraki
RihoKuraki

Reputation: 7

Start counting bars number when the condition is met and add the exceptions to the count as well

When the conditions are met, I want to count the bars number, but also add exceptions. For example, I want to count 11 days in a row to see if there is 9 days up during this period, and if one day(let's say today) is down but the close price are less or equal to 1% than yesterday (means yesterday is up day, not previous up day), then count this day is a up day too.

For example, let's say day1 is up day, here is 11days in a row, and there is 3 days down (day3, day5, day6), but day3's close price are less or equal to 1% than yesterday (means day2 and day2 is up day), and day2 is up day, so day3 is a up day too, which mean is it will met the condition this time.(9 days up in 11days)

day1  day2  day3   day4  day5   day6   day7   day8  day9  day10  day11
10    11    10.5   10.8    9     8      12     17    18     19    20
up    up    down    up    down  down    up     up    up     up    up

And in a case like this, the condition is not valid, day3' close price are less or equal to 1% than yesterday(day2), but yesterday(day2) are not up day, which mean the condition are not met this time.(8 days up in 11days this time)

day1  day2  day3   day4  day5   day6  day7  day8  day9  day10  day11
10     9     8.5   10.8    8     9     12    17    18    19     20
up    down   down   up    down   up    up    up    up    up     up

This is my code:

//@version=5

indicator("Green bars", overlay = true, max_labels_count = 500)

barsBack = input.int(11)

var float[] green_bar_array = array.new_float()

if close > close[1] or ((close/close[1])-1) * 100 <= 1
    array.push(green_bar_array, close)

if array.size(green_bar_array) > barsBack
    array.shift(green_bar_array)

bool updaygreen = false
int green_count = 0

if array.size(green_bar_array) == barsBack
    for i = 1 to barsBack - 1
        if array.get(green_bar_array, i) >= array.get(green_bar_array, i - 1)
            green_count += 1
    if green_count >= 9
        updaygreen := true

plotshape(updaygreen, location=location.belowbar, color=#00b543, style=shape.circle, size=size.tiny, transp=55)

But the result are not correct... what am I missing? Please help me, thanks!

EDIT:

//@version=5
indicator("Count Bar Color", overlay = true, max_labels_count = 500)

var int     barsBack        = input.int(11, 'Number of bars lookback', 0)

var int[]   greenBars       = array.new_int(barsBack, 0)
var int[]   redBars         = array.new_int(barsBack, 0)
var int     greenBarCount   = 0
var int     redBarCount     = 0

direction = ta.change(close) // red or green ?

if barstate.isconfirmed
    if direction > 0
        array.push(greenBars, 1) 
        array.push(redBars,   0)
    else if direction < 0 
        array.push(greenBars, 0) 
        array.push(redBars,   1)

    if direction != 0 
        array.shift(greenBars) 
        array.shift(redBars)

    greenBarCount := array.sum(greenBars)
    redBarCount   := array.sum(redBars)

upday = greenBarCount >= 9

plotshape(upday, location=location.belowbar, color=#00b543, style=shape.circle, size=size.tiny, transp=55)

Changed my code to this now, but still don't know how to add "close price are less or equal to 1% than yesterday(yesterday is up day, not previous up day)" into it...

Upvotes: 0

Views: 1168

Answers (1)

tentme
tentme

Reputation: 156

This is how I would do it. Please try it and leave feedback.

//@version=5
indicator("Count Bar Color")
len = input(11)
thres = input(9)
pct = input(1.0)
up = close >= close[1]
dn = close < close[1]
valid = up or (up[1] and dn and ta.roc(close, 1)>-pct)
count = math.sum(valid?1:0, len)
plot(count, color=count>=thres?color.yellow:color.blue, style=plot.style_columns)
hline(thres)

Upvotes: 0

Related Questions