This is part of a pine script for tradingview. On the script after '//Condition', I want an alert to generate only when the condition changes from long to short or short to long. Not the end of each candle as it does now, as one condition is always true.
This has been changed to a study.
threshold = input(title="Threshold", type=float, defval=0.0014, step=0.0001)
buying = l3_0 > threshold ? true : l3_0 < -threshold ? false : buying[1]
///// T edit
selling = l3_0 > -threshold ? true : l3_0 < threshold ? false :
selling[1] //// T edit END
hline(0, title="base line")
bgcolor(l3_0 > 0.0014 ? green : l3_0 < -0.0014 ? red : gray, transp=20)
bgcolor(buying ? green : red, transp=20)
plot(l3_0, color=silver, style=area, transp=75)
plot(l3_0, color=aqua, title="prediction")
///// Stragegy
/////////////////////////////////////////////////////
//longCondition = buying
//if (longCondition)
//strategy.entry("Long", strategy.long)
//shortCondition = buying != true
//if (shortCondition)
//strategy.entry("Short", strategy.short)
///// Alerts ///////////////////////////////////////////////////////alertcondition(condition, title, message)
//Condition
long = l3_0 > 0.0014
short = l3_0 < -0.0014
alertcondition(long, title = "ANN Long", message= "ANN Long")
alertcondition(short, title = "ANN Short", message= "ANN Short")
Let's look at a smaller example using MACD. We want to go long whenever delta
is >= 0
and go short whenever delta
is <0
. Also, we would like to stay in our position unless the opposite signal is triggered (enter once and wait for the opposite signal).
Your code looks like below:
//@version=3
study("My Script", overlay=true)
// Get the inputs
MACDLengthMACD = input(title="MACD Length", defval=9, minval=1, maxval=100)
fastLengthMACD = input(title="MACD Fast Length", defval=12, minval=1, maxval=100)
slowlengthMACD = input(title="MACD Slow Length", defval=26, minval=1, maxval=100)
// Standard MACD calculations
MACD = ema(close, fastLengthMACD) - ema(close, slowlengthMACD)
aMACD = ema(MACD, MACDLengthMACD)
deltaMACD = MACD - aMACD
buySignal = (deltaMACD >= 0)
sellSignal= (deltaMACD < 0)
plotshape(series=buySignal, text="BUY", style=shape.triangleup, location=location.belowbar, color=green, size=size.small)
plotshape(series=sellSignal, text="SELL", style=shape.triangledown, location=location.abovebar, color=red, size=size.small)
In this case, you will get multiple BUY or SELL signals because buySignal
and sellSignal
will be true
as long as their conditions are true
.
However, those signals should be true
for one bar only in order to trigger only one BUY or SELL signal. To accomplish that, you can use another variable (isLong
, isShort
in below code) and use history reference operator []
to determine if you were previously LONG or SHORT.
Then, only trigger your BUY signal if you are not already LONG and only trigger your SELL signal if you are not already SHORT. This way you will get only one BUY or SELL signal.
//@version=3
study("My Script", overlay=true)
// Get the inputs
MACDLengthMACD = input(title="MACD Length", defval=9, minval=1, maxval=100)
fastLengthMACD = input(title="MACD Fast Length", defval=12, minval=1, maxval=100)
slowlengthMACD = input(title="MACD Slow Length", defval=26, minval=1, maxval=100)
// Standard MACD calculations
MACD = ema(close, fastLengthMACD) - ema(close, slowlengthMACD)
aMACD = ema(MACD, MACDLengthMACD)
deltaMACD = MACD - aMACD
// Deternine if we are currently LONG
isLong = false
isLong := nz(isLong[1], false)
// Determine if we are currently SHORT
isShort = false
isShort := nz(isShort[1], false)
// Buy only if the buy signal is triggered and we are not already long
buySignal = not isLong and (deltaMACD >= 0)
// Sell only if the sell signal is triggered and we are not already short
sellSignal= not isShort and (deltaMACD < 0)
if (buySignal)
isLong := true
isShort := false
if (sellSignal)
isLong := false
isShort := true
plotshape(series=buySignal, text="BUY", style=shape.triangleup, location=location.belowbar, color=green, size=size.small)
plotshape(series=sellSignal, text="SELL", style=shape.triangledown, location=location.abovebar, color=red, size=size.small)
This will result in: