Pine script 在首先满足条件的位置绘制形状,但不在连续的蜡烛上绘制?

Pine script 在首先满足条件的位置绘制形状,但不在连续的蜡烛上绘制?,pine-script,Pine Script,我正在尝试编写一个指示器,它为绘制的形状生成多个真实条件。例如,MACD交叉与ADX条件相结合,如下所示: study("MACD on chart", shorttitle="MACD on Chart", overlay=true) // MACD // Inputs fast_length = input(title="Fast Length", type=input.integer, defval=6) slow_length

我正在尝试编写一个指示器,它为绘制的形状生成多个真实条件。例如,MACD交叉与ADX条件相结合,如下所示:

study("MACD on chart", shorttitle="MACD on Chart", overlay=true)

// MACD
// Inputs
fast_length = input(title="Fast Length", type=input.integer, defval=6)
slow_length = input(title="Slow Length", type=input.integer, defval=13)
src = input(title="Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 4)
sma_source = input(title="Simple MA(Oscillator)", type=input.bool, defval=false)
sma_signal = input(title="Simple MA(Signal Line)", type=input.bool, defval=false)
// Calculation
fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
// hist = macd - signal

// DMI
// Inputs
lensig = input(14, title="ADX Smoothing", minval=1, maxval=50)
len = input(14, minval=1, title="DI Length")
// Calculation
up = change(high)
down = -change(low)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
trur = rma(tr, len)
plus = fixnan(100 * rma(plusDM, len) / trur)
minus = fixnan(100 * rma(minusDM, len) / trur)
sum = plus + minus
adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)


longCondition = macd >= signal and adx >= 20
plotshape(longCondition, style=shape.labelup, size=size.tiny, color=color.new(color.green, 60), location=location.belowbar)

shortCondition = signal <= macd and adx >= 20
plotshape(shortCondition, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 60), location=location.abovebar)
研究(“图表上的MACD”,shorttitle=“图表上的MACD”,overlay=true)
//MACD
//投入
fast_length=input(title=“fast length”,type=input.integer,deffal=6)
slow_length=input(title=“slow length”,type=input.integer,deffal=13)
src=input(title=“Source”,type=input.Source,deffal=close)
信号长度=输入(title=“信号平滑”,type=input.integer,minval=1,maxval=50,deffal=4)
sma_源=输入(title=“简单MA(振荡器)”,type=input.bool,deffal=false)
sma_信号=输入(title=“简单MA(信号线)”,type=input.bool,deffal=false)
//算计
fast\u ma=sma\u源?sma(src,快速长度):ema(src,快速长度)
慢速\u ma=sma\u源?sma(src,慢速长度):ema(src,慢速长度)
macd=快-慢
信号=sma_信号?sma(macd,信号长度):ema(macd,信号长度)
//hist=macd-信号
//DMI
//投入
lensig=输入(14,title=“ADX平滑”,最小值=1,最大值=50)
len=输入(14,minval=1,title=“DI长度”)
//算计
上升=变化(高)
向下=-变化(低)
plusDM=na(向上)?na:(向上>向下和向上>0?向上:0)
minusDM=na(向下)?na:(向下>上下>0?向下:0)
trur=rma(tr,len)
plus=fixnan(100*rma(plusDM,len)/trur)
减=固定值(100*rma(最小值,长)/trur)
总和=正+负
adx=100*rma(绝对值(加减)/(总和=0?1:总和),lensig)
长条件=macd>=信号和adx>=20
plotshape(longCondition,style=shape.labelup,size=size.tiny,color=color.new(color.green,60),location=location.belowbar)
短路条件=信号=20
plotshape(shortCondition,style=shape.labeldown,size=size.tiny,color=color.new(color.red,60),location=location.overbar)
不幸的是,这会在图表上产生多个绘制的形状,因为对于多个蜡烛来说,这种情况是正确的。相反,它希望只有第一次满足新条件的情况下,才有一个新的形状

为了避免一个问题:不,在这种情况下我不能使用
交叉
,因为我在脚本中使用它的方式会产生错误的结果。交叉仅在它实际交叉的蜡烛上是正确的,但条件是以某种方式设置的,即实际交叉不会是正确的触发蜡烛。我希望这是有道理的

下面是一个直观的示例:


此代码将满足您的需要。
我看到您正在计算和,它也作为内置函数存在。
您可以在中找到更多信息。
另外,您不需要创建
color.new()
来获得
plotshape()
中的透明度,因为它可以作为参数使用

