简体   繁体   中英

How to check for a sequence of similar candle types in PineScript

I am trying to check if the past n candles are of the same type.

For example, are all five previous candles bullish, or are they all bearish.

With my approach, none of any consecutive candles fit the condition

How can I go about checking for a candle sequence where all previous n candles are all blue or green?

Thank you all in advance.

I am also willing to try out any other working approach/ ideas.

isBullish = true
isBearish = true

for i = 1 to iterationCount
    notSeries = not(isBullish and isBearish)

    if notSeries
        break

    if close[i] > open[i] and isBullish
        isBullish := true
        isBearish := false
    else 
        isBearish := true
        isBullish := false

You could assign a numerical value to a variable to define your bar states and sum them to determine if n consecutive candles have the same state.

bullCandle = close > open ? 1 : 0
int isBullishCount = 0
for i = 1 to iterationCount
    isBullishCount += bullCandle[i]

isBullish = isBullishCount == iterationCount
//@version=4
study("Candles", overlay=true)

i_candles = input(5, "Consecutive candles", input.integer)

candle_direction    = close >= open ? 1 : -1
sum_direction       = sum(candle_direction, i_candles)

all_up              = sum_direction ==  i_candles
all_down            = sum_direction == -i_candles

bgcolor(all_up   ? color.green : na)
bgcolor(all_down ? color.red   : na)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM