chooka
Pine Script Master
Pine Script Master
Posts: 18
Joined: February 7th, 2022

Display of Labels & Create Alerts from higher timeframe closes

Hi,

First time poster here, but thought id give it a crack.

Has anyone had any experience with creating labels &/or alerts via higher timeframe bar closes? Using the security function, id like a 1hr created alert to not only alert on the current timeframe, but also on the 4hr & 1d should their conditions be true at bar close only on those timeframes.

I have the script running with the security clauses correctly, and I can prevent a series of labels from printing by not allowing any to print if they were in the bar previous.

Alerts however, they are generating every bar close on the 1h, where i only want the 4H & 1D to alert on close of their own respective bars (4h closes & 1d closes), but from the 1h alert.

Thought about using a for loop, and using the multiplier of each (4 & 24) in this, but thought there might be a more elegant solution.

Note - this is effectively for overloading the alert system of TV, and being able to squeeze more timeframes into the one alert. I cant see why it couldn't be done and will continue on, just thought id reach out in case anyone has had experience with this before and can suggest a nice solution.

Many thanks
:heart:

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Display of Labels & Create Alerts from higher timeframe closes

Hey

I am not really sure, what you are getting the error with as there is no reference source code provided. But what i understand is that you want to get a value from Higher Time Frame and than display it as a label on that same exact time on the lower time frame.

I am attaching a quick sample code that does this.

You can change the time frame in settings. This just gets the ema of higher time frame and prints a label on each new tick on higher time frame.

I use ta.change(time(resolution_input)) to mark the current bar at each new higher time change. We than use IF logic to check if this is true, than to print a new label


Code: Select all

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © processingclouds

//@version=5
indicator("My script", overlay=true)

resolution_input = input.timeframe("30", title="Timeframe", group = "General")
isNewPeriod = ta.change(time(resolution_input))

higherEma = request.security(syminfo.ticker, resolution_input, ta.ema(close,10))

if isNewPeriod 
    label.new(bar_index,high,style=label.style_none,text= "HERE\n"+str.tostring(higherEma),color=color.white)


chooka
Pine Script Master
Pine Script Master
Posts: 18
Joined: February 7th, 2022

Re: Display of Labels & Create Alerts from higher timeframe closes

you absolute legend! that worked a treat!!

Thank you!!!

chooka
Pine Script Master
Pine Script Master
Posts: 18
Joined: February 7th, 2022

Re: Display of Labels & Create Alerts from higher timeframe closes

Hi, As an extension to this - i've found a gap with what I am trying to do with generating alerts on bar close. The label display component works flawlessly currently.

Id also like to alert only if the higher timeframe candle is confirmed - and is not real time, otherwise, currently the script currently generates alerts during the real-time higher timeframe bar, because the current timeframe bar confirms.

Essentially, when creating these from the lower timeframe charts, if I use the barstate.isconfirmed variable, this is only based on the current timeframe, and not the higher timeframe candle.
ie. If my current timeframe is 5m, and I want to set an alert off on bar close of the 1h.

https://www.tradingview.com/pine-script ... state.html
There is a comment at the bottom of this page that states that:
Variable barstate.isconfirmed returns the state of current chart symbol data only. It does not take into account any secondary symbol data requested with the security function.

Is there a way to get this to work? I have tried all day today to come up with a series of different functions, counters, multipliers etc but havent gotten anywhere unfortunately.

Thanks again for your assistance

chooka
Pine Script Master
Pine Script Master
Posts: 18
Joined: February 7th, 2022

Re: Display of Labels & Create Alerts from higher timeframe closes

Thought i should add some code of what i am trying to do... hope this makes sense

Code: Select all

enable_tf1 = true
enable_tf2 = true
alert_tf1 = true
alert_tf2 = true


timeframe1 = "60"
timeframe2 = "240"
lookback = 90
lengthRSI = 14


tf1_isNewPeriod = ta.change(time(timeframe1)) 
tf2_isNewPeriod = ta.change(time(timeframe2))

tf1_multiplier = calculateTimeDividedBy(timeframe1) / calculateTimeDividedBy(timeframe.period)
tf2_multiplier = calculateTimeDividedBy(timeframe2) / calculateTimeDividedBy(timeframe.period)


rsi = ta.rsi(close, lengthRSI)

/////////////////////////////////////
// Find Divergence 
///////////////////////////////////

find_divergence(price_close, oscillator, lookback) =>
    highest_bar = math.abs(ta.highestbars(oscillator, lookback))  // Finds bar with highest value in last X bars
    lowest_bar = math.abs(ta.lowestbars(oscillator, lookback))  // Finds bar with lowest value in last X bars

    // Defining variable
    max_price = float(na)
    max_osc = float(na)
    min_price = float(na)
    min_osc = float(na)
    pivoth = bool(na)
    pivotl = bool(na)
    divbear = bool(na)
    divbull = bool(na)

    // If bar with lowest / highest is current bar, use it's value
    max_price := highest_bar == 0 ? price_close : na(max_price[1]) ? price_close : max_price[1]
    max_osc := highest_bar == 0 ? oscillator : na(max_osc[1]) ? oscillator : max_osc[1]
    min_price := lowest_bar == 0 ? price_close : na(min_price[1]) ? price_close : min_price[1]
    min_osc := lowest_bar == 0 ? oscillator : na(min_osc[1]) ? oscillator : min_osc[1]


    // Compare high of current bar being examined with previous bar's high
    // If curr bar high is higher than the max bar high in the lookback window range
    if price_close > max_price  // we have a new high
        max_price := price_close  // change variable "max" to use current bar's high value
        max_price
    if oscillator > max_osc  // we have a new high
        max_osc := oscillator  // change variable "max_rsi" to use current bar's RSI value
        max_osc
    if price_close < min_price  // we have a new low
        min_price := price_close  // change variable "min" to use current bar's low value
        min_price
    if oscillator < min_osc  // we have a new low
        min_osc := oscillator  // change variable "min_rsi" to use current bar's RSI value
        min_osc


    // Finds pivot point with at least 2 right candles with lower value
    pivoth := max_osc == max_osc[2] and max_osc[2] != max_osc[3] ? true : na
    pivotl := min_osc == min_osc[2] and min_osc[2] != min_osc[3] ? true : na

    // Detects divergences between price and indicator with 1 candle delay so it filters out repeating divergences
    if max_price[1] > max_price[2] and oscillator[1] < max_osc and oscillator <= oscillator[1]
        divbear := true
        divbear
    if min_price[1] < min_price[2] and oscillator[1] > min_osc and oscillator >= oscillator[1]
        divbull := true
        divbull
    [divbull, divbear]


[tf1_divbull, tf1_divbear] = request.security(syminfo.ticker, timeframe1, find_divergence(close[barstate.isconfirmed ? 0 : 1], rsi, lookback))
[tf2_divbull, tf2_divbear] = request.security(syminfo.ticker, timeframe2, find_divergence(close[barstate.isconfirmed ? 0 : 1], rsi, lookback))


////////////////////////////
// Draw Labels 
/////////////////////////

if enable_tf1 and tf1_isNewPeriod
    tf1_divergence = and tf1_divbear ? label.new(bar_index - 1 * tf1_multiplier, 90, '3', color=barstate.islast and not barstate.isconfirmed ? color.new(tf1_col, 80) : tf1_col, textcolor=color.black, style=label.style_label_down, yloc=yloc.price, size=size.normal) : tf1_divbull, ? label.new(bar_index - 1 * tf1_multiplier, 10, '3', color=barstate.islast and not barstate.isconfirmed ? color.new(tf1_col, 80) : tf1_col, textcolor=color.black, style=label.style_label_up, yloc=yloc.price, size=size.normal) : na

if enable_tf2 and tf2_isNewPeriod
    tf2_divergence = and tf2_divbear ? label.new(bar_index - 1 * tf2_multiplier, 90, '3', color=barstate.islast and not barstate.isconfirmed ? color.new(tf2_col, 80) : tf2_col, textcolor=color.black, style=label.style_label_down, yloc=yloc.price, size=size.normal) : tf2_divbull, ? label.new(bar_index - 1 * tf2_multiplier, 10, '3', color=barstate.islast and not barstate.isconfirmed ? color.new(tf2_col, 80) : tf2_col, textcolor=color.black, style=label.style_label_up, yloc=yloc.price, size=size.normal) : na


//////////////////////////
// Create Alerts
///////////////////////

if alert_tf1 and tf1_isNewPeriod and barstate.isconfirmed
    if  tf1_divbull
        alert(syminfo.ticker + ' Bullish Divergence - Price: ' + str.tostring(close) + ' - Timeframe: ' + timeframe1, alert.freq_once_per_bar_close)
    if tf1_divbear
        alert(syminfo.ticker + ' Bearish Divergence - Price: ' + str.tostring(close) + ' - Timeframe: ' + timeframe1, alert.freq_once_per_bar_close)

if alert_tf2 and tf2_isNewPeriod and barstate.isconfirmed
    if  tf2_divbull
        alert(syminfo.ticker + ' Bullish Divergence - Price: ' + str.tostring(close) + ' - Timeframe: ' + timeframe2, alert.freq_once_per_bar_close)
    if tf2_divbear
        alert(syminfo.ticker + ' Bearish Divergence - Price: ' + str.tostring(close) + ' - Timeframe: ' + timeframe2, alert.freq_once_per_bar_close)
        
        

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Display of Labels & Create Alerts from higher timeframe closes

Your code seems to be not working, either you have removed few functions and variables.
e.g. calculateTimeDividedBy , you use this in tf1_multiplier but i do not see this function in the code
tf1_col tf2_col are not defined
Also your label drawings lines have syntax error: 1. they start with " = and " , you cannot start with and
2. your second nested if , " tf1_divbull, ? " you have put an unwanted comma before ? which will give error

If you can send code that is working, i might be able to better understand how it displays .


Also you can use close[barstate.isrealtime ? 1 : 0]
as only barsrtate.isconfirmed is mentioned as not supported by security function

chooka
Pine Script Master
Pine Script Master
Posts: 18
Joined: February 7th, 2022

Re: Display of Labels & Create Alerts from higher timeframe closes

Hi Processingclouds,

Ah sorry, my apologies, I only included some code snippets to illustrate what I was trying to do rather than the whole code block - which is why it wouldn't compile. I have included a full compiled block below.

Code: Select all



//@version=5
indicator("My script")


enable_tf1 = true
enable_tf2 = true
alert_tf1 = true
alert_tf2 = true

tf1_col = color.blue
tf2_col = color.purple


timeframe1 = "60"
timeframe2 = "240"
lookback = 90
lengthRSI = 14


// ---------------------------------------------------------------------------------------------- //
cutLastDigit(str) =>
    s = str + ';'
    r = str.replace_all(s, '1;', '')
    if r != s
        [r, 1]
    else
        r := str.replace_all(s, '2;', '')
        if r != s
            [r, 2]
        else
            r := str.replace_all(s, '3;', '')
            if r != s
                [r, 3]
            else
                r := str.replace_all(s, '4;', '')
                if r != s
                    [r, 4]
                else
                    r := str.replace_all(s, '5;', '')
                    if r != s
                        [r, 5]
                    else
                        r := str.replace_all(s, '6;', '')
                        if r != s
                            [r, 6]
                        else
                            r := str.replace_all(s, '7;', '')
                            if r != s
                                [r, 7]
                            else
                                r := str.replace_all(s, '8;', '')
                                if r != s
                                    [r, 8]
                                else
                                    r := str.replace_all(s, '9;', '')
                                    if r != s
                                        [r, 9]
                                    else
                                        r := str.replace_all(s, '0;', '')
                                        if r != s
                                            [r, 0]

strToNumInt(str) =>
    integer = 0  // integer part of the number
    s_new = str
    position = 0.0  // handled position of the number.
    sign = 1  // sign of the number. 1.0 is PLUS and -1.0 is MINUS.
    for i = 0 to 30 by 1
        [s, digit] = cutLastDigit(s_new)  // here we'll cut the digits by one from the string till the string is empty.
        integer += digit * int(math.pow(10, position))  // it's just a regular digit.
        position += 1
        if s == ''
            break
        s_new := s  // If we are here, then there are more digits in the string. Let's handle the next one!
        s_new
    sign * integer  // We've exited from the loop. Build the returning value.

