Página 1 de 1

Añadir alerta a indicador Trading View

Publicado: 22 Mar 2022 15:45
por Albertius
Hola, soy nuevo en el foro. Lo primero mandar saludos a todos y dar las gracias por adelantado a aquellos que atiendan mi consulta. Soy usuario principiante de Trading View, poco a poco estoy aprendiendo sobre Pine pero me cuesta mucho ya que no tengo formación sobre programación. Actualmente uso un indicador al que me gustaría poder añadirle una alerta para no estar pendiente del PC o teléfono, pero no sé como podría hacerlo.

Pongo aquí el código del indicador a ver si me podéis ayudar. Me he intentado documentar pero como no tengo base me estoy volviendo loco y no soy capaz. Muchas gracias.


study("RSI Swing Indicator", overlay=true, max_bars_back=1000)

// RSI Settings for user
rsiSource = input(title="RSI Source", type=input.source, defval=close)
rsiLength = input(title="RSI Length", type=input.integer, defval=7)
rsiOverbought = input(title="RSI Overbought", type=input.integer, defval=70, minval=51, maxval=100)
rsiOvesold = input(title="RSI Oversold", type=input.integer, defval=30, minval=1, maxval=49)

// RSI value based on inbuilt RSI
rsiValue = rsi(rsiSource, rsiLength)

// Get the current state
isOverbought = rsiValue >= rsiOverbought
isOversold = rsiValue <= rsiOvesold

// State of the last extreme 0 for initialization, 1 = overbought, 2 = oversold
var laststate = 0

// Highest and Lowest prices since the last state change
var hh = low
var ll = high

// Labels
var label labelll = na
var label labelhh = na

// Swing lines
var line line_up = na
var line line_down = na

var last_actual_label_hh_price = 0.0
var last_actual_label_ll_price = 0.0


// FUNCTIONS
obLabelText() =>
if(last_actual_label_hh_price < high)
"HH"
else
"LH"
//plot(last_actual_label_hh_price)
osLabelText() =>
if(last_actual_label_ll_price < low)
"HL"
else
"LL"

// Create oversold or overbought label
createOverBoughtLabel(isIt) =>
if(isIt)
label.new(x=bar_index, y=na ,yloc=yloc.abovebar, style=label.style_label_down, color=color.red, size=size.tiny, text=obLabelText())
else
label.new(x=bar_index, y=na ,yloc=yloc.belowbar, style=label.style_label_up, color=color.green, size=size.tiny, text=osLabelText())


// Move the oversold swing and label
moveOversoldLabel() =>
label.set_x(labelll, bar_index)
label.set_y(labelll, low)
label.set_text(labelll, osLabelText())
line.set_x1(line_down, bar_index)
line.set_y1(line_down, low)

moveOverBoughtLabel() =>
label.set_x(labelhh, bar_index)
label.set_y(labelhh, high)
label.set_text(labelhh, obLabelText())
line.set_x1(line_up, bar_index)
line.set_y1(line_up, high)

// We go from oversold straight to overbought NEW DRAWINGS CREATED HERE
if(laststate == 2 and isOverbought)
hh := high
labelhh := createOverBoughtLabel(true)
last_actual_label_ll_price := label.get_y(labelll)
labelll_ts = label.get_x(labelll)
labelll_price = label.get_y(labelll)
line_up := line.new(x1=bar_index, y1=high, x2=labelll_ts, y2=labelll_price, width=1)

// We go from overbought straight to oversold NEW DRAWINGS CREATED HERE
if(laststate == 1 and isOversold)
ll := low
labelll := createOverBoughtLabel(false)
last_actual_label_hh_price := label.get_y(labelhh)
labelhh_ts = label.get_x(labelhh)
labelhh_price = label.get_y(labelhh)
line_down := line.new(x1=bar_index, y1=high, x2=labelhh_ts, y2=labelhh_price, width=1)


// If we are overbought
if(isOverbought)
if(high >= hh)
hh := high
moveOverBoughtLabel()
laststate := 1

// If we are oversold
if(isOversold)
if(low <= ll)
ll := low
moveOversoldLabel()
laststate := 2


// If last state was overbought and we are overbought
if(laststate == 1 and isOverbought)
if(hh <= high)
hh := high
moveOverBoughtLabel()

//If we are oversold and the last state was oversold, move the drawings to the lowest price
if(laststate == 2 and isOversold)
if(low <= ll)
ll := low
moveOversoldLabel()


// If last state was overbought
if(laststate == 1)
if(hh <= high)
hh := high
moveOverBoughtLabel()

// If last stare was oversold
if(laststate == 2)
if(ll >= low)
ll := low
moveOversoldLabel()

Re: Añadir alerta a indicador Trading View

Publicado: 22 Mar 2022 16:25
por X-Trader
Hola Albertius, ¡bienvenido al Foro!

Una cuestión fundamental para poder ayudarte: ¿cuál es el evento que debe desatar la alerta?

Saludos,
X-Trader

Re: Añadir alerta a indicador Trading View

Publicado: 22 Mar 2022 16:46
por Albertius
Gracias por contestar.

Al ser un indicador dinámico. Lo que me gustaría es que cada vez que se genere una etiqueta de HH, LH, LL, HL, que Trading View me avise con una alerta "una vez por cierre de barra".

Perdón si no me explico con claridad. Adjunto una captura de pantalla para que se me entienda mejor.

Re: Añadir alerta a indicador Trading View

Publicado: 26 Mar 2022 18:11
por Albertius
X-Trader escribió: 22 Mar 2022 16:25 Hola Albertius, ¡bienvenido al Foro!

Una cuestión fundamental para poder ayudarte: ¿cuál es el evento que debe desatar la alerta?

Saludos,
X-Trader
Entiendo que la alerta que intento añadir, no es posible? Simple para salir de dudas y dejar de romperme la cabeza intentándolo. Gracias

Re: Añadir alerta a indicador Trading View

Publicado: 27 Mar 2022 19:04
por X-Trader
Hola Albertius, la creación de alertas en TradingView es muy sencilla, aquí tienes un tutorial explicando cómo hacerlo:

https://www.tradingview.com/pine-script ... lerts.html

En tu caso concreto, la cosa sería más o menos así (te he añadido comentarios para que veas donde he puesto las alertas):

Código: Seleccionar todo

study("RSI Swing Indicator", overlay=true, max_bars_back=1000)

// RSI Settings for user
rsiSource = input(title="RSI Source", type=input.source, defval=close)
rsiLength = input(title="RSI Length", type=input.integer, defval=7)
rsiOverbought = input(title="RSI Overbought", type=input.integer, defval=70, minval=51, maxval=100)
rsiOvesold = input(title="RSI Oversold", type=input.integer, defval=30, minval=1, maxval=49)

// RSI value based on inbuilt RSI
rsiValue = rsi(rsiSource, rsiLength)

// Get the current state
isOverbought = rsiValue >= rsiOverbought
isOversold = rsiValue <= rsiOvesold

// State of the last extreme 0 for initialization, 1 = overbought, 2 = oversold
var laststate = 0

// Highest and Lowest prices since the last state change
var hh = low
var ll = high

// Labels
var label labelll = na
var label labelhh = na

// Swing lines
var line line_up = na
var line line_down = na

var last_actual_label_hh_price = 0.0
var last_actual_label_ll_price = 0.0


// FUNCTIONS
obLabelText() =>
if(last_actual_label_hh_price < high)
"HH"
else
"LH"
//plot(last_actual_label_hh_price)
osLabelText() =>
if(last_actual_label_ll_price < low)
"HL"
else
"LL"

// Create oversold or overbought label
createOverBoughtLabel(isIt) =>
if(isIt)
label.new(x=bar_index, y=na ,yloc=yloc.abovebar, style=label.style_label_down, color=color.red, size=size.tiny, text=obLabelText())
//Alerta insertada
alert("Estamos en modo Overbought", alert.freq_once_per_bar_close)
else
label.new(x=bar_index, y=na ,yloc=yloc.belowbar, style=label.style_label_up, color=color.green, size=size.tiny, text=osLabelText())
//Alerta insertada
alert("Estamos en modo Oversold", alert.freq_once_per_bar_close)


// Move the oversold swing and label
moveOversoldLabel() =>
label.set_x(labelll, bar_index)
label.set_y(labelll, low)
label.set_text(labelll, osLabelText())
line.set_x1(line_down, bar_index)
line.set_y1(line_down, low)
//Alerta insertada
alert("Estamos en modo Oversold", alert.freq_once_per_bar_close)

moveOverBoughtLabel() =>
label.set_x(labelhh, bar_index)
label.set_y(labelhh, high)
label.set_text(labelhh, obLabelText())
line.set_x1(line_up, bar_index)
line.set_y1(line_up, high)
//Alerta insertada
alert("Estamos en modo Overbought", alert.freq_once_per_bar_close)

// We go from oversold straight to overbought NEW DRAWINGS CREATED HERE
if(laststate == 2 and isOverbought)
hh := high
labelhh := createOverBoughtLabel(true)
last_actual_label_ll_price := label.get_y(labelll)
labelll_ts = label.get_x(labelll)
labelll_price = label.get_y(labelll)
line_up := line.new(x1=bar_index, y1=high, x2=labelll_ts, y2=labelll_price, width=1)

// We go from overbought straight to oversold NEW DRAWINGS CREATED HERE
if(laststate == 1 and isOversold)
ll := low
labelll := createOverBoughtLabel(false)
last_actual_label_hh_price := label.get_y(labelhh)
labelhh_ts = label.get_x(labelhh)
labelhh_price = label.get_y(labelhh)
line_down := line.new(x1=bar_index, y1=high, x2=labelhh_ts, y2=labelhh_price, width=1)


// If we are overbought
if(isOverbought)
if(high >= hh)
hh := high
moveOverBoughtLabel()
laststate := 1

// If we are oversold
if(isOversold)
if(low <= ll)
ll := low
moveOversoldLabel()
laststate := 2


// If last state was overbought and we are overbought
if(laststate == 1 and isOverbought)
if(hh <= high)
hh := high
moveOverBoughtLabel()

//If we are oversold and the last state was oversold, move the drawings to the lowest price
if(laststate == 2 and isOversold)
if(low <= ll)
ll := low
moveOversoldLabel()


// If last state was overbought
if(laststate == 1)
if(hh <= high)
hh := high
moveOverBoughtLabel()

// If last stare was oversold
if(laststate == 2)
if(ll >= low)
ll := low
moveOversoldLabel()
Pruébalo y si no te funciona, me dices.

Saludos,
X-Trader