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 t
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: