I'm days old on pinescript seeking support from the community here.
My strategy has conditions to automate a buy
or sell
alert.
I'm leveraging 3 diff indicators combined. The one I'm blocked on is the UT BOT Indicator
buy
or sell
Currently the alert should works as:
Fire a buy alert IF:
Fire a Sell alert IF:
Edge case:
Ask:
Please scroll down to the end of the code to see my attempts so far:
//@version=5
strategy(title='Combined indicators', shorttitle='COMBIND', overlay=true)
// UT BOT indicator START
// Inputs
a = input(1, title='Key Vaule. \'This changes the sensitivity\'')
c = input(10, title='ATR Period')
h = input(false, title='Signals from Heikin Ashi Candles')
xATR = ta.atr(c)
nLoss = a * xATR
src = h ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close, lookahead=barmerge.lookahead_off) : close
xATRTrailingStop = 0.0
iff_1 = src > nz(xATRTrailingStop[1], 0) ? src - nLoss : src + nLoss
iff_2 = src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), src + nLoss) : iff_1
xATRTrailingStop := src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), src - nLoss) : iff_2
pos = 0
iff_3 = src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)
pos := src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0) ? 1 : iff_3
xcolor = pos == -1 ? color.red : pos == 1 ? color.green : color.blue
ema = ta.ema(src, 1)
above = ta.crossover(ema, xATRTrailingStop)
below = ta.crossover(xATRTrailingStop, ema)
buy = src > xATRTrailingStop and above
sell = src < xATRTrailingStop and below
barbuy = src > xATRTrailingStop
barsell = src < xATRTrailingStop
plotshape(buy, title='Buy', text='Buy', style=shape.labelup, location=location.belowbar, color=color.new(color.green, 0), textcolor=color.new(color.white, 0), size=size.tiny)
plotshape(sell, title='Sell', text='Sell', style=shape.labeldown, location=location.abovebar, color=color.new(color.red, 0), textcolor=color.new(color.white, 0), size=size.tiny)
barcolor(barbuy ? color.green : na)
barcolor(barsell ? color.red : na)
alertcondition(buy, 'UT Long', 'UT Long')
alertcondition(sell, 'UT Short', 'UT Short')
// ******** My Logic *********
// First Approach: Fails to trigger Alert if conditions are met after the chart continues plotting, because the `buy/sell` signal was flashed x candles ago
// send Bot alert
if sell and close < signal and stcRed // only focus on the sell variable here
alert("UT Sell+ LIN REG bellow + STC is RED", alert.freq_once_per_bar_close)
if buy and close > signal and stcGreen // only focus on the buy variable here
alert("UT Buy + LIN REG above + STC is GREEN", alert.freq_once_per_bar_close)
// Second Approach: Change State of buy and sell variables via a boolean check. It fails because once set to true, these will trigger alerts on every single bar close.
var bool buy_signal = false
var bool sell_signal = false
if buy
buy_signal := true
sell_signal:= false
if sell
sell_signal := true
buy_signal := false
log.info("Buy is: " + str.tostring(buy_signal))
log.info("Sell is: " + str.tostring(sell_signal))
// send Bot alert
if sell_signal and close < signal and stcRed
alert("UT Sell+ LIN REG bellow + STC is RED", alert.freq_once_per_bar_close)
if buy_signal and close > signal and stcBuy
alert("UT Buy + LIN REG above + STC is GREEN", alert.freq_once_per_bar_close)
Without the right solution the strategy is flawed and highly prone to losses. See real case scenario on images below
Just use a var
variable to track that.
var ut_was_long = false
if (buy) // New buy signal from UT Bot
ut_was_long := true
else if (sell)
ut_was_long := false
It will keep its value between each iteration so you can use that flag to figure out what the last signal was.