How can I stop an alertcondition once its been act

2019-08-28 02:24发布

问题:

I am triggering two separate alertconditions (when a crossover and crossunder happens)

They cross over a few time after this alert and this trigger it multiple times. I'm hoping to set a condition so that once they've done it once it no longer triggers the alertcondition until the other alertcondition is triggered

aka alertcondition(long...) is triggered once only even if its conditions happen again but the condition is reinstated after alertcondition(short...) is triggered and vice versa

long = crossover(RSIMain,SellAlertLevel)
short = crossunder(RSIMain,BuyAlertLevel)

alertcondition(long, title='BUY', message='BUY!')
alertcondition(short, title='SELL', message='SELL!')

plotshape(long, style=shape.arrowup, text="Long", color=green, location=location.belowbar, size=size.auto)
plotshape(short, style=shape.arrowdown, text="Short", color=red, location=location.abovebar, size=size.auto)

isLongOpen = false
isShortOpen = false

then at the bottom of code:

if (long)
    isLongOpen := true
    isShortOpen := false

if (short)
    isShortOpen := true
    isLongOpen := false

alertcondition((long and not isLongOpen), title....)
plotshape((long and not isLongOpen), title....)

回答1:

Well, it might help to plot long and short. It visualizes your problem.

long or short is true, whenever a crossover/crossunder happens. And whenever it is true, your alert gets triggered.

You need to use a flag to figure out if you are already long or short. So, you can only "BUY" / "SELL" if you have not already bought/sold.

You can do this like that:

//@version=4
study("My Script", overlay=true)

SellAlertLevel = 30
BuyAlertLevel = 70

isLong = false              // A flag for going LONG
isLong := nz(isLong[1])     // Get the previous value of it

isShort = false             // A flag for going SHORT
isShort := nz(isShort[1])   // Get the previous value of it

RSIMain = rsi(close, 14)

buySignal = crossover(RSIMain,SellAlertLevel) and not isLong    // Buy only if we are not already long
sellSignal = crossunder(RSIMain,BuyAlertLevel) and not isShort  // Sell only if we are not already short

if (buySignal)
    isLong := true
    isShort := false

if (sellSignal)
    isLong := false
    isShort := true

plotshape(series=buySignal, title="BUY", text="BUY", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(series=sellSignal, title="SELL", text="SELL", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)



标签: pine-script