简体   繁体   中英

Pinescript; Combining two indicators

I'm trying to combine 2 semi-complex indicators, but my lack of experience is preventing me from overcoming the errors at each attempt. Below is the code, perhaps someone here has the time and capability to make this work

    //@version=3
study(" RSI + BB (EMA) + Dispersion (2.0)", overlay=false)

// Инициализация параметров
src = input(title="Source", type=source, defval=close) // Устанавливаем тип цены для расчетов
for_rsi = input(title="RSI_period", type=integer, defval=14) // Период для RSI
for_ma = input(title="Basis_BB", type=integer, defval=20) // Период для MA внутри BB
for_mult = input(title="Stdev", type=integer, defval=2, minval=1, maxval=5) // Число стандартных отклонений для BB
for_sigma = input(title="Dispersion", type=float, defval=0.1, minval=0.01, maxval=1) // Дисперсия вокруг MA

// Условия работы скрипта
current_rsi = rsi(src, for_rsi) // Текущее положение индикатора RSI
basis = ema(current_rsi, for_ma)
dev = for_mult * stdev(current_rsi, for_ma)
upper = basis + dev
lower = basis - dev
disp_up = basis + ((upper - lower) * for_sigma) // Минимально-допустимый порог в области мувинга, который должен преодолеть RSI (сверху)
disp_down = basis - ((upper - lower) * for_sigma) // Минимально-допустимый порог в области мувинга, который должен преодолеть RSI (снизу)
color_rsi = current_rsi >= disp_up ? lime : current_rsi <= disp_down ? red : #ffea00 // Текущий цвет RSI, в зависимости от его местоположения внутри BB

// Дополнительные линии и заливка для областей для RSI
h1 = hline(70, color=#d4d4d4, linestyle=dotted, linewidth=1)
h2 = hline(30, color=#d4d4d4, linestyle=dotted, linewidth=1)
fill (h1, h2, transp=95)

// Алерты и условия срабатывания
rsi_Green = crossover(current_rsi, disp_up)
rsi_Red = crossunder(current_rsi, disp_down)

alertcondition(condition=rsi_Green, 
     title="RSI cross Above Dispersion Area",
     message="The RSI line closing crossed above the Dispersion area.")

alertcondition(condition=rsi_Red,
     title="RSI cross Under Dispersion Area",
     message="The RSI line closing crossed below the Dispersion area")

// Результаты и покраска
plot(basis, color=black)
plot(upper, color=#00fff0, linewidth=2)
plot(lower, color=#00fff0, linewidth=2)
s1 = plot(disp_up, color=white)
s2 = plot(disp_down, color=white)
fill(s1, s2, color=white, transp=80)
plot(current_rsi, color=color_rsi, linewidth=2)

combined with the following:

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

//@version=4
study("Stochastic Heat Map", shorttitle="SHM", overlay=false)

_source = close

MA =        input(title="MA", options=["SMA", "EMA", "WMA"], defval="SMA")
Theme =     input(title="Theme", options=["Theme 1", "Theme 2", "Theme 3"], defval="Theme 2")
inc =       input(title="Increment", defval=1)
smooth =    input(title="Smooth Fast", defval=1, type=input.integer)
smoothSlow= input(title="Smooth Slow", defval=5, type=input.integer)
plotNumber= input(title="Plot Number", defval=28, minval=1, maxval=28, type=input.integer)
PaintBars = input(title="Paint Bars", defval=true, type=input.bool)
IncType =   input(title="Waves", defval=false, type=input.bool)

trans = 0

cp1 = color.new(#01ff00, trans)
cp2 = color.new(#05d904, trans)
cp3 = color.new(#04b504, trans)
cp4 = color.new(#039103, trans)
cp5 = color.new(#027502, trans)

cn1 = color.new(#ff0505, trans)
cn2 = color.new(#d60606, trans)
cn3 = color.new(#b80606, trans)
cn4 = color.new(#910303, trans)
cn5 = color.new(#750202, trans)

cp6 =  color.new(#00ddff, trans)
cp7 =  color.new(#04bcd9, trans)
cp8 =  color.new(#049cb3, trans)
cp9 =  color.new(#047f91, trans)
cp10 = color.new(#046775, trans)

cn6 =  color.new(#d800ff, trans)
cn7 =  color.new(#bb04db, trans)
cn8 =  color.new(#9b05b5, trans)
cn9 =  color.new(#7b038f, trans)
cn10 = color.new(#640275, trans)

cp11 = color.new(#cf0000, trans)
cp12 = color.new(#f22b11, trans)
cp13 = color.new(#f29811, trans)
cp14 = color.new(#eef211, trans)
cp15 = color.new(#3af211, trans)

cn11 = color.new(#02269e, trans)
cn12 = color.new(#0039f5, trans)
cn13 = color.new(#1176f2, trans)
cn14 = color.new(#11aff2, trans)
cn15 = color.new(#11e7f2, trans)

fc1 = color.new(color.white, 0)
sc1 = color.new(color.silver, 0)
fc2 = color.new(color.white, 0)
sc2 = color.new(color.blue, 0)
fc3 = color.new(color.white, 0)
sc3 = color.new(color.orange, 0)

fastColour = Theme == "Theme 1" ? fc1 : Theme == "Theme 2" ? fc2  : fc3
slowColour = Theme == "Theme 1" ? sc1 : Theme == "Theme 2" ? sc2  : sc3

pColour1 = Theme == "Theme 1" ? cp1 : Theme == "Theme 2" ? cp6  : cp11
pColour2 = Theme == "Theme 1" ? cp2 : Theme == "Theme 2" ? cp7  : cp12
pColour3 = Theme == "Theme 1" ? cp3 : Theme == "Theme 2" ? cp8  : cp13
pColour4 = Theme == "Theme 1" ? cp4 : Theme == "Theme 2" ? cp9  : cp14
pColour5 = Theme == "Theme 1" ? cp5 : Theme == "Theme 2" ? cp10 : cp15

nColour1 = Theme == "Theme 1" ? cn1 : Theme == "Theme 2" ? cn6  : cn11
nColour2 = Theme == "Theme 1" ? cn2 : Theme == "Theme 2" ? cn7  : cn12
nColour3 = Theme == "Theme 1" ? cn3 : Theme == "Theme 2" ? cn8  : cn13
nColour4 = Theme == "Theme 1" ? cn4 : Theme == "Theme 2" ? cn9  : cn14
nColour5 = Theme == "Theme 1" ? cn5 : Theme == "Theme 2" ? cn10 : cn15

getColour(a) =>
    if(a >= 90)
        pColour1
    else
        if(a >= 80)
            pColour2
        else
            if(a >= 70)
                pColour3
            else
                if(a >= 60)
                    pColour4
                else
                    if(a >= 50)
                        pColour5
                    else
                        if(a >= 40)
                            nColour5
                        else
                            if(a >= 30)
                                nColour4
                            else
                                if(a >= 20)
                                    nColour3
                                else
                                    if(a >= 10)
                                        nColour2
                                    else
                                        if(a >= 0)
                                            nColour1
                                            
getStoch(i, incr) =>
    c = (i * inc)
    s = smooth + incr
    if MA == "SMA"
        sma(stoch(_source, high, low, c), s)
    else
        if MA == "EMA"
            ema(stoch(_source, high, low, c), s)
        else
            if MA == "WMA"
                wma(stoch(_source, high, low, c), s)

stoch1 =  plotNumber > 0  ? getStoch(IncType ? 1   : 1 , IncType ? 1  : 0) : 0
stoch2 =  plotNumber > 1  ? getStoch(IncType ? 2   : 2 , IncType ? 2  : 0) : 0
stoch3 =  plotNumber > 2  ? getStoch(IncType ? 3   : 3 , IncType ? 3  : 0) : 0
stoch4 =  plotNumber > 3  ? getStoch(IncType ? 4   : 4 , IncType ? 4  : 0) : 0
stoch5 =  plotNumber > 4  ? getStoch(IncType ? 5   : 5 , IncType ? 5  : 0) : 0
stoch6 =  plotNumber > 5  ? getStoch(IncType ? 6   : 6 , IncType ? 6  : 0) : 0
stoch7 =  plotNumber > 6  ? getStoch(IncType ? 7   : 7 , IncType ? 7  : 0) : 0
stoch8 =  plotNumber > 7  ? getStoch(IncType ? 8   : 8 , IncType ? 8  : 0) : 0
stoch9 =  plotNumber > 8  ? getStoch(IncType ? 9   : 9 , IncType ? 9  : 0) : 0
stoch10 = plotNumber > 9  ? getStoch(IncType ? 10  : 10, IncType ? 10 : 0) : 0
stoch11 = plotNumber > 10 ? getStoch(IncType ? 15  : 11, IncType ? 11 : 0) : 0
stoch12 = plotNumber > 11 ? getStoch(IncType ? 20  : 12, IncType ? 12 : 0) : 0
stoch13 = plotNumber > 12 ? getStoch(IncType ? 25  : 13, IncType ? 13 : 0) : 0
stoch14 = plotNumber > 13 ? getStoch(IncType ? 30  : 14, IncType ? 14 : 0) : 0
stoch15 = plotNumber > 14 ? getStoch(IncType ? 35  : 15, IncType ? 15 : 0) : 0
stoch16 = plotNumber > 15 ? getStoch(IncType ? 40  : 16, IncType ? 16 : 0) : 0
stoch17 = plotNumber > 16 ? getStoch(IncType ? 45  : 17, IncType ? 17 : 0) : 0
stoch18 = plotNumber > 17 ? getStoch(IncType ? 50  : 18, IncType ? 18 : 0) : 0
stoch19 = plotNumber > 18 ? getStoch(IncType ? 55  : 19, IncType ? 19 : 0) : 0
stoch20 = plotNumber > 19 ? getStoch(IncType ? 60  : 20, IncType ? 20 : 0) : 0
stoch21 = plotNumber > 20 ? getStoch(IncType ? 70  : 21, IncType ? 21 : 0) : 0
stoch22 = plotNumber > 21 ? getStoch(IncType ? 80  : 22, IncType ? 22 : 0) : 0
stoch23 = plotNumber > 22 ? getStoch(IncType ? 90  : 23, IncType ? 23 : 0) : 0
stoch24 = plotNumber > 23 ? getStoch(IncType ? 100 : 24, IncType ? 24 : 0) : 0
stoch25 = plotNumber > 24 ? getStoch(IncType ? 110 : 25, IncType ? 25 : 0) : 0
stoch26 = plotNumber > 25 ? getStoch(IncType ? 120 : 26, IncType ? 26 : 0) : 0
stoch27 = plotNumber > 26 ? getStoch(IncType ? 140 : 27, IncType ? 27 : 0) : 0
stoch28 = plotNumber > 27 ? getStoch(IncType ? 160 : 28, IncType ? 28 : 0) : 0

colour1 =  getColour(stoch1) 
colour2 =  getColour(stoch2) 
colour3 =  getColour(stoch3) 
colour4 =  getColour(stoch4) 
colour5 =  getColour(stoch5) 
colour6 =  getColour(stoch6) 
colour7 =  getColour(stoch7) 
colour8 =  getColour(stoch8) 
colour9 =  getColour(stoch9) 
colour10 = getColour(stoch10)
colour11 = getColour(stoch11)
colour12 = getColour(stoch12)
colour13 = getColour(stoch13)
colour14 = getColour(stoch14)
colour15 = getColour(stoch15)
colour16 = getColour(stoch16)
colour17 = getColour(stoch17)
colour18 = getColour(stoch18)
colour19 = getColour(stoch19)
colour20 = getColour(stoch20)
colour21 = getColour(stoch21)
colour22 = getColour(stoch22)
colour23 = getColour(stoch23)
colour24 = getColour(stoch24)
colour25 = getColour(stoch25)
colour26 = getColour(stoch26)
colour27 = getColour(stoch27)
colour28 = getColour(stoch28)

getAverage = (stoch1 + stoch2 + stoch3 + stoch4 + stoch5 + stoch6 + stoch7 + stoch8 + stoch9 + stoch10 + stoch11 + stoch12 + stoch13 + stoch14 + stoch15 + stoch16 + stoch17 + stoch18 + stoch19 + stoch20 + stoch21 + stoch22 + stoch23 + stoch24 + stoch25 + stoch26 + stoch27 + stoch28) / plotNumber
fast = ((getAverage / 100) * plotNumber)
slow = MA == "SMA" ? sma(fast, smoothSlow) : MA == "EMA" ? ema(fast, smoothSlow) : wma(fast, smoothSlow)
barColour = getColour(getAverage)

plot1 =  plot(plotNumber > 0  ? 0 : na, color=colour1, linewidth=1)
plot2 =  plot(plotNumber > 1  ? 1 : na, color=colour2, linewidth=1)
plot3 =  plot(plotNumber > 2  ? 2 : na, color=colour3, linewidth=1)
plot4 =  plot(plotNumber > 3  ? 3 : na, color=colour4, linewidth=1)
plot5 =  plot(plotNumber > 4  ? 4 : na, color=colour5, linewidth=1)
plot6 =  plot(plotNumber > 5  ? 5 : na, color=colour6, linewidth=1)
plot7 =  plot(plotNumber > 6  ? 6 : na, color=colour7, linewidth=1)
plot8 =  plot(plotNumber > 7  ? 7 : na, color=colour8, linewidth=1)
plot9 =  plot(plotNumber > 8  ? 8 : na, color=colour9, linewidth=1)
plot10 = plot(plotNumber > 9  ? 9 : na, color=colour10, linewidth=1)
plot11 = plot(plotNumber > 10 ? 10 : na, color=colour11, linewidth=1)
plot12 = plot(plotNumber > 11 ? 11 : na, color=colour12, linewidth=1)
plot13 = plot(plotNumber > 12 ? 12 : na, color=colour13, linewidth=1)
plot14 = plot(plotNumber > 13 ? 13 : na, color=colour14, linewidth=1)
plot15 = plot(plotNumber > 14 ? 14 : na, color=colour15, linewidth=1)
plot16 = plot(plotNumber > 15 ? 15 : na, color=colour16, linewidth=1)
plot17 = plot(plotNumber > 16 ? 16 : na, color=colour17, linewidth=1)
plot18 = plot(plotNumber > 17 ? 17 : na, color=colour18, linewidth=1)
plot19 = plot(plotNumber > 18 ? 18 : na, color=colour19, linewidth=1)
plot20 = plot(plotNumber > 19 ? 19 : na, color=colour20, linewidth=1)
plot21 = plot(plotNumber > 20 ? 20 : na, color=colour21, linewidth=1)
plot22 = plot(plotNumber > 21 ? 21 : na, color=colour22, linewidth=1)
plot23 = plot(plotNumber > 22 ? 22 : na, color=colour23, linewidth=1)
plot24 = plot(plotNumber > 23 ? 23 : na, color=colour24, linewidth=1)
plot25 = plot(plotNumber > 24 ? 24 : na, color=colour25, linewidth=1)
plot26 = plot(plotNumber > 25 ? 25 : na, color=colour26, linewidth=1)
plot27 = plot(plotNumber > 26 ? 26 : na, color=colour27, linewidth=1)
plot28 = plot(plotNumber > 27 ? 27 : na, color=colour28, linewidth=1)
plot29 = plot(plotNumber, color=colour28, linewidth=1)

plot(slow, color=slowColour, linewidth=1, style=plot.style_line)
plot(fast, color=fastColour, linewidth=1, style=plot.style_line)

barcolor(PaintBars ? barColour : na)

// -->

Visually the lines and colors can already be adjusted/brightened in the settings to be better suited. But it looks like there still needs to be a transparent shader histogram/background inside the Bollinger bands so the colors don't blend/bleed through too much in order to prevent distractions. I would really appreciate the help combining the pinescript code to get it working on trading view. This would make a good public modified indicator to share.

You cannot combine an indicator overlay = true with another indicator overlay = false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM