Page 1 of 1

How can I avoid the error: "The alert is triggered too often"?

Posted: Wed Mar 09, 2022 8:09 pm
by EveryDayBetter
Hello. How can I avoid the error "The alert is triggered too often", when I wanna get alerts from the following script? Are there mistakes in the script?

Code: Select all

//@version=5
strategy("US stocks 1 - Pullback SMA 50 Screener ", overlay=true, max_bars_back=4000)

// General Function
f_barssince(_cond, _count) =>
    _barssince = int(math.max(1, nz(bar_index - ta.valuewhen(_cond, bar_index, _count))))
    _barssince

barssince(_cond, _count) => int(math.max(1, nz(f_barssince(_cond, _count))))
f_vw(cond, expr, count) => ta.valuewhen(cond, expr, count)

tostring(x, y)=> x + str.tostring(y)

var int dec = str.length(str.tostring(syminfo.mintick))-2

truncate(number, decimals) =>
    factor = math.pow(10, decimals)
    int(number * factor) / factor

// --------
gr1         = 'Table Style'
tabPosI     = 'Middle' //input.string('Middle', 'Table Position', ['Top', 'Middle', 'Bot'], group=gr1)
titleCol    = color.white //input.color(color.white, 'Background -- Title', '', '1', gr1)
pairCol     = color.white //input.color(color.white, 'Pair', '', '1', gr1)
textCol     = color.black //input.color(color.black, 'Text Color', group=gr1)

textSizeI   = 'Small' //input.string('Small', 'Text Size', ['Small', 'Tiny', 'Normal'], group=gr1)
textSize    = textSizeI=='Small'? size.small : textSizeI=='Tiny'? size.tiny : size.normal 

tabPos      = tabPosI=='Top'? position.top_right : tabPosI=='Bot'? position.bottom_right : position.middle_right
screenerTable   = table.new(tabPos, 50,50, color.new(color.black,100), color.black, 1, color.black, 1)



// ------------------------------------
// Screener
showTable   = false
fScreen(pair, tf, rowNumber, colNumber)=>
    // The condition will you screen -> in this script, the cond named with lCond_
    lCond_      = close > ta.sma(close, 150) and low < ta.sma(close, 50)  // <-- Here is the place for the entry condition
    Col1        = color.blue
    fixRow      = rowNumber+1
    
    // alerting -> 'BreakUp On' is the message
    if pair!='' and lCond_ 
        alert('BreakUp on' +pair+tf, alert.freq_once_per_bar_close)
    
    // table for debugging and confirming that the screener have screened well, change showTable to true for showing the table
    fixCol2     = colNumber==1? 0 : 2
    fixCol1     = colNumber==1? 1 : 3
    if pair!='' and showTable  
        table.cell(screenerTable, fixCol1, 0, 'Pair'              , bgcolor=titleCol, text_size=textSize, text_color=textCol)
        table.cell(screenerTable, fixCol1, rowNumber, pair+' '+tf , bgcolor=pairCol , text_size=textSize, text_color=textCol)
        table.cell(screenerTable, fixCol2, 0, 'Condition'         , bgcolor=titleCol, text_size=textSize, text_color=textCol)
        table.cell(screenerTable, fixCol2, rowNumber, reqCond1    , bgcolor=pairCol , text_size=textSize, text_color=textCol)

