0

i have written a pinescript using array ( with parts borrowed from others). Once i put it up on tradingview and ran it is working but the problem is that when i get an alert, it sends the alert for the first symbol in the array and not the actual one that triggered it ( had it set to alert.freq_*once _*per_bar ). If i set it to alert.freq all, it sends an alert for all of the symbols regardless of which one has triggered it.

Below i have put up the script i was using to test. i was using the 5m chart to set it and had all 4 open so i could tell exactly whether or not the alert should have been triggered.

I am sure it is something i have messed up in the code but cant work out what. I have tried dozens of different configurations but i either get no alerts or too many.

Thanks in advance for any help

//@version=5
indicator(title="Alert TEST post ", shorttitle="Alert TEST post", overlay=true ,format=format.price , precision=4)



//Symbols
u01 = input.bool(true,  title = "", group = 'Symbols', inline = 's01')
u02 = input.bool(true,  title = "", group = 'Symbols', inline = 's02')
u03 = input.bool(true,  title = "", group = 'Symbols', inline = 's03')
u04 = input.bool(true,  title = "", group = 'Symbols', inline = 's04')
//u05 = input.bool(true,  title = "", group = 'Symbols', inline = 's05')


s01 = input.symbol('FARMBUSD',  group = 'Symbols', inline = 's01')
s02 = input.symbol('LUNCBUSD',  group = 'Symbols', inline = 's02')
s03 = input.symbol('BONDBUSD', group = 'Symbols', inline = 's03')
s04 = input.symbol('FISBUSD',  group = 'Symbols', inline = 's04') 
//s05 = input.symbol('QUICKBUSD',  group = 'Symbols', inline = 's05')



//INPUTS

macdGroup           = "=== macd ==="
//macd_len            = input.float(defval = (0), title='macd',  group=macdGroup)
macdAlert           = input.bool(true, title="Alert",  group=macdGroup, inline="1")
macdFreq            = input.string( alert.freq_once_per_bar, title="Alert Frequency", options=[alert.freq_all, alert.freq_once_per_bar, alert.freq_once_per_bar_close] ,group=macdGroup, inline="1")  
fastMAlen           = input.int(12, minval=1, title="macd fast moving average")
slowMAlen           = input.int(26, minval=1, title="macd slow moving average")
signalmacdlen       = input.int(9, minval=1, title="macd signal line moving average")
fast                = 12
slow                = 9
signal_length       = input.int(title="Signal Smoothing", minval = 1, maxval = 20, defval = 9)
sma_source          = input.string(title="Oscillator MA Type", defval="EMA", options=["SMA", "EMA"])
sma_signal          = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])
res                 = input.timeframe("",  "Indicator TimeFrame")


////////////////////////

stochGroup          = "=== Stochastic RSI ==="
//stoch_len           = input.float(title='Stochastic RSI', defval=0, group=stochGroup)
stochAlert          = input.bool(true, title="Alert", group=stochGroup, inline="1")
stochFreq           = input.string( alert.freq_once_per_bar, title="Alert Frequency", options=[alert.freq_all, alert.freq_once_per_bar, alert.freq_once_per_bar_close], group=stochGroup, inline="1")
stoch_length        = input.int(14, minval=1)
OverBought          = input(80)
OverSold            = input(20)
smoothK             = input(3)
smoothD             = input(3)


corGroup            = "=== Scanner ==="
alertFreq           = input.string( alert.freq_once_per_bar, title="Alert Frequency", options=[alert.freq_all, alert.freq_once_per_bar, alert.freq_once_per_bar_close], group=corGroup)

// Table Position
in_table_pos        = input.string(title="Location  ", defval= "Middle Left", 
     options =["Top Right",  "Middle Right",  "Bottom Right", 
              "Top Center", "Middle Center", "Bottom Center", 
              "Top Left",   "Middle Left",   "Bottom Left"], 
     group= corGroup, inline= "2")

// Table Size
in_table_size       = input.string(title= "Size", defval="Normal", 
     options=["Auto",  "Huge",  "Large", "Normal", "Small", "Tiny"], 
     group= corGroup , inline= "2")

corTable            = input.bool(true, title="", group=corGroup, inline="1")
bgCol               = input.color(color.new(color.gray,80), title="Background", group=corGroup, inline="1")
textCol             = input.color(color.white, title="Text", group=corGroup, inline="1")

plotHiddenBull      = false
plotHiddenBear      = false

