检查在特定时间范围内是否为 True |松脚本V5

问题描述 投票:0回答:1

我的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

这肯定是由于信息发布日期不同,也就是说它们不是同时真实的。

这就是为什么我需要一个函数/解决方法来检查条件在特定时间范围内是否为真

pine-script pine-script-v5 trading
1个回答
0
投票

您可以使用

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
© www.soinside.com 2019 - 2024. All rights reserved.