//@version=4
study("MACD on chart", shorttitle="MACD on Chart", overlay=true)

var bool longCondition          = na
var bool shortCondition         = na
var bool showLong               = na
var bool showShort              = na
var bool showOnlyFirstSignal    = input(true, "Show only first signal", input.bool)

// MACD
// Inputs
fast_length     = input(title="Fast Length",            type=input.integer, defval=6)
slow_length     = input(title="Slow Length",            type=input.integer, defval=13)
src             = input(title="Source",                 type=input.source,  defval=close)
signal_length   = input(title="Signal Smoothing",       type=input.integer, defval= 4, minval = 1, maxval = 50)
sma_source      = input(title="Simple MA(Oscillator)",  type=input.bool,    defval=false)
sma_signal      = input(title="Simple MA(Signal Line)", type=input.bool,    defval=false)

// Calculation
fast_ma         = sma_source ? sma(src, fast_length) : ema(src, fast_length)
slow_ma         = sma_source ? sma(src, slow_length) : ema(src, slow_length)
macd            = fast_ma - slow_ma
signal          = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
// hist = macd - signal

// DMI
// Inputs
lensig          = input(14, title="ADX Smoothing", minval=1, maxval=50)
len             = input(14, minval=1, title="DI Length")
// Calculation
up              = change(high)
down            = -change(low)
plusDM          = na(up)   ? na : (up > down and up > 0   ? up   : 0)
minusDM         = na(down) ? na : (down > up and down > 0 ? down : 0)
trur            = rma(tr, len)
plus            = fixnan(100 * rma(plusDM,  len) / trur)
minus           = fixnan(100 * rma(minusDM, len) / trur)
sum             = plus + minus
adx             = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), lensig)

longCondition   := macd >= signal and adx >= 20
shortCondition  := signal <= macd and adx >= 20

showLong        := showOnlyFirstSignal ? longCondition  and not longCondition[1]  : longCondition
showShort       := showOnlyFirstSignal ? shortCondition and not shortCondition[1] : shortCondition

plotshape(showLong,  style=shape.labelup,   size=size.tiny, color=color.green, transp=60, location=location.belowbar)
plotshape(showShort, style=shape.labeldown, size=size.tiny, color=color.red,   transp=60, location=location.abovebar)
长版本

if showOnlyFirstSignal
    // To show Long on the chart, the longCondition on the current bar must be true, but the longCondition on the previous bar must be false.
    // So, as long as the longCondition on the previous bar stays true, showLong will be false, resulting in it not showing on the chart.
    // The reverse is also true: showLong can only become true, if the longCondition on the previous bar was false.
    showLong    := longCondition and not longCondition[1]
else
    showLong    := longCondition

您可能还想看一下,这可能会为您进一步澄清问题。

您的问题并不完全清楚。你是说你想在整个图表上只有一个形状?也就是说,只在第一次满足条件时才绘制形状,但在第一次出现后就不再绘制形状了?即使条件在第一次发生后再次变为真?你能用一个你正在寻找的带注释的屏幕截图来更新你的问题吗?如果可能的话,还有到目前为止的整个脚本,以便我们可以对其进行评估?谢谢,当然不是。仅在第一次,该条件在一系列中再次满足。我只是不想让它连续出现多次,然后当条件再次满足时,再出现一个情节。你能发布一个脚本来说明你的意思吗?以及它发生的时间范围。我相信我能帮你解决这个问题。谢谢你的编辑。我试着给你们准备一个最简单的例子。谢谢你的帮助。为了让问题更容易理解,我修改了问题,并添加了一个图表,这应该有助于解释问题。哦,哇。事实上,这很有道理。谢谢你,比约恩。你能给我解释一下longCondition和not longCondition[1]:longCondition的作用吗?我理解它就像是if条件的一个简短形式。所以showLong是真的,如果longCondition是真的,但不是1巴回来,否则它只是longCondition。对吗?对。更新了我的答案,解释了你的问题。Pine中以前的值用
[n]
引用,其中
n
表示从当前条返回的条数。Bjorn,这是一个怪物答案。我没想到会有这么多帮助。非常感谢。:)
if showOnlyFirstSignal
    // To show Long on the chart, the longCondition on the current bar must be true, but the longCondition on the previous bar must be false.
    // So, as long as the longCondition on the previous bar stays true, showLong will be false, resulting in it not showing on the chart.
    // The reverse is also true: showLong can only become true, if the longCondition on the previous bar was false.
    showLong    := longCondition and not longCondition[1]
else
    showLong    := longCondition