// Get Table Position
table_pos(p) => 
    switch p
        "Top Right"     => position.top_right     
        "Middle Right"  => position.middle_right   
        "Bottom Right"  => position.bottom_right  
        "Top Center"    => position.top_center    
        "Middle Center" => position.middle_center
        "Bottom Center" => position.bottom_center
        "Top Left"      => position.top_left      
        "Middle Left"   => position.middle_left 
        => position.bottom_left


// Get Table Size
table_size(s) => 
    switch s
        "Auto"   => size.auto   
        "Huge"   => size.huge   
        "Large"  => size.large  
        "Normal" => size.normal 
        "Small"  => size.small
        => size.tiny
    




///////////////////////////               END OF TABLE        /////////////////////////////////




Timeframeisminutes=true

//Security requests
ignore_invalid_symbol = false

//////////////////         F GET DEFINITIONS     ////////////////////////

    /////general macd definitions
fast_length         = fastMAlen    
slow_length         = slowMAlen   
fastMA              = ta.ema(close, fast)
slowMA              = ta.ema(close, slow)
src                 = input.source(title="Source", defval=close)
macd                = slowMA - fastMA
signal              = ta.ema(macd, signalmacdlen)
hist                = macd - signal

macd_cross_Up       = ta.crossover(macd,signal)
macd_cross_Dn       = ta.crossunder(macd,signal)

macd_cross_Up       := macd[1] <= signal[1] and macd > signal
macd_cross_Dn       := macd[1] >= signal[1] and macd < signal

macd_cross_state            = macd_cross_Up ? 1 : macd_cross_Dn ? -1 : 0
macd_cross_state_5          = macd_cross_Up ? 1 : macd_cross_Dn ? -1 : 0
macd_cross_state_15         = macd_cross_Up ? 1 : macd_cross_Dn ? -1 : 0
macd_cross_state_60         = macd_cross_Up ? 1 : macd_cross_Dn ? -1 : 0
macd_cross_state_240        = macd_cross_Up ? 1 : macd_cross_Dn ? -1 : 0
macd_cross_state_720        = macd_cross_Up ? 1 : macd_cross_Dn ? -1 : 0


macd_already_up             = ((macd > signal) and  hist > hist[1]) or ((macd > macd[1]) and  hist > hist[1])
macd_already_dn             = ((macd < signal) and  hist < hist[1]) or ((macd > signal) and  hist < hist[1])

macd_trend_state            = macd_already_up ? 1 : macd_already_dn ? -1 : 0
macd_trend_state_5          = macd_already_up ? 1 : macd_already_dn ? -1 : 0
macd_trend_state_15         = macd_already_up ? 1 : macd_already_dn ? -1 : 0
macd_trend_state_60         = macd_already_up ? 1 : macd_already_dn ? -1 : 0
macd_trend_state_240        = macd_already_up ? 1 : macd_already_dn ? -1 : 0
macd_trend_state_720        = macd_already_up ? 1 : macd_already_dn ? -1 : 0


///////          General stochastic definitions
k                   = ta.sma(ta.stoch(close, high, low, stoch_length), smoothK)
d                   = ta.sma(k, smoothD)

stochUp             = k[1] >= d[1] and k > d
stochDn             = k[1] <= d[1] and k < d
stoch_cross_Up      = ta.crossover(k,d)
stoch_cross_Dn      = ta.crossunder(k,d)

stoch_already_up            = (k > d) and  (k > k[1])
stoch_already_dn            = ((k < d) and k < k[1]) or ((k > d) and  k < k[1])

stoch_trend_state           = stoch_already_up  ? 1 : stoch_already_dn ? -1 : 0
stoch_trend_state_5         = stoch_already_up  ? 1 : stoch_already_dn ? -1 : 0
stoch_trend_state_15        = stoch_already_up  ? 1 : stoch_already_dn ? -1 : 0
stoch_trend_state_60        = stoch_already_up  ? 1 : stoch_already_dn ? -1 : 0
stoch_trend_state_240       = stoch_already_up  ? 1 : stoch_already_dn ? -1 : 0
stoch_trend_state_720       = stoch_already_up  ? 1 : stoch_already_dn ? -1 : 0

stoch_cross_state           = stoch_cross_Up ? 1 : stoch_cross_Dn ? -1 : 0
stoch_cross_state_5         = stoch_cross_Up ? 1 : stoch_cross_Dn ? -1 : 0
stoch_cross_state_15        = stoch_cross_Up ? 1 : stoch_cross_Dn ? -1 : 0
stoch_cross_state_60        = stoch_cross_Up ? 1 : stoch_cross_Dn ? -1 : 0
stoch_cross_state_240       = stoch_cross_Up ? 1 : stoch_cross_Dn ? -1 : 0
stoch_cross_state_720       = stoch_cross_Up ? 1 : stoch_cross_Dn ? -1 : 0
 
 ////////////////////////        ROC   Price change

 
