【问题标题】:looking back in time for first green bar回顾第一个绿色酒吧
【发布时间】:2020-09-16 17:54:52
【问题描述】:

我正在使用一些代码(对 Pine 来说非常新),我在其中连续查找 3 根绿色蜡烛并为第一根蜡烛添加标签。使用 bar_index-2,这部分很容易。 但是,假设连续有 5 支绿色蜡烛。我有多个标签,但我只希望标签位于第一个绿色蜡烛上,该蜡烛连续 5 个开始。我如何获得这些信息? 感谢您的帮助! 抢

【问题讨论】:

    标签: pine-script trading


    【解决方案1】:

    这需要一个向下栏来重置条件:

    //@version=4
    study("", "", true)
    
    // Define our boolean condition for an up bar.
    upBar = close > open
    
    // Here we do many things in one line:
    // 1. Using `sum()`, we will add a value for the last 3 bars.
    //    That's the `sum(..., 3)` part.
    // 2. The value we add for each bar is the evaluation of an expression.
    //    That expression is a ternary: `upBar ? 1 : 0`.
    //    It evaluates to `1` if the current bar is an upBar,
    //    and to zero when it is not.
    // 3. We use the `==` boolean operator to compare the result of `sum()` to `3`.
    //    When it is equal to `3`, `true` will be assigned to `upBars3`.
    //    This will occur when `upBar` was true on each of the last 3 bars.
    upBars3 = sum(upBar ? 1 : 0, 3) == 3
    
    // Here we generate a signal when our `upBars3` boolean variable is `true`,
    // and it was not `true` on the previous bar, which is the `not upBars3[1]` part.
    signal = upBars3 and not upBars3[1]
    
    // Here we plot an arrow 2 bars back when our signal triggers.
    // Note that if you publish a script plotting in the past like this,
    // it is expected that you will explain this in your script's description
    // so you do not mislead traders into thinking your script is prescient.
    plotchar(signal, "signal", "▲", location.top, size = size.tiny, offset = - 2)
    

    【讨论】:

    • 谢谢!你能描述 sum(upBar ? 1 : 0, 3) == 3 的作用吗?我将其解读为:如果 upBar 为真,则将值设置为 1,否则设置为 0。
    • 里面有总和和某种测试,看看它是否是 3,但我不理解语法,我想理解它。我在手册的“语言运算符”部分中没有看到这一点,特别是。谢谢!
    • 这几行确实发生了很多事情。我在代码中添加了 cmets。如果我可以这样说,这些台词也是对 Pine 力量的有力说明。如果您有任何问题,请随时咨询。
    猜你喜欢
    • 2019-07-10
    • 1970-01-01
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    相关资源
    最近更新 更多