calculateTimeDividedBy(res) =>
    timeDividiedBy = time(timeframe.period)
    if strToNumInt(res) < 1440
        timeDividiedBy := strToNumInt(res) * 60 * 1000
        timeDividiedBy
    for i = 1 to 50 by 1
        if res == str.tostring(i) + 'D' or res == 'D'
            timeDividiedBy := i * 1 * 86400000
            break
        else if res == str.tostring(i) + 'W' or res == 'W'
            timeDividiedBy := i * 7 * 86400000
            break
        else if res == str.tostring(i) + 'M' or res == 'M'
            timeDividiedBy := i * 30 * 86400000
            break
    timeDividiedBy



tf1_isNewPeriod = ta.change(time(timeframe1)) 
tf2_isNewPeriod = ta.change(time(timeframe2))

tf1_multiplier = calculateTimeDividedBy(timeframe1) / calculateTimeDividedBy(timeframe.period)
tf2_multiplier = calculateTimeDividedBy(timeframe2) / calculateTimeDividedBy(timeframe.period)


rsi = ta.rsi(close, lengthRSI)

/////////////////////////////////////
// Find Divergence 
///////////////////////////////////

find_divergence(price_close, oscillator, lookback) =>
    highest_bar = math.abs(ta.highestbars(oscillator, lookback))  // Finds bar with highest value in last X bars
    lowest_bar = math.abs(ta.lowestbars(oscillator, lookback))  // Finds bar with lowest value in last X bars

    // Defining variable
    max_price = float(na)
    max_osc = float(na)
    min_price = float(na)
    min_osc = float(na)
    pivoth = bool(na)
    pivotl = bool(na)
    divbear = bool(na)
    divbull = bool(na)

    // If bar with lowest / highest is current bar, use it's value
    max_price := highest_bar == 0 ? price_close : na(max_price[1]) ? price_close : max_price[1]
    max_osc := highest_bar == 0 ? oscillator : na(max_osc[1]) ? oscillator : max_osc[1]
    min_price := lowest_bar == 0 ? price_close : na(min_price[1]) ? price_close : min_price[1]
    min_osc := lowest_bar == 0 ? oscillator : na(min_osc[1]) ? oscillator : min_osc[1]


    // Compare high of current bar being examined with previous bar's high
    // If curr bar high is higher than the max bar high in the lookback window range
    if price_close > max_price  // we have a new high
        max_price := price_close  // change variable "max" to use current bar's high value
        max_price
    if oscillator > max_osc  // we have a new high
        max_osc := oscillator  // change variable "max_rsi" to use current bar's RSI value
        max_osc
    if price_close < min_price  // we have a new low
        min_price := price_close  // change variable "min" to use current bar's low value
        min_price
    if oscillator < min_osc  // we have a new low
        min_osc := oscillator  // change variable "min_rsi" to use current bar's RSI value
        min_osc


    // Finds pivot point with at least 2 right candles with lower value
    pivoth := max_osc == max_osc[2] and max_osc[2] != max_osc[3] ? true : na
    pivotl := min_osc == min_osc[2] and min_osc[2] != min_osc[3] ? true : na

    // Detects divergences between price and indicator with 1 candle delay so it filters out repeating divergences
    if max_price[1] > max_price[2] and oscillator[1] < max_osc and oscillator <= oscillator[1]
        divbear := true
        divbear
    if min_price[1] < min_price[2] and oscillator[1] > min_osc and oscillator >= oscillator[1]
        divbull := true
        divbull
    [divbull, divbear]


[tf1_divbull, tf1_divbear] = request.security(syminfo.ticker, timeframe1, find_divergence(close[barstate.isrealtime ? 1 : 0], rsi, lookback))
[tf2_divbull, tf2_divbear] = request.security(syminfo.ticker, timeframe2, find_divergence(close[barstate.isrealtime ? 1 : 0], rsi, lookback))


////////////////////////////
// Draw Labels 
/////////////////////////

