首页 > 解决方案 > 在 pine-script 中如何防止在多种情况下连续发出警报?

问题描述

嗨,我是 pine 脚本新手 :)
我做了一些脚本,参考了 stackoverflow,它几乎完成了,但是代码中有一些问题,即连续警报。

我为多元化投资发出了 4 个警报(buy25,50,75,99),我的意图是一旦满足一个条件关闭其他条件,所以一个条件进入一个柱(例如:如果 buy25 --> 忽略 buy50,75,99)

这是我的完整代码

//@version=4
study(title="test1", overlay=true)

//noise ratio
R = 0.5
range = high[1] - low[1]

//SMA Split
longa= close >= sma(close, 3)
longb= close >= sma(close, 5)
longc= close >= sma(close, 10)
longd= close >= sma(close, 20)

sm3 = if (longa)
    1
else
    0
sm5 = if (longb)
    1
else
    0
sm10 = if (longc)
    1
else
    0
sm20 = if (longd)
    1
else
    0

smq = sm3 + sm5 + sm10 + sm20

//show target price
buy_price = open + R * range
plot(buy_price, title='buy_price', color= color.black, linewidth = 2, style=plot.style_stepline)
BUYING = high > buy_price  and smq>=1
plotshape(BUYING, style=shape.arrowup, location=location.belowbar ,color=color.purple, size=size.small)
exit = high[1] > buy_price[1] and smq[1] >= 1 and time_close
plotshape(exit, style=shape.labeldown, location=location.abovebar , size=size.small)


// buying condition
buy25 = smq == 1 and close > buy_price
buy50 = smq == 2 and close > buy_price
buy75 = smq == 3 and close > buy_price
buy99 = smq == 4 and close > buy_price
CLOSE = time_close

//for prevent duplicate alert
var bought = false
buy_25 = false
buy_50 = false
buy_75 = false
buy_99 = false
closing = false

if buy25 and not bought
    bought := true
    buy_25 := true

if buy50 and not bought
    bought := true
    buy_50 := true

if buy75 and not bought
    bought := true
    buy_75 := true

if buy99 and not bought
    bought := true
    buy_99 := true

if CLOSE and bought
    bought := false
    closing := true

//alert
alertcondition(closing, title='CLOSE', message='CLOSE')
alertcondition(buy_25, title= 'buy25', message= 'buy25')
alertcondition(buy_50, title= 'buy50', message= 'buy50')
alertcondition(buy_75, title= 'buy75', message= 'buy75')
alertcondition(buy_99, title= 'buy99', message= 'buy99')

我挣扎了一天来解决这个问题(谷歌搜索,翻找stackoverflow),我发现使用“var购买”但未能解决..所以任何人都可以解决我的问题,请告诉我!谢谢你,祝你有美好的一天

标签: pine-script

解决方案


你可以尝试用 替换bought := truebought = true


推荐阅读