我的pine脚本代码有以下问题
working version 1:
buy_signal = condition1
working version 2:
buy_signal = condition1
working version 3:
buy_signal = condition1 or condition2
但以下内容不会产生任何交易...
唯一不起作用的是:
buy_signal = condition1 and condition2
这肯定是由于信息发布日期不同,也就是说它们不是同时真实的。
这就是为什么我需要一个函数/解决方法来检查条件在特定时间范围内是否为真
您可以使用
ta.barssince()
查看您的状况何时为 true
。然后用它作为条件。
ta.barssince()
计算自上次条件为真以来的柱数。
ta.barssince(condition) → series int
类似这样的:
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