// Input Pair and TF
gr12    = 'Watchlist'
pair1   = input.symbol('FAST', '1', '', '1', gr12, true), tf1 = input.timeframe('D', '', inline='1', group= gr12, confirm=true) 
pair2   = input.symbol('HD', '2', '', '2', gr12, true), tf2 = input.timeframe('D', '', inline='2', group= gr12, confirm=true) 
pair3   = input.symbol('ASML', '3', '', '3', gr12, true), tf3 = input.timeframe('D', '', inline='3', group= gr12, confirm=true) 
pair4   = input.symbol('BNTX', '4', '', '4', gr12, true), tf4 = input.timeframe('D', '', inline='4', group= gr12, confirm=true) 
pair5   = input.symbol('ETSY', '5', '', '5', gr12, true), tf5 = input.timeframe('D', '', inline='5', group= gr12, confirm=true) 
pair6   = input.symbol('GOOG', '6', '', '6', gr12, true), tf6 = input.timeframe('D', '', inline='6', group= gr12, confirm=true) 
pair7   = input.symbol('NTES', '7', '', '7', gr12, true), tf7 = input.timeframe('D', '', inline='7', group= gr12, confirm=true) 
pair8   = input.symbol('STM', '8', '', '8', gr12, true), tf8 = input.timeframe('D', '', inline='8', group= gr12, confirm=true) 
pair9   = input.symbol('AAPL', '9', '', '9', gr12, true), tf9 = input.timeframe('D', '', inline='9', group= gr12, confirm=true) 
pair10  = input.symbol('ABBV', '10', '', '10', gr12, true), tf10 = input.timeframe('D', '', inline='10', group= gr12, confirm=true) 
pair11  = input.symbol('ABNB', '11', '', '11', gr12, true), tf11 = input.timeframe('D', '', inline='11', group= gr12, confirm=true) 
pair12  = input.symbol('ADBE', '12', '', '12', gr12, true), tf12 = input.timeframe('D', '', inline='12', group= gr12, confirm=true) 
pair13  = input.symbol('AMD', '13', '', '13', gr12, true), tf13 = input.timeframe('D', '', inline='13', group= gr12, confirm=true) 
pair14  = input.symbol('AMT', '14', '', '14', gr12, true), tf14 = input.timeframe('D', '', inline='14', group= gr12, confirm=true) 
pair15  = input.symbol('AMZN', '15', '', '15', gr12, true), tf15 = input.timeframe('D', '', inline='15', group= gr12, confirm=true) 
pair16  = input.symbol('BKNG', '16', '', '16', gr12, true), tf16 = input.timeframe('D', '', inline='16', group= gr12, confirm=true) 
pair17  = input.symbol('BLK', '17', '', '17', gr12, true), tf17 = input.timeframe('D', '', inline='17', group= gr12, confirm=true) 
pair18  = input.symbol('BRK-B', '18', '', '18', gr12, true), tf18 = input.timeframe('D', '', inline='18', group= gr12, confirm=true) 
pair19  = input.symbol('COIN', '19', '', '19', gr12, true), tf19 = input.timeframe('D', '', inline='19', group= gr12, confirm=true) 
pair20  = input.symbol('CRM', '20', '', '20', gr12, true), tf20 = input.timeframe('D', '', inline='20', group= gr12, confirm=true) 
pair21  = input.symbol('CSCO', '21', '', '21', gr12, true), tf21 = input.timeframe('D', '', inline='21', group= gr12, confirm=true) 
pair22  = input.symbol('NOW', '22', '', '22', gr12, true), tf22 = input.timeframe('D', '', inline='22', group= gr12, confirm=true) 
pair23  = input.symbol('DHR', '23', '', '23', gr12, true), tf23 = input.timeframe('D', '', inline='23', group= gr12, confirm=true) 
pair24  = input.symbol('DIS', '24', '', '24', gr12, true), tf24 = input.timeframe('D', '', inline='24', group= gr12, confirm=true) 
pair25  = input.symbol('FB', '25', '', '25', gr12, true), tf25 = input.timeframe('D', '', inline='25', group= gr12, confirm=true) 
pair26  = input.symbol('GOOGL', '26', '', '26', gr12, true), tf26 = input.timeframe('D', '', inline='26', group= gr12, confirm=true) 
pair27  = input.symbol('IBM', '27', '', '27', gr12, true), tf27 = input.timeframe('D', '', inline='27', group= gr12, confirm=true) 
pair28  = input.symbol('INTC', '28', '', '28', gr12, true), tf28 = input.timeframe('D', '', inline='28', group= gr12, confirm=true) 
pair29  = input.symbol('ISRG', '29', '', '29', gr12, true), tf29 = input.timeframe('D', '', inline='29', group= gr12, confirm=true) 
pair30  = input.symbol('JNJ', '30', '', '30', gr12, true), tf30 = input.timeframe('D', '', inline='30', group= gr12, confirm=true) 
pair31  = input.symbol('KO', '31', '', '31', gr12, true), tf31 = input.timeframe('D', '', inline='31', group= gr12, confirm=true) 
pair32  = input.symbol('LMT', '32', '', '32', gr12, true), tf32 = input.timeframe('D', '', inline='32', group= gr12, confirm=true) 
pair33  = input.symbol('MA', '33', '', '33', gr12, true), tf33 = input.timeframe('D', '', inline='33', group= gr12, confirm=true) 
pair34  = input.symbol('MCD', '34', '', '34', gr12, true), tf34 = input.timeframe('D', '', inline='34', group= gr12, confirm=true) 
pair35  = input.symbol('MMM', '35', '', '35', gr12, true), tf35 = input.timeframe('D', '', inline='35', group= gr12, confirm=true) 
pair36  = input.symbol('MO', '36', '', '36', gr12, true)   , tf36 = input.timeframe('D', '', inline='36', group= gr12, confirm=true)
pair37  = input.symbol('MSFT', '37', '', '37', gr12, true)   , tf37 = input.timeframe('D', '', inline='37', group= gr12, confirm=true)
pair38  = input.symbol('NEE', '38', '', '38', gr12, true), tf38 = input.timeframe('D', '', inline='38', group= gr12, confirm=true)
pair39  = input.symbol('NFLX', '39', '', '39', gr12, true)  , tf39 = input.timeframe('D', '', inline='39', group= gr12, confirm=true)
pair40  = input.symbol('NKE', '40', '', '40', gr12, true)   , tf40 = input.timeframe('D', '', inline='40', group= gr12, confirm=true)

