My pine script code has the following problem
working version 1:
buy_signal = condition1
working version 2:
buy_signal = condition1
working version 3:
buy_signal = condition1 or condition2
but with the following it doesn't generate any trades...
The only one not working is:
buy_signal = condition1 and condition2
This must be due to the different release dates of the information, which means that they are not true at the same time.
That's why I need a function/ Workaround to check if a condition is true in a certain time frame
You can use ta.barssince()
to see when your condition was true
. Then use that as a condition.
ta.barssince()
Counts the number of bars since the last time the condition was true.
ta.barssince(condition) → series int
Something like this:
barssince_cond1 = ta.barssince(condition1)
barssince_cond2 = ta.barssince(condition2)
buy_signal = false
buy_signal := if (condition1 and condition2) // If both conditions are true on the same bar, then buy
true
else if (condition1) // If only the condition1 is true, check when was the last time condition2 was true
if (barssince_cond2 > 0) and (barssince_cond2 < 5) // Was it true within the last 5 bars? Then buy
true
else if (condition2) // If only the condition2 is true, check when was the last time condition1 was true
if (barssince_cond1 > 0) and (barssince_cond1 < 5) // Was it true within the last 5 bars? Then buy
true