if enable_tf1 and tf1_isNewPeriod
    tf1_divergence = tf1_divbear ? label.new(bar_index - 1 * tf1_multiplier, 90, '3', color=barstate.islast and not barstate.isconfirmed ? color.new(tf1_col, 80) : tf1_col, textcolor=color.black, style=label.style_label_down, yloc=yloc.price, size=size.normal) : tf1_divbull ? label.new(bar_index - 1 * tf1_multiplier, 10, '3', color=barstate.islast and not barstate.isconfirmed ? color.new(tf1_col, 80) : tf1_col, textcolor=color.black, style=label.style_label_up, yloc=yloc.price, size=size.normal) : na

if enable_tf2 and tf2_isNewPeriod
    tf2_divergence = tf2_divbear ? label.new(bar_index - 1 * tf2_multiplier, 90, '3', color=barstate.islast and not barstate.isconfirmed ? color.new(tf2_col, 80) : tf2_col, textcolor=color.black, style=label.style_label_down, yloc=yloc.price, size=size.normal) : tf2_divbull ? label.new(bar_index - 1 * tf2_multiplier, 10, '3', color=barstate.islast and not barstate.isconfirmed ? color.new(tf2_col, 80) : tf2_col, textcolor=color.black, style=label.style_label_up, yloc=yloc.price, size=size.normal) : na


//////////////////////////
// Create Alerts
///////////////////////

if alert_tf1 and tf1_isNewPeriod and barstate.isconfirmed
    if  tf1_divbull
        alert(syminfo.ticker + ' Bullish Divergence - Price: ' + str.tostring(close) + ' - Timeframe: ' + timeframe1, alert.freq_once_per_bar_close)
    if tf1_divbear
        alert(syminfo.ticker + ' Bearish Divergence - Price: ' + str.tostring(close) + ' - Timeframe: ' + timeframe1, alert.freq_once_per_bar_close)

if alert_tf2 and tf2_isNewPeriod and barstate.isconfirmed
    if  tf2_divbull
        alert(syminfo.ticker + ' Bullish Divergence - Price: ' + str.tostring(close) + ' - Timeframe: ' + timeframe2, alert.freq_once_per_bar_close)
    if tf2_divbear
        alert(syminfo.ticker + ' Bearish Divergence - Price: ' + str.tostring(close) + ' - Timeframe: ' + timeframe2, alert.freq_once_per_bar_close)

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Display of Labels & Create Alerts from higher timeframe closes

Hey,
Does changing all references from barstate.isconfirmed to isrealtime not fix your issue ?

Code: Select all



//@version=5
indicator("My script")


enable_tf1 = true
enable_tf2 = true
alert_tf1 = true
alert_tf2 = true

tf1_col = color.blue
tf2_col = color.purple


timeframe1 = "60"
timeframe2 = "240"
lookback = 90
lengthRSI = 14


// ---------------------------------------------------------------------------------------------- //
cutLastDigit(str) =>
    s = str + ';'
    r = str.replace_all(s, '1;', '')
    if r != s
        [r, 1]
    else
        r := str.replace_all(s, '2;', '')
        if r != s
            [r, 2]
        else
            r := str.replace_all(s, '3;', '')
            if r != s
                [r, 3]
            else
                r := str.replace_all(s, '4;', '')
                if r != s
                    [r, 4]
                else
                    r := str.replace_all(s, '5;', '')
                    if r != s
                        [r, 5]
                    else
                        r := str.replace_all(s, '6;', '')
                        if r != s
                            [r, 6]
                        else
                            r := str.replace_all(s, '7;', '')
                            if r != s
                                [r, 7]
                            else
                                r := str.replace_all(s, '8;', '')
                                if r != s
                                    [r, 8]
                                else
                                    r := str.replace_all(s, '9;', '')
                                    if r != s
                                        [r, 9]
                                    else
                                        r := str.replace_all(s, '0;', '')
                                        if r != s
                                            [r, 0]

strToNumInt(str) =>
    integer = 0  // integer part of the number
    s_new = str
    position = 0.0  // handled position of the number.
    sign = 1  // sign of the number. 1.0 is PLUS and -1.0 is MINUS.
    for i = 0 to 30 by 1
        [s, digit] = cutLastDigit(s_new)  // here we'll cut the digits by one from the string till the string is empty.
        integer += digit * int(math.pow(10, position))  // it's just a regular digit.
        position += 1
        if s == ''
            break
        s_new := s  // If we are here, then there are more digits in the string. Let's handle the next one!
        s_new
    sign * integer  // We've exited from the loop. Build the returning value.

calculateTimeDividedBy(res) =>
    timeDividiedBy = time(timeframe.period)
    if strToNumInt(res) < 1440
        timeDividiedBy := strToNumInt(res) * 60 * 1000
        timeDividiedBy
    for i = 1 to 50 by 1
        if res == str.tostring(i) + 'D' or res == 'D'
            timeDividiedBy := i * 1 * 86400000
            break
        else if res == str.tostring(i) + 'W' or res == 'W'
            timeDividiedBy := i * 7 * 86400000
            break
        else if res == str.tostring(i) + 'M' or res == 'M'
            timeDividiedBy := i * 30 * 86400000
            break
    timeDividiedBy



tf1_isNewPeriod = ta.change(time(timeframe1)) 
tf2_isNewPeriod = ta.change(time(timeframe2))

tf1_multiplier = calculateTimeDividedBy(timeframe1) / calculateTimeDividedBy(timeframe.period)
tf2_multiplier = calculateTimeDividedBy(timeframe2) / calculateTimeDividedBy(timeframe.period)


rsi = ta.rsi(close, lengthRSI)

/////////////////////////////////////
// Find Divergence 
///////////////////////////////////

find_divergence(price_close, oscillator, lookback) =>
    highest_bar = math.abs(ta.highestbars(oscillator, lookback))  // Finds bar with highest value in last X bars
    lowest_bar = math.abs(ta.lowestbars(oscillator, lookback))  // Finds bar with lowest value in last X bars

    // Defining variable
    max_price = float(na)
    max_osc = float(na)
    min_price = float(na)
    min_osc = float(na)
    pivoth = bool(na)
    pivotl = bool(na)
    divbear = bool(na)
    divbull = bool(na)

    // If bar with lowest / highest is current bar, use it's value
    max_price := highest_bar == 0 ? price_close : na(max_price[1]) ? price_close : max_price[1]
    max_osc := highest_bar == 0 ? oscillator : na(max_osc[1]) ? oscillator : max_osc[1]
    min_price := lowest_bar == 0 ? price_close : na(min_price[1]) ? price_close : min_price[1]
    min_osc := lowest_bar == 0 ? oscillator : na(min_osc[1]) ? oscillator : min_osc[1]


    // Compare high of current bar being examined with previous bar's high
    // If curr bar high is higher than the max bar high in the lookback window range
    if price_close > max_price  // we have a new high
        max_price := price_close  // change variable "max" to use current bar's high value
        max_price
    if oscillator > max_osc  // we have a new high
        max_osc := oscillator  // change variable "max_rsi" to use current bar's RSI value
        max_osc
    if price_close < min_price  // we have a new low
        min_price := price_close  // change variable "min" to use current bar's low value
        min_price
    if oscillator < min_osc  // we have a new low
        min_osc := oscillator  // change variable "min_rsi" to use current bar's RSI value
        min_osc


    // Finds pivot point with at least 2 right candles with lower value
    pivoth := max_osc == max_osc[2] and max_osc[2] != max_osc[3] ? true : na
    pivotl := min_osc == min_osc[2] and min_osc[2] != min_osc[3] ? true : na

    // Detects divergences between price and indicator with 1 candle delay so it filters out repeating divergences
    if max_price[1] > max_price[2] and oscillator[1] < max_osc and oscillator <= oscillator[1]
        divbear := true
        divbear
    if min_price[1] < min_price[2] and oscillator[1] > min_osc and oscillator >= oscillator[1]
        divbull := true
        divbull
    [divbull, divbear]


[tf1_divbull, tf1_divbear] = request.security(syminfo.ticker, timeframe1, find_divergence(close[barstate.isrealtime ? 1 : 0], rsi, lookback))
[tf2_divbull, tf2_divbear] = request.security(syminfo.ticker, timeframe2, find_divergence(close[barstate.isrealtime ? 1 : 0], rsi, lookback))


