连续的红色蜡烛然后连续的绿色蜡烛

Consecutive Red Candles then Consecutive green candles

我想标记 (plotshape) 两个连续的红色蜡烛。然后是两根连续的绿色蜡烛,然后是两根连续的红色蜡烛,然后是两根连续的绿色蜡烛。请看图片,我需要这样的 pine 脚本模式。

 //@version=4
study(title="Highlight red candles", overlay=true)

twoRedCandles = (close[1] < open[1]) and (close < open)

twoGreenCandles = (close[1] > open[1]) and (close > open)

如果有连续的 green/red 个柱,则使用 var 计数器进行计数。

当条形颜色相反或达到您要查找的数字时重置此计数器。

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vitruvius

//@version=5
indicator("My script", overlay=true)

green_th = input.int(2)
red_th = input.int(3)

is_green = close > open
is_red = not is_green

var cnt_green = 0
var cnt_red = 0

cnt_green := is_green ? cnt_green + 1 : 0
cnt_red := is_red ? cnt_red + 1 : 0

is_green_th_hit = cnt_green == green_th
is_red_th_hit = cnt_red == red_th

cnt_green := is_green_th_hit ? 0 : cnt_green
cnt_red := is_red_th_hit ? 0 : cnt_red

plotshape(is_green_th_hit, "G", shape.triangleup, location.belowbar, color.green)
plotshape(is_red_th_hit, "R", shape.triangledown, location.abovebar, color.red)

编辑

你可以用另一个var看看最后一个是不是绿阵

//@version=5
indicator("My script", overlay=true)

green_th = input.int(2)
red_th = input.int(3)

is_green = close > open
is_red = not is_green

var last_one_was_green = false
var cnt_green = 0
var cnt_red = 0

cnt_green := is_green ? cnt_green + 1 : 0
cnt_red := is_red ? cnt_red + 1 : 0

is_green_th_hit = not last_one_was_green and (cnt_green == green_th)
is_red_th_hit = last_one_was_green and (cnt_red == red_th)

last_one_was_green := is_green_th_hit ? true : is_red_th_hit ? false : last_one_was_green
cnt_green := is_green_th_hit ? 0 : cnt_green
cnt_red := is_red_th_hit ? 0 : cnt_red

plotshape(is_green_th_hit, "G", shape.triangleup, location.belowbar, color.green)
plotshape(is_red_th_hit, "R", shape.triangledown, location.abovebar, color.red)