首页 > 解决方案 > Tradingview pinescipt heiken ashi color change

问题描述

I'm trying to write a code that sends alert when heiken ashi bars change colour but it keeps sending the alert every time the condition is true but I only want the alert to be triggered once the condition is met

    study("Heikin Ashi Candle Colour Change Alerts", overlay=true)

val = (open + high + low + close) / 4
up = val > val[1]
down = val <= val[1]

alertcondition(condition = up, title = 'Up Candle', message = ' - UP CANDLE')
alertcondition(condition = down, title = 'Down Candle', message = ' - DOWN CANDLE')

plotshape(up, title = 'Up Candle', style = shape.square, color = lime, location = location.bottom)
plotshape(down, title = 'Down Candle', style = shape.square, color = red, location = location.bottom)

标签: pine-script

解决方案


使用一个简单的计数器:

CountUP = 0
CountUP := nz(CountUP[1])
CountDOWN = 0
CountDOWN := nz(CountDOWN[1])

if up
    CountUP += 1
    CountDOWN := 0
    CountDOWN

if down
    CountUP := 0
    CountDOWN += 1
    CountUP

推荐阅读