allan7
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: December 24th, 2023

Combining two indicators in one script

Am very new to pinescript so need help. Trying to combine 2 indicators of PS version 5 into once script on overlay i.e. in the price charting area not the bottom. But it just goes into the bottom area with error code "can't compile this script."

Here is code for main indicator i.e. SuperTrend Channels
and trying to add SMA as a combo. .....code for sma is also in version 5.


// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// © LuxAlgo

//@version=5
indicator("Supertrend Channels [LuxAlgo]",overlay=true,max_lines_count=500)
length = input(14)
mult = input(2)
//------------------------------------------------------------------------------
upper = 0.,lower = 0.,os = 0,max = 0.,min = 0.

src = close
atr = ta.atr(length)*mult
up = hl2 + atr
dn = hl2 - atr
upper := src[1] < upper[1] ? math.min(up,upper[1]) : up
lower := src[1] > lower[1] ? math.max(dn,lower[1]) : dn

os := src > upper ? 1 : src < lower ? 0 : os[1]
spt = os == 1 ? lower : upper

max := ta.cross(src,spt) ? nz(math.max(max[1],src),src) :
os == 1 ? math.max(src,max[1]) :
math.min(spt,max[1])

min := ta.cross(src,spt) ? nz(math.min(min[1],src),src) :
os == 0 ? math.min(src,min[1]) :
math.max(spt,min[1])

avg = math.avg(max,min)
//------------------------------------------------------------------------------
var area_up_col = color.new(#0cb51a,80)
var area_dn_col = color.new(#ff1100,80)

plot0 = plot(max,'Upper Channel'
,max != max[1] and os == 1 ? na : #0cb51a)
plot1 = plot(avg,'Average'
,#ff5d00)
plot2 = plot(min,'Lower Channel'
,min != min[1] and os == 0 ? na : #ff1100)

fill(plot0,plot1,area_up_col,'Upper Area')
fill(plot1,plot2,area_dn_col,'Lower Area')


//sma

len = input.int(9, minval=1, title="Length")
src = input(close, title="Source")
offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)
out = ta.sma(src, len)
plot(out, color=color.blue, title="MA", offset=offset)

ma(source, length, type) =>
switch type
"SMA" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)

typeMA = input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="Smoothing")
smoothingLength = input.int(title = "Length", defval = 5, minval = 1, maxval = 100, group="Smoothing")

smoothingLine = ma(out, smoothingLength, typeMA)
plot(smoothingLine, title="Smoothing Line", color=#f37f20, offset=offset, display=display.none)

augerpro
Pine Script Rookie
Pine Script Rookie
Posts: 10
Joined: July 31st, 2022

Re: Combining two indicators in one script

Be warned I'm a novice, but you have defined the variable "src" twice. Once in the SuperTrend and once in your SMA at the bottom. You only need to define it once, assuming you are using the same source for both functions. Also the MA selector is using "source" and "length", make sure you are using the correct variables here, you can't just cut and past that code while naming your own variables something else.

allan7
Pine Script Rookie
Pine Script Rookie
Posts: 2
Joined: December 24th, 2023

Re: Combining two indicators in one script

Thx for the inputs, I changed src in moving avg code to src1 but it still shows "cannot compile script" and that too in the bottom panel while both the indicators are supposed to be plotted in upper panel along with the price bars. The overlay = true for both the indicators.

Return to “Pine Script Q&A”