roc_price_5                  = ta.roc(close, 1 ) 
price_roc_5_up_2           = roc_price_5 >= 0.02
roc_price_60                  = ta.roc(close, 1 ) 
price_roc_60_up_2           = roc_price_60 >= 0.02

///////////////////////////////  F GET SETUP    ///////////////////////////

///////////////////        GET 5   ///////////////////

f_get_five_macd_cross_state() => [macd_cross_state_5] 
f_get_five_macd_trend_state() => [macd_trend_state_5]
f_get_five_stoch_cross_state() => [stoch_cross_state_5] 
f_get_five_stoch_trend_state() => [stoch_trend_state_5]                 
f_get_five_price_roc() => [roc_price_5]

f_get_five_all() =>
    f_get_five_macd_cross_state()
    f_get_five_macd_trend_state()
    f_get_five_stoch_cross_state()
    f_get_five_stoch_trend_state() 
    f_get_five_price_roc()
 
request.security(syminfo.tickerid, "5", f_get_five_all(), barmerge.gaps_off,  barmerge.lookahead_on)

///////////////////        GET 15   ///////////////////

f_get_fifteen_macd_cross_state() => [macd_cross_state_15] 
f_get_fifteen_macd_trend_state() => [macd_trend_state_15]
f_get_fifteen_stoch_cross_state() => [stoch_cross_state_15] 
f_get_fifteen_stoch_trend_state() => [stoch_trend_state_15]

f_get_fifteen_all() =>
    f_get_fifteen_macd_cross_state() 
    f_get_fifteen_macd_trend_state()
    f_get_fifteen_stoch_cross_state()
    f_get_fifteen_stoch_trend_state() 
request.security(syminfo.tickerid, "15", f_get_fifteen_all(), barmerge.gaps_off,  barmerge.lookahead_on)

///////////////////        GET 60   ///////////////////

f_get_sixty_macd_cross_state() => [macd_cross_state_60] 
f_get_sixty_macd_trend_state() => [macd_trend_state_60]
f_get_sixty_stoch_cross_state() => [stoch_cross_state_60] 
f_get_sixty_stoch_trend_state() => [stoch_trend_state_60]
f_get_sixty_price_roc() => [roc_price_60]

f_get_sixty_all() =>
    f_get_sixty_macd_cross_state()
    f_get_sixty_macd_trend_state() 
    f_get_sixty_stoch_cross_state() 
    f_get_sixty_stoch_trend_state()
    f_get_sixty_price_roc() 
request.security(syminfo.tickerid, "60", f_get_sixty_all(), barmerge.gaps_off,  barmerge.lookahead_on)

///////////////////        GET 240   ///////////////////

f_get_twoforty_macd_cross_state() => [macd_cross_state_240] 
f_get_twoforty_macd_trend_state() => [macd_trend_state_240]
f_get_twoforty_stoch_cross_state() => [stoch_cross_state_240] 
f_get_twoforty_stoch_trend_state() => [stoch_trend_state_240]

f_get_twoforty_all() =>
    f_get_twoforty_macd_cross_state()
    f_get_twoforty_macd_trend_state()
    f_get_twoforty_stoch_cross_state()  
    f_get_twoforty_stoch_trend_state()

request.security(syminfo.tickerid, "240", f_get_twoforty_all(), barmerge.gaps_off,  barmerge.lookahead_on)

///////////////////        GET 720   ///////////////////

f_get_seventwenty_macd_cross_state() => [macd_cross_state_720] 
f_get_seventwenty_macd_trend_state() => [macd_trend_state_720]
f_get_seventwenty_stoch_cross_state() => [stoch_cross_state_720] 
f_get_seventwenty_stoch_trend_state() => [stoch_trend_state_720]

f_get_seventwenty_all() =>
    f_get_seventwenty_macd_cross_state()
    f_get_seventwenty_macd_trend_state()
    f_get_seventwenty_stoch_cross_state() 
    f_get_seventwenty_stoch_trend_state()
request.security(syminfo.tickerid, "720", f_get_seventwenty_all(), barmerge.gaps_off,  barmerge.lookahead_on)

///////////////////        GET ALL  ///////////////////

f_get_all_timeframes() =>
    [all5] = f_get_five_all()
    [all15] = f_get_fifteen_all()
    [all60] = f_get_sixty_all()
    [all120] = f_get_twoforty_all()
    [all720] = f_get_seventwenty_all() 

request.security(syminfo.tickerid, "", f_get_all_timeframes(), barmerge.gaps_off,  barmerge.lookahead_on) 