fScreen(pair1, tf1, 2, 1)
fScreen(pair2, tf2, 3, 1)
fScreen(pair3, tf3, 4, 1)
fScreen(pair4, tf4, 5, 1)
fScreen(pair5, tf5, 6, 1)
fScreen(pair6, tf6, 7, 1)
fScreen(pair7, tf7, 8, 1)
fScreen(pair8, tf8, 9, 1)
fScreen(pair9, tf9, 10, 1)
fScreen(pair10, tf10, 11, 1)
fScreen(pair11, tf11, 12, 1)
fScreen(pair12, tf12, 13, 1)
fScreen(pair13, tf13, 14, 1)
fScreen(pair14, tf14, 15, 1)
fScreen(pair15, tf15, 16, 1)
fScreen(pair16, tf16, 17, 1)
fScreen(pair17, tf17, 18, 1)
fScreen(pair18, tf18, 19, 1)
fScreen(pair19, tf19, 20, 1)
fScreen(pair20, tf20, 1, 2)
fScreen(pair21, tf21, 2, 2)
fScreen(pair22, tf22, 3, 2)
fScreen(pair23, tf23, 4, 2)
fScreen(pair24, tf24, 5, 2)
fScreen(pair25, tf25, 6, 2)
fScreen(pair26, tf26, 7, 2)
fScreen(pair27, tf27, 8, 2)
fScreen(pair28, tf28, 9, 2)
fScreen(pair29, tf29, 10, 2)
fScreen(pair30, tf30, 11, 2)
fScreen(pair31, tf31, 12, 2)
fScreen(pair32, tf32, 13, 2)
fScreen(pair33, tf33, 14, 2)
fScreen(pair34, tf34, 15, 2)
fScreen(pair35, tf35, 16, 2)
fScreen(pair36, tf36, 17, 2)
fScreen(pair37, tf37, 18, 2)
fScreen(pair38, tf38, 19, 2)
fScreen(pair39, tf39, 20, 2)
fScreen(pair40, tf40, 21, 2)

//strategy 
if close > ta.sma(close, 150) and close < ta.sma(close, 50)
    strategy.entry('long', strategy.long)

//Declaring percentage numbers
SL = 0.15
TP = 0.04

//Calculating the exit price for sl and tp
longstop = strategy.position_avg_price * (1 - SL)
longprofit = strategy.position_avg_price * (1 + TP)

//for long positions
if strategy.position_size > 0
    strategy.exit(id='close', stop=longstop, limit=longprofit)

sma1 = ta.sma(close, 50)
sma2 = ta.sma(close, 150)

plot(sma1, color=color.new(#000000, 0))
plot(sma2, color=color.new(#7FFF00, 0))

Re: How can I avoid the error: "The alert is triggered too often"?

Posted: Sat Mar 26, 2022 9:41 am
by processingclouds
Hey,

It would be better to separate the alert outside the fScreen function, and instead add a cumulative alert for all the symbol calls.
This will avoid alert being triggered too often. As you have 40 pairs, using that function, some of them are triggering at the same time. So better way would be to combine the messages as a plain string or json format and than call the alert after all fScreen functions for all pairs have been called.