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
- I need to find a way to retrieve when and if the last flashed signal from the indicator was a
buy
orsell
- Just for reference these are the other two indicators I have on my strategy: linear regression candles and stc
Currently the alert should works as:
Fire a buy alert IF:
- UT Bot Indicator sends a Buy Signal
- STC indicator is green
- Linear Regression Candle is closed above the signal white line
Fire a Sell alert IF:
- UT Bot Indicator sends a Sell Signal
- STC indicator is red
- Linear Regression Candle is closed below the signal white line
Edge case:
- Alert won’t trigger when UT Bot indicator flashes Buy or Sell, but the STC and Linear Regression Candles conditions are not met.
Ask:
- Once other conditions are met (STC and Linear Regression): Find in history the candle where and what signal (buy or sell) the UT Bot Indicator last flashed… then alert() accordingly.
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