关于一次发出警报的 Pinescript 问题

Pinescript issue regarding making an alert hit one single time

攻略代码

我有一个 pine 代码,它根据移动平均线条件进行买卖。代码如下:

study("MAS_Alerts")

qty = input(10000, "Buy quantity")


ma1 = input( "SMA",title="Select MA", options=["SMA", "EMA","TEMA", "WMA","HMA"])


len1 = input(7, minval=1, title="Period")

s=sma(close,len1)

e=ema(close,len1)


xEMA1 = ema(close, len1)
xEMA2 = ema(xEMA1, len1)
xEMA3 = ema(xEMA2, len1)
t = 3 * xEMA1 - 3 * xEMA2 + xEMA3


f_hma(_src, _length)=>
    _return = wma((2 * wma(_src, _length / 2)) - wma(_src, _length), round(sqrt(_length)))

h = f_hma(close, len1)

w = wma(close, len1)

ma = ma1 == "SMA" ? s : ma1 == "EMA" ? e : ma1 == "WMA" ? w : ma1 == "HMA" ? h : ma1 == "TEMA" ? t : na

警报代码

现在我正在尝试通过添加更多代码来从上面的代码中创建一个警报功能,您可以在下面看到:

long_condition = 0
long_count = 1
green = color.green
red = color.red
if(s)
    if(long_count)
        long_count := long_count - 1
        if(s < close)
            long_condition := long_condition + 1
        else
            long_condition := long_condition - 1    

plot(long_condition, "Long", color=green)   

short_condition = 0
short_count = 1

if(s)
    if(short_count)
        short_count := short_count - 1
        if(s > close)
            short_condition := short_condition + 1
        else
            short_condition := short_condition - 1    

plot(short_condition, "Short", color=red)    

我计划只在满足购买条件时生成一次警报:

if(s)
    if(long_count)
        long_count := long_count - 1
        if(s < close)
            long_condition := long_condition + 1
        else
            long_condition := long_condition - 1    

plot(long_condition, "Long", color=green)  

或出售

if(s)
    if(short_count)
        short_count := short_count - 1
        if(s > close)
            short_condition := short_condition + 1
        else
            short_condition := short_condition - 1    

plot(short_condition, "Short", color=red)   

只要满足一个条件,假设当前价格高于 SMA 值:if(s < close) 我们将成功制作长图,因为它首先有效。 现在的主要问题 为什么我必须写这个 post 是因为价格长期保持在 SMA 以上取决于市场趋势我的警报代码 在条件有效的情况下多次触发相同的长图。我只想打印一个警示图,无论是长图还是短图,都只打印一次并停止重复它(我的意思是,如果已经进行了一个长图,我不希望它重复,直到一个新的短图plot condition if(s > close) is valid) and vice versa if once an short plot has already proceeded I don't want it to repeat the short plot alert again and until if a new long plot condition if(s < close) is valid. 如果一个新的长图条件 if(s < close) 有效,则反之亦然。我们怎样才能让它成为可能?

这使用 barssince() 检查消除连续进入信号:

//@version=4
study("MAS_Alerts2")

qty = input(10000, "Buy quantity")


ma1 = input( "SMA",title="Select MA", options=["SMA", "EMA","TEMA", "WMA","HMA"])


len1 = input(7, minval=1, title="Period")

s=sma(close,len1)

e=ema(close,len1)


xEMA1 = ema(close, len1)
xEMA2 = ema(xEMA1, len1)
xEMA3 = ema(xEMA2, len1)
t = 3 * xEMA1 - 3 * xEMA2 + xEMA3


f_hma(_src, _length)=>
    _return = wma((2 * wma(_src, _length / 2)) - wma(_src, _length), round(sqrt(_length)))

h = f_hma(close, len1)

w = wma(close, len1)

ma = ma1 == "SMA" ? s : ma1 == "EMA" ? e : ma1 == "WMA" ? w : ma1 == "HMA" ? h : ma1 == "TEMA" ? t : na
long_condition = 0
long_count = 1
green = color.green
red = color.red

long_trigger = s < close
short_trigger = s > close
b_since_long = barssince(long_trigger)[1]
b_since_short = barssince(short_trigger)[1]
if(s)
    if(long_count)
        long_count := long_count - 1
        if long_trigger and b_since_long > b_since_short
            long_condition := long_condition + 1
        else
            long_condition := long_condition - 1    

plot(long_condition, "Long", color=green)   

short_condition = 0
short_count = 1

if(s)
    if(short_count)
        short_count := short_count - 1
        if short_trigger and b_since_short > b_since_long
            short_condition := short_condition + 1
        else
            short_condition := short_condition - 1    

plot(short_condition, "Short", color=red)    
bgcolor(long_trigger ? color.green : short_trigger ? color.red : na)
plotchar(b_since_long, "b_since_long", "", location.top, size = size.tiny)
plotchar(b_since_short, "b_since_short", "", location.top, size = size.tiny)

上面是你的,下面是新版本: