incluir 2 indicadores en un solo script (Bandas de Bolliger y Auto Fib Extension)

El espacio del Foro donde compartir indicadores y estrategias creados con el lenguaje de la plataforma TradingView.
Responder
JJTrader
Mensajes: 3
Registrado: 03 Abr 2023 00:22

incluir 2 indicadores en un solo script (Bandas de Bolliger y Auto Fib Extension)

Mensaje por JJTrader »

Hola,

Es posibles incluir 2 indicadores tales como Bandas de Bolliger y Auto Fib Extension en un solo script.

Agradezco su orientación

Estos son los script de cada indicador:

Bandas de Bollinger

//@version=5
indicator(shorttitle="BB", title="Bollinger Bands", overlay=true, timeframe="", timeframe_gaps=true)
length = input.int(20, minval=1)
src = input(close, title="Source")
mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
offset = input.int(0, "Offset", minval = -500, maxval = 500)
plot(basis, "Basis", color=#2962FF, offset = offset)
p1 = plot(upper, "Upper", color=#bbbdc2, offset = offset)
p2 = plot(lower, "Lower", color=#bbbdc2, offset = offset)
fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))

//Evaluation
Bear = high >= upper
Bull = low <= lower

//Plots
plotshape(Bear, style=shape.triangledown, location=location.abovebar,
color=color.red, size=size.tiny)
plotshape(Bull, style=shape.triangleup, location=location.belowbar,
color=color.green, size=size.tiny)

// Alert Functionality
alertcondition(Bear or Bull, title="Any Signal", message="{{exchange}}:{{ticker}}" + " {{interval}}")
alertcondition(Bear, title="Bearish Signal", message="{{exchange}}:{{ticker}}" + " {{interval}}")
alertcondition(Bull, title="Bullish Signal", message="{{exchange}}:{{ticker}}" + " {{interval}}")





Aut Fib Extension



//@version=5
indicator("Auto Fib Extension", overlay=true)

depthTooltip = "The minimum number of bars that will be taken into account when calculating the indicator."
depth = input.int(title="Depth", defval=10, minval=2, inline = "Pivots", tooltip=depthTooltip)
reverse = input(false, "Reverse")
var extendLeft = input(false, "Extend Left    |    Extend Right", inline = "Extend Lines")
var extendRight = input(true, "", inline = "Extend Lines")
var extending = extend.none
if extendLeft and extendRight
extending := extend.both
if extendLeft and not extendRight
extending := extend.left
if not extendLeft and extendRight
extending := extend.right
prices = input(true, "Show Prices")
levels = input(true, "Show Levels", inline = "Levels")
levelsFormat = input.string("Values", "", options = ["Values", "Percent"], inline = "Levels")
labelsPosition = input.string("Left", "Labels Position", options = ["Left", "Right"])
backgroundTransparency = input.int(85, "Background Transparency", minval = 0, maxval = 100)

upperThreshold = 0.236
lowerThreshold = 1.0

import TradingView/ZigZag/5 as zigzag

pivots(src, length, isHigh) =>
if bar_index >= length
price = nz(src[length])
found = true
for i = 0 to length * 2
if (isHigh and src > price) or (not isHigh and src < price)
found := false
break
if found
zigzag.Point.new(time[length], price)

update()=>
var line lineLastHL = na
var line lineLastLH = na
var line lineLast = na

var zigzag.Point[] pivotsH = array.new<zigzag.Point>()
var zigzag.Point lastH = na
var zigzag.Point[] pivotsL = array.new<zigzag.Point>()
var zigzag.Point lastL = na

var isHighLast = false
var float startPrice = na
var float endPrice = na

H = pivots(high, depth / 2, true)
L = pivots(low, depth / 2, false)

countPivotsH = array.size(pivotsH)
countPivotsL = array.size(pivotsL)

if countPivotsH > 0 and countPivotsL > 0
lastH := array.get(pivotsH, countPivotsH-1)
lastL := array.get(pivotsL, countPivotsL-1)
isHighLast := lastH.tm > lastL.tm
if isHighLast
if not na(H)
if H.price > lastH.price
array.set(pivotsH, countPivotsH-1, H)
H := na
else
if not na(L)
if L.price < lastL.price
array.set(pivotsL, countPivotsL-1, L)
L := na

if not na(H)
array.push(pivotsH, H)

if not na(L)
array.push(pivotsL, L)

if barstate.islast and array.size(pivotsH) > 0 and array.size(pivotsL) > 0
pivotsHCopy = array.copy(pivotsH)
pivotsLCopy = array.copy(pivotsL)
while array.size(pivotsHCopy) > 0 and array.size(pivotsLCopy) > 0
lastH := array.pop(pivotsHCopy)
lastL := array.pop(pivotsLCopy)

isHighLast := lastH.tm > lastL.tm
pivots = isHighLast ? pivotsHCopy : pivotsLCopy

for i = array.size(pivots)-1 to 0
if i < 0
break
p = array.get(pivots, i)
if p.tm < lastL.tm
break
betterPrice = isHighLast ? p.price > lastH.price : p.price < lastL.price
if p.price > lastH.price
lastH := array.pop(pivots)
else
array.remove(pivots, i)

if array.size(pivotsHCopy) == 0 or array.size(pivotsLCopy) == 0
break

isHighLast := lastH.tm > lastL.tm
pivots := isHighLast ? pivotsHCopy : pivotsLCopy

prevPivot = array.get(pivots, array.size(pivots)-1)
startPrice := prevPivot.price

if isHighLast
endPrice := lastL.price
diff = math.abs(startPrice - endPrice)
if lastH.price > endPrice + diff * lowerThreshold or lastH.price < endPrice + diff * upperThreshold
array.push(pivotsLCopy, lastL)
continue
line.delete(lineLastHL)
line.delete(lineLastLH)
lineLastHL := line.new(prevPivot.tm, prevPivot.price, lastL.tm, lastL.price, color=color.red, width=1, style=line.style_dashed, xloc = xloc.bar_time)
lineLastLH := line.new(lastL.tm, lastL.price, lastH.tm, lastH.price, color=color.green, width=1, style=line.style_dashed, xloc = xloc.bar_time)
lineLast := lineLastLH
else
endPrice := lastH.price
diff = math.abs(startPrice - endPrice)
if lastL.price < endPrice - diff * lowerThreshold or lastL.price > endPrice - diff * upperThreshold
array.push(pivotsHCopy, lastH)
continue
line.delete(lineLastHL)
line.delete(lineLastLH)
lineLastLH := line.new(prevPivot.tm, prevPivot.price, lastH.tm, lastH.price, color=color.red, width=1, style=line.style_dashed, xloc = xloc.bar_time)
lineLastHL := line.new(lastH.tm, lastH.price, lastL.tm, lastL.price, color=color.green, width=1, style=line.style_dashed, xloc = xloc.bar_time)
lineLast := lineLastHL
break
diff = (isHighLast ? -1 : 1) * math.abs(startPrice - endPrice)
offset = isHighLast ? line.get_y1(lineLastLH) - line.get_y2(lineLastLH) : line.get_y1(lineLastHL) - line.get_y2(lineLastHL)
offset := (isHighLast ? -1 : 1) * math.abs(offset)
[endPrice - offset, diff, lineLast]

[endPrice, diff, lineLast] = update()

_draw_line(price, col) =>
var id = line.new(time, price, time, price, color=col, width=1, extend=extending, xloc = xloc.bar_time)
if not na(lineLast)
line.set_xy1(id, line.get_x1(lineLast), price)
line.set_xy2(id, line.get_x2(lineLast), price)
id

_draw_label(price, txt, txtColor) =>
if not na(price)
x = labelsPosition == "Left" ? line.get_x1(lineLast) : not extendRight ? line.get_x2(lineLast) : time
labelStyle = labelsPosition == "Left" ? label.style_label_right : label.style_label_left
align = labelsPosition == "Left" ? text.align_right : text.align_left
labelsAlignStrLeft = txt + '\n ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ \n'
labelsAlignStrRight = ' ' + txt + '\n ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ ‏ \n'
labelsAlignStr = labelsPosition == "Left" ? labelsAlignStrLeft : labelsAlignStrRight
var id = label.new(x=x, y=price, text=labelsAlignStr, textcolor=txtColor, style=labelStyle, textalign=align, color=#00000000, xloc = xloc.bar_time)
label.set_xy(id, x, price)
label.set_text(id, labelsAlignStr)
label.set_textcolor(id, txtColor)

_wrap(txt) =>
"(" + str.tostring(txt, format.mintick) + ")"

_label_txt(level, price) =>
if not na(price)
l = levelsFormat == "Values" ? str.tostring(level) : str.tostring(level * 100) + "%"
(levels ? l : "") + (prices ? _wrap(price) : "")

_crossing_level(sr, r) =>
(r > sr and r < sr[1]) or (r < sr and r > sr[1])


processLevel(show, value, colorL, lineIdOther) =>
float m = value
r = endPrice + ((reverse ? -1 : 1) * diff * m)
if show
lineId = _draw_line(r, colorL)
_draw_label(r, _label_txt(m, r), colorL)
if _crossing_level(close, r)
alert("Autofib: " + syminfo.ticker + " crossing level " + str.tostring(value))
if not na(lineIdOther)
linefill.new(lineId, lineIdOther, color = color.new(colorL, backgroundTransparency))
lineId
else
lineIdOther

show_0 = input(true, "", inline = "Level0")
value_0 = input(0, "", inline = "Level0")
color_0 = input(#787b86, "", inline = "Level0")

show_0_236 = input(true, "", inline = "Level0")
value_0_236 = input(0.236, "", inline = "Level0")
color_0_236 = input(#f44336, "", inline = "Level0")

show_0_382 = input(true, "", inline = "Level1")
value_0_382 = input(0.382, "", inline = "Level1")
color_0_382 = input(#81c784, "", inline = "Level1")

show_0_5 = input(true, "", inline = "Level1")
value_0_5 = input(0.5, "", inline = "Level1")
color_0_5 = input(#4caf50, "", inline = "Level1")

show_0_618 = input(true, "", inline = "Level2")
value_0_618 = input(0.618, "", inline = "Level2")
color_0_618 = input(#009688, "", inline = "Level2")

show_0_65 = input(false, "", inline = "Level2")
value_0_65 = input(0.65, "", inline = "Level2")
color_0_65 = input(#009688, "", inline = "Level2")

show_0_786 = input(true, "", inline = "Level3")
value_0_786 = input(0.786, "", inline = "Level3")
color_0_786 = input(#64b5f6, "", inline = "Level3")

show_1 = input(true, "", inline = "Level3")
value_1 = input(1, "", inline = "Level3")
color_1 = input(#787b86, "", inline = "Level3")

show_1_272 = input(false, "", inline = "Level4")
value_1_272 = input(1.272, "", inline = "Level4")
color_1_272 = input(#81c784, "", inline = "Level4")

show_1_414 = input(false, "", inline = "Level4")
value_1_414 = input(1.414, "", inline = "Level4")
color_1_414 = input(#f44336, "", inline = "Level4")

show_1_618 = input(true, "", inline = "Level5")
value_1_618 = input(1.618, "", inline = "Level5")
color_1_618 = input(#2962ff, "", inline = "Level5")

show_1_65 = input(false, "", inline = "Level5")
value_1_65 = input(1.65, "", inline = "Level5")
color_1_65 = input(#2962ff, "", inline = "Level5")

show_2_618 = input(true, "", inline = "Level6")
value_2_618 = input(2.618, "", inline = "Level6")
color_2_618 = input(#f44336, "", inline = "Level6")

show_2_65 = input(false, "", inline = "Level6")
value_2_65 = input(2.65, "", inline = "Level6")
color_2_65 = input(#f44336, "", inline = "Level6")

show_3_618 = input(true, "", inline = "Level7")
value_3_618 = input(3.618, "", inline = "Level7")
color_3_618 = input(#9c27b0, "", inline = "Level7")

show_3_65 = input(false, "", inline = "Level7")
value_3_65 = input(3.65, "", inline = "Level7")
color_3_65 = input(#9c27b0, "", inline = "Level7")

show_4_236 = input(true, "", inline = "Level8")
value_4_236 = input(4.236, "", inline = "Level8")
color_4_236 = input(#e91e63, "", inline = "Level8")

show_4_618 = input(false, "", inline = "Level8")
value_4_618 = input(4.618, "", inline = "Level8")
color_4_618 = input(#81c784, "", inline = "Level8")

show_neg_0_236 = input(false, "", inline = "Level9")
value_neg_0_236 = input(-0.236, "", inline = "Level9")
color_neg_0_236 = input(#f44336, "", inline = "Level9")

show_neg_0_382 = input(false, "", inline = "Level9")
value_neg_0_382 = input(-0.382, "", inline = "Level9")
color_neg_0_382 = input(#81c784, "", inline = "Level9")

show_neg_0_618 = input(false, "", inline = "Level10")
value_neg_0_618 = input(-0.618, "", inline = "Level10")
color_neg_0_618 = input(#009688, "", inline = "Level10")

show_neg_0_65 = input(false, "", inline = "Level10")
value_neg_0_65 = input(-0.65, "", inline = "Level10")
color_neg_0_65 = input(#009688, "", inline = "Level10")


lineId0 = processLevel(show_neg_0_65, value_neg_0_65, color_neg_0_65, line(na))
lineId1 = processLevel(show_neg_0_618, value_neg_0_618, color_neg_0_618, lineId0)
lineId2 = processLevel(show_neg_0_382, value_neg_0_382, color_neg_0_382, lineId1)
lineId3 = processLevel(show_neg_0_236, value_neg_0_236, color_neg_0_236, lineId2)
lineId4 = processLevel(show_0, value_0, color_0, lineId3)
lineId5 = processLevel(show_0_236, value_0_236, color_0_236, lineId4)
lineId6 = processLevel(show_0_382, value_0_382, color_0_382, lineId5)
lineId7 = processLevel(show_0_5, value_0_5, color_0_5, lineId6)
lineId8 = processLevel(show_0_618, value_0_618, color_0_618, lineId7)
lineId9 = processLevel(show_0_65, value_0_65, color_0_65, lineId8)
lineId10 = processLevel(show_0_786, value_0_786, color_0_786, lineId9)
lineId11 = processLevel(show_1, value_1, color_1, lineId10)
lineId12 = processLevel(show_1_272, value_1_272, color_1_272, lineId11)
lineId13 = processLevel(show_1_414, value_1_414, color_1_414, lineId12)
lineId14 = processLevel(show_1_618, value_1_618, color_1_618, lineId13)
lineId15 = processLevel(show_1_65, value_1_65, color_1_65, lineId14)
lineId16 = processLevel(show_2_618, value_2_618, color_2_618, lineId15)
lineId17 = processLevel(show_2_65, value_2_65, color_2_65, lineId16)
lineId18 = processLevel(show_3_618, value_3_618, color_3_618, lineId17)
lineId19 = processLevel(show_3_65, value_3_65, color_3_65, lineId18)
lineId20 = processLevel(show_4_236, value_4_236, color_4_236, lineId19)
lineId21 = processLevel(show_4_618, value_4_618, color_4_618, lineId20)
Avatar de Usuario
Gibranes
Mensajes: 231
Registrado: 22 Jun 2010 07:57

Re: incluir 2 indicadores en un solo script (Bandas de Bolliger y Auto Fib Extension)

Mensaje por Gibranes »

Hola:
No utilizo TraderdingView uso ninjatrader, en mi plataforma el problema que planteas lo soluciono así:
Creo el chart añado los indicadores para ese chart y lo guardo en una plantilla, al usar esa plantilla ya se carga el chart con los indicadores deseados. No sé si tu plataforma tiene esa opción.
gu5tavo71
Mensajes: 27
Registrado: 12 Sep 2022 00:15
Contactar:

Re: incluir 2 indicadores en un solo script (Bandas de Bolliger y Auto Fib Extension)

Mensaje por gu5tavo71 »

Hola

Para unir dos indicadores, por lo general solo tenés que eliminar un para de lineas del segundo indicador:

Código: Seleccionar todo

//@version=5
indicator(...)
No posteo tu codigo completo, porque son mas de 300 lineas. Es muy largo.
Podes descarga tu script unificado desde https://pastebin.com/H3hj1rPf
@gu5tavo71
Senior PineScript Developer
TradingView | Twitter | Freelancer | Telegram
JJTrader
Mensajes: 3
Registrado: 03 Abr 2023 00:22

Re: incluir 2 indicadores en un solo script (Bandas de Bolliger y Auto Fib Extension)

Mensaje por JJTrader »

Gibranes escribió: 03 Abr 2023 08:38 Hola:
No utilizo TraderdingView uso ninjatrader, en mi plataforma el problema que planteas lo soluciono así:
Creo el chart añado los indicadores para ese chart y lo guardo en una plantilla, al usar esa plantilla ya se carga el chart con los indicadores deseados. No sé si tu plataforma tiene esa opción.
Gracias por la respuesta y el dato de la plataforma que usas, en Tradingview ya intente lo que me recomendaste y salto el error por el uso de los 2 indicadores al mismo tiempo.
JJTrader
Mensajes: 3
Registrado: 03 Abr 2023 00:22

Re: incluir 2 indicadores en un solo script (Bandas de Bolliger y Auto Fib Extension)

Mensaje por JJTrader »

gu5tavo71 escribió: 03 Abr 2023 15:44 Hola

Para unir dos indicadores, por lo general solo tenés que eliminar un para de lineas del segundo indicador:

Código: Seleccionar todo

//@version=5
indicator(...)
No posteo tu codigo completo, porque son mas de 300 lineas. Es muy largo.
Podes descarga tu script unificado desde https://pastebin.com/H3hj1rPf
Excelente gu5tavo71, funciono muy bien, totalmente agradecido!!
Si te ha gustado este hilo del Foro, ¡compártelo en redes!


Responder

Volver a “Pine Script”