【问题标题】:Pine script condition 1 and condition 2 fulfilled at up to n steps backPine 脚本条件 1 和条件 2 在最多 n 步后满足
【发布时间】:2021-12-05 20:35:50
【问题描述】:

假设我有条件 1 和条件 2。如果条件 1 和条件 2 在最多 5 个柱内得到满足,那么我想执行一些操作。例如,假设条件 1 在当前收盘时满足,条件 2 在 5 个柱前满足,那么我想执行一些操作。我如何在 Pine 中制定它?

condition1 = ...
condition2 = ...

if (condition1(close)==true or condition1(close-2)==true  or             
condition1(close-3)==true  or condition1(close-4)==true  or     
condition1(close-5)==true)

and (condition2(close)==true or condition2(close-2)==true  or             
condition2(close-3)==true  or condition2(close-4)==true  or     
condition2(close-5)==true)

then...

是否可以这样表述:

if condition1(close:close-5)== true and condition2(close:close-5)== true then ...

我读过例如这个线程: Change background for only the last 5 bars: A Very Simple Problem I can't crack 这听起来像一个类似的问题,但我不确定如何实现它。

【问题讨论】:

    标签: if-statement conditional-statements pine-script


    【解决方案1】:

    a) 您需要使用ta.barssince() 函数。

    https://www.tradingview.com/pine-script-reference/v5/#fun_ta%7Bdot%7Dbarssince

    //@version=5
    indicator("My Script")
    
    /// let's say condition 1 was met at current close, and condition 2 was fulfilled 5 bars ago, then I want to perform some action
    
    ema10 = ta.ema(close, 10)
    condition1 = close > open
    condition2 = ta.crossover(ema10, close)
    
    
    x = false
    
    if condition1 and ta.barssince(condition2) == 5
        x := true
    
    
    plot(ema10)    
    bgcolor(condition2? color.orange:na)
    bgcolor(x?color.green:na)
    
    

    b) 另一种方法是使用历史引用运算符[]

    https://www.tradingview.com/pine-script-docs/en/v5/language/Operators.html#history-referencing-operator

    //@version=5
    indicator("My Script")
    
    // lets say you want to check if condition2 if true for 5 bars, and condition1 is true in the current bar
    
    ema10 = ta.ema(close, 10)
    condition1 = ta.crossover(ema10, close) 
    condition2 = close > open
    
    condition3 = condition2[1] and condition2[2] and condition2[3] and condition2[4] and condition2[5]
    
    
    x = false
    
    if condition1 and condition3
        x := true
    
    plot(ema10)    
    bgcolor(condition2? color.orange:na)
    bgcolor(x?color.green:na)
    
    
    
    
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-10
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    相关资源
    最近更新 更多