如果警报条件为真,如何在图表上绘制形状

How to plot a shape on a chart if alert conditions are true

这是我目前正在为警报系统编写的代码。我边学边学,所以请随时纠正任何错误,即使它与我的问题无关。

//WORK IN PROGRESS

//@version=4
study("Multiple Indicator Alert WIP", overlay = true)


//Custom inputs for each indicator
rsilen = input(title="RSI Length", type=input.integer, defval=14)
emalen1 = input(title="1st EMA Length", type=input.integer, defval=12, minval=1)
emalen2 = input(title="2nd EMA Length", type=input.integer, defval=24)
emalen3 = input(title="3rd EMA Length", type=input.integer, defval=30)
emalen4 = input(title="4th EMA Length", type=input.integer, defval=60)
DIlen = input(title="DI Length", type=input.integer, defval=7)
ADXSmoothlen=input(title="ADX Smoothing", type=input.integer, defval=7)

//variables for all indicators (incomplete)
RSI = rsi(close, rsilen)
EMA1 = ema(close, emalen1)
EMA2 = ema(close, emalen2)
EMA3 = ema(close, emalen3)
EMA4 = ema(close, emalen4)
[diplus, diminus, adx] = dmi(DIlen, ADXSmoothlen)
price = close

bullishcondition = RSI > 50 and EMA1 > EMA2 and price > EMA1 + EMA2 and diplus > diminus

alertcondition(condition= bullishcondition, message="All conditions bullish for 4 hour chart")

我试图让它在满足警报条件时在图表上绘制一些东西。也许是一个小三角形之类的。我读过使用 if 语句不可能与 plot() 参数一起使用,并且我已经看到有关如何执行此操作的示例,但我仍然感到困惑。我想这样做,这样我就可以查看代码是否有效,而不必实际设置警报并等待它触发。提前致谢。

我曾经在 alertcondition 参数中设置条件,但我认为将其设为变量更有效,这样我就可以调整变量并在变量为 true.

如果要绘制形状,可以使用 plotshapelabel.new,也可以使用 plotcharplotarrow 作为箭头。

plotshape 通常是最好的选择,可以按如下方式使用:

plotshape(bullishcondition,style=shape.triangleup,location=location.belowbar,color=#0cb51a)

label.new 可以与 if 语句一起使用。

label l = na
n = bar_index
if bullishcondition
    l := label.new(n,low,color=#0cb51a,style=label.style_arrowup)

为了绘制具有相对于其他观察系列的位置的形状,请在 plotshape 中使用 location.absolute,如下所示:

plotshape(bullishcondition ? rsi : na,style=shape.square,location=location.absolute,color=#0cb51a)

此处在 bullishcondition 为真时,在 rsi 值处绘制了一个正方形。