//////////////////////         ARRAYS

only_symbol(s) => 
    array.get(str.split(s, ":"), 1)

// // ARRAYS //

s_arr           = array.new_string(0)
u_arr           = array.new_bool(0)

ema_arr         = array.new_int(0)

// Add Symbols 
array.push(s_arr, only_symbol(s01))
array.push(s_arr, only_symbol(s02))
array.push(s_arr, only_symbol(s03))
array.push(s_arr, only_symbol(s04))
//array.push(s_arr, only_symbol(s05))


// FLAGS //
array.push(u_arr, u01)
array.push(u_arr, u02)
array.push(u_arr, u03)
array.push(u_arr, u04)
//array.push(u_arr, u05)



/////// //////////////////////////    LONG SHORT CRITERIA            ---------   BE SURE TO ADD SCREENER FUNCTION LISTS BELOW



plotshape(macd_cross_Up,  title =  "BUY",  style = shape.triangledown, location= location.top, color =color.lime, size = size.tiny)



////////////////           ALERT SETUP      ///////////////////////////////////////

//screener_func(_ticker) =>
for i = 0 to 3
    if array.get(u_arr, i)
        if  array.get(u_arr, i) and (macd_cross_Up)
            message_test = 'BUY' + str.tostring(array.get(s_arr,i)) + '   5 min test'  
            alert(message_test, alert.freq_all)

different configurations

Juz
  • 17
  • 3

1 Answers1

0

There are multiple problems here.

f_get_all_timeframes() will only return whatever is in the last line.

f_get_all_timeframes() =>
    [all5] = f_get_five_all()
    [all15] = f_get_fifteen_all()
    [all60] = f_get_sixty_all()
    [all120] = f_get_twoforty_all()
    [all720] = f_get_seventwenty_all()

If you want to return multiple values, you need to return a tuple.

f_get_all_timeframes() =>
    all5 = f_get_five_all()
    all15 = f_get_fifteen_all()
    all60 = f_get_sixty_all()
    all120 = f_get_twoforty_all()
    all720 = f_get_seventwenty_all()
    [all5, all15, all60, all120, all720]

Calling f_get_all_timeframes() in a security() call does not make any sense to me. With f_get_all_timeframes() you already get all the values from other timeframes.

request.security(syminfo.tickerid, "", f_get_all_timeframes(), barmerge.gaps_off,  barmerge.lookahead_on) 

Now, coming to your main question.

Your alert condition is below:

array.get(u_arr, i) and (macd_cross_Up)

In order to trigger an alert, array.get(u_arr, i) and macd_cross_Up must be true.

You push, u01, u02, u03, u04 to u_arr array. Those values are input.bools. Their values never change during execution.

macd_cross_Up is a bool variable that only works on your chart's timeframe.

None of those values in your alert condition have anything to do with other timeframes or other symbols.

vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • Thanks for the help @vitrvius, i have just started coding and knew that i probably made a mess of it. Is there a way i can set it up so that it gets all the variables i want from different symbols and sends an alert just on that one, or do i have to set up the alert on every symbol individually. Im trying to limit the amount of alerts i have to manage. Thanks again for the help – Juz Nov 13 '22 at 08:29
  • Im trying to set it up with as many symbols on the one alert as possible so that if i want to change the alert variables i wont have to go through hundreds of alerts and set them up again. Probably an impossible mission but thought i would give it a try – Juz Nov 13 '22 at 08:43
  • This is how i had it set up thinking i was getting the variables from other timeframes. I changed the alert just to try and sort out the multiple trigger issue. Forgot to add it back into the code 'code' Long_5mto4HR = macd_cross_state_5 == 1 and (stoch_trend_state_15 ==1 or macd_cross_state_15 == 1 or macd_trend_state_15 == 1) and (macd_cross_state_60 == 1 or macd_trend_state_60 == 1 or stoch_trend_state_60 == 1) BS_5mto1HR = Long_5mto1HR ? 1 : 0 'code' – Juz Nov 13 '22 at 08:58
  • And the alert set up this way, its wrong but just thought it might help showing what i am trying to achieve for i = 0 to 25 if array.get(u_arr, i) if array.get(u_arr, i) and BS_5mto4HR ==1 message_5to4hr = str.tostring(array.get(s_arr,i)) + ' Short Buy 5m to 4 HR' alert(message_5to4hr, alert.freq_all) – Juz Nov 13 '22 at 08:59
  • There are many issues in this code. I suggest you focus on one timeframe and one symbol first, then move on to multiple timeframes/symbols. – vitruvius Nov 13 '22 at 11:54