4

I am trying to enter a strategy after "X" consecutive bars.

greenCandle = barstate.isconfirmed and (close > open)
G = input(11, minval=1)

strategy.entry("buy", true, 1, when = greenCandle[G] and close[0]>open[0])

This gives me an entry 11 bars after the green but does not give me an entry after 11 consecutive greens.

hello11
  • 115
  • 4
  • 15

3 Answers3

3

The easiest way to count consecutive events is to use math.sum().

math.sum(source, length) → series float

Pass your_condition ? 1 : 0 as source so it would add 1 to the count if your condition is true and add 0 otherwise. Then compare this sum with your desired target count.

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

in_cnt_long = input.int(5, "Consecutive long event count")
in_cnt_short = input.int(3, "Consecutive short event count")

is_green_candle = (close >= open)
is_red_candle = (not is_green_candle)

green_candle_cons_cnt = math.sum(is_green_candle ? 1 : 0, in_cnt_long)  // Sum the number of candles where is_green_candle was true over the lookback period 
cons_green_cond = (green_candle_cons_cnt == in_cnt_long)                // Check if the consecutive count is equal to your target

red_candle_cons_cnt = math.sum(is_red_candle ? 1 : 0, in_cnt_short)     // Sum the number of candles where is_red_candle was true over the lookback period 
cons_red_cond = (red_candle_cons_cnt == in_cnt_short)                   // Check if the consecutive count is equal to your target

if (cons_green_cond)
    strategy.entry("Long", strategy.long)

if (cons_red_cond)
    strategy.entry("Short", strategy.short)

enter image description here

vitruvius
  • 15,740
  • 3
  • 16
  • 26
1

I'd code it that way:

//@version=5
x = input.int(11)
red = close < open                       //red bar condition
count = ta.barssince(red)                //count of bars since red

if barstate.isconfirmed and count == x   //evaluates whether barstate is confirmed and count is equal to the given x
    strategy.entry("Long", strategy.long) //Enter Long

kyu23
  • 61
  • 5
  • Thanks I tried the below but it comes up with the error: "Mismatched input 'strategy.entry' expecting 'end of line without line continuation'." Code: //@version=5 strategy("GHa3", overlay=true) x = input.int(11) red = close < open //red bar condition count = ta.barssince(red) //count of bars since red if barstate.isconfirmed and count == x //evaluates whether barstate is confirmed and count is equal to the given x strategy.entry("Long", strategy.long) //Enter Long – hello11 Jun 26 '22 at 19:55
  • This is difficult to explain for me with word, so just look at this [picture](https://prnt.sc/ZKRbxJ7jgBw5)... the problem is the number of spaces. – kyu23 Jun 26 '22 at 20:49
  • I edited the code above, so you should be able to copy it, which should work right away – kyu23 Jun 26 '22 at 20:50
  • Thanks, really appreciate your help. It now comes with the error "Could not find function or function reference 'input.int'." – hello11 Jun 26 '22 at 21:09
  • oh okay, then you are not on version 5 of pine script. just use `input()` – kyu23 Jun 27 '22 at 18:40
1

Answer provided first by vitruvius.
Personally, I simply prefer using simple functions.

//@version=5
strategy("Example", overlay = true, process_orders_on_close = true)

//#region -------------------------- INPUT

i_bullLookback  = input.int(5, 'Bullish Lookback', inline = '', group = 'Max History', minval = 1)
i_bearLookback  = input.int(3, 'Bearish Lookback', inline = '', group = 'Max History', minval = 1)


//#region ---------------------- FUNCTIONS

// @function                      when a defnied condition is met, count consecutive candles until a given number is reached and returns true as its done.
// @param condition               (series bool) provided condtion(s). Here: bullCondition/bearCondition.
// @param lookback                (simple int)  user input 
// @returns                       (simple bool) true/false
// Dependencies                   user input.
checkCondition(series bool _condition, simple int _lookback) =>
    count  = math.sum(_condition ? 1 : 0, _lookback)
    count == _lookback ? 1 : 0


//#region ------------------- DECLARATIONS

bool    bullCondition   = close >= open
bool    bearCondition   = not bullCondition

// function call
bool    longCondition   = checkCondition(bullCondition, i_bullLookback)
bool    shortCondition  = checkCondition(bearCondition, i_bearLookback)


//#region --------------------- STRATEGIES

switch 
    longCondition   => 
        strategy.entry("Long ID", strategy.long)
        // add more logic as needed

    shortCondition  => 
        strategy.entry("Short ID", strategy.short)
        // add more logic as needed