////////////////////////////
// Draw Labels 
/////////////////////////

if enable_tf1 and tf1_isNewPeriod
    tf1_divergence = tf1_divbear ? label.new(bar_index - 1 * tf1_multiplier, 90, '3', color=barstate.islast and barstate.isrealtime ? color.new(tf1_col, 80) : tf1_col, textcolor=color.black, style=label.style_label_down, yloc=yloc.price, size=size.normal) : tf1_divbull ? label.new(bar_index - 1 * tf1_multiplier, 10, '3', color=barstate.islast and barstate.isrealtime ? color.new(tf1_col, 80) : tf1_col, textcolor=color.black, style=label.style_label_up, yloc=yloc.price, size=size.normal) : na

if enable_tf2 and tf2_isNewPeriod
    tf2_divergence = tf2_divbear ? label.new(bar_index - 1 * tf2_multiplier, 90, '3', color=barstate.islast and barstate.isrealtime ? color.new(tf2_col, 80) : tf2_col, textcolor=color.black, style=label.style_label_down, yloc=yloc.price, size=size.normal) : tf2_divbull ? label.new(bar_index - 1 * tf2_multiplier, 10, '3', color=barstate.islast and barstate.isrealtime ? color.new(tf2_col, 80) : tf2_col, textcolor=color.black, style=label.style_label_up, yloc=yloc.price, size=size.normal) : na


//////////////////////////
// Create Alerts
///////////////////////

if alert_tf1 and tf1_isNewPeriod and not barstate.isrealtime
    if  tf1_divbull
        alert(syminfo.ticker + ' Bullish Divergence - Price: ' + str.tostring(close) + ' - Timeframe: ' + timeframe1, alert.freq_once_per_bar_close)
    if tf1_divbear
        alert(syminfo.ticker + ' Bearish Divergence - Price: ' + str.tostring(close) + ' - Timeframe: ' + timeframe1, alert.freq_once_per_bar_close)

if alert_tf2 and tf2_isNewPeriod and not barstate.isrealtime
    if  tf2_divbull
        alert(syminfo.ticker + ' Bullish Divergence - Price: ' + str.tostring(close) + ' - Timeframe: ' + timeframe2, alert.freq_once_per_bar_close)
    if tf2_divbear
        alert(syminfo.ticker + ' Bearish Divergence - Price: ' + str.tostring(close) + ' - Timeframe: ' + timeframe2, alert.freq_once_per_bar_close)

chooka
Pine Script Master
Pine Script Master
Posts: 18
Joined: February 7th, 2022

Re: Display of Labels & Create Alerts from higher timeframe closes

Hi, after just testing it again this morning on low timeframes (5m & 15m) - the labels work well, however the alerts are still firing too early.

I want the alert to fire upon the higher timeframe close, what it is doing is firing on the current timeframe close (once the divergence is found on the higher timeframe, but not locked in/closed).

ie. I set my alert on 5 min, and i want it to alert me for 15m & 30m divergence from the 5m chart, once the 15m and 30m closes happen.

What it is doing is seeing a possible divergence (but its not locked in until the higher timeframe closes), and it is firing an alert 5mins after it sees it, rather than waiting until the higher timeframe closes, ie 15m close or 30m close.

15m chart shows that a divergence is about to play (see how its transparent), but its not locked in
https://www.tradingview.com/x/7mPLeDLh/

5m chart doesnt show this yet, as its not locked in
https://www.tradingview.com/x/VzciV9EZ/

alert was fired already at 11:20am (11:15 seen, but not locked in + 5min), but should have waited until 11:30am candle close (15m) (if the divergence is still there)

Does this make sense? Maybe i can better explain it better with images?

chooka
Pine Script Master
Pine Script Master
Posts: 18
Joined: February 7th, 2022

Re: Display of Labels & Create Alerts from higher timeframe closes

Ah sorry - I think it might be working. Its firing 5mins after the close, not on the close.

If i change the alert to once_per_bar instead of once_per_bar_close hopefully that fixes it.

Ill continue to test!

Apologies.

Return to “Pine Script Q&A”