问题描述
我正在处理一些代码(这是Pine的新手),在其中我连续寻找3个绿色蜡烛,并向第一个蜡烛添加标签。使用bar_index-2可以轻松完成该部分。 但是,假设连续有5支绿色蜡烛。我有多个标签,但我只希望标签位于连续5个蜡烛的第一个绿色蜡烛上。我如何获得该信息? 谢谢你的帮助! 罗布
解决方法
这需要一个向下条以重置条件:
//@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)