3

I'm trying to use custom Stochastic and RSI in version5 but it only allows me to use 1 indicator in the script.

I've tried using study but that doesn't work.

How can I fix this?

I've tried removing version version5 and using only study but that breaks the RSI code.

PS - not a programmer. Know very little about coding, trying to learn though!

//@version=5

indicator(title="Stochastic+RSI+MACD", shorttitle="Stoch+RSI+MACD", format=format.price, precision=2, timeframe="", timeframe_gaps=true)

//Stochastic
periodK = input.int(14, title="%K Length", minval=1)
smoothK = input.int(1, title="%K Smoothing", minval=1)
periodD = input.int(3, title="%D Smoothing", minval=1)
k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
d = ta.sma(k, periodD)
plot(k, title="%K", color=#2196F3)
plot(d, title="%D", color=#FF9800)
h0 = hline(80, "Upper Band", color=#787B86)
h1 = hline(20, "Lower Band", color=#787B86)
fill(h0, h1, color=color.rgb(33, 150, 243, 90), title="Background")

//RSI
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => 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)

rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(14, title="MA Length", group="MA Settings")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands"

plot(rsi, "RSI", color=#7E57C2)
rsiUpperBand = hline(50, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(50, "RSI Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")
bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Upper Bollinger Band", color=color.green)
bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Lower Bollinger Band", color=color.green)
fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill")

//MACD
// Getting inputs
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
src = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])
// Plot colors
col_macd = input(#2962FF, "MACD Line  ", group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "Signal Line  ", group="Color Settings", inline="Signal")
col_grow_above = input(#26A69A, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")
// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
plot(macd, title="MACD", color=col_macd)
plot(signal, title="Signal", color=col_signal)
rohanx
  • 33
  • 5

1 Answers1

2

You can only have 1 indicator() or study() tag in a script.
You have to merge the code of both, like this for example:

//@version=5
//Stochastic
indicator(title="Stochastic+RSI", shorttitle="Stoch+RSI", format=format.price, precision=2, timeframe="", timeframe_gaps=true)

periodK = input.int(14, title="%K Length", minval=1)
smoothK = input.int(1, title="%K Smoothing", minval=1)
periodD = input.int(3, title="%D Smoothing", minval=1)

rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(14, title="MA Length", group="MA Settings")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")

k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
d = ta.sma(k, periodD)
plot(k, title="%K", color=#2962FF)
plot(d, title="%D", color=#FF6D00)
h0 = hline(80, "Upper Band", color=#787B86)
h1 = hline(20, "Lower Band", color=#787B86)
fill(h0, h1, color=color.rgb(33, 150, 243, 90), title="Background")



ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => 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)


up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands"

plot(rsi, "RSI", color=#7E57C2)
plot(rsiMA, "RSI-based MA", color=color.yellow)
rsiUpperBand = hline(70, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(30, "RSI Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")
bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Upper Bollinger Band", color=color.green)
bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Lower Bollinger Band", color=color.green)
fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill")
Bjorn Mistiaen
  • 6,459
  • 3
  • 18
  • 42
  • Hey @Bjorn Mistiaen! That worked like a charm, thanks! I made some modifications to the settings and added the MACD as well. The histogram is 'squeezing' the Stoch & RSI to a point where it's not legible. Do you know how I can scale them? PS - Have updated the code in the original question. – rohanx Jan 25 '22 at 14:44
  • You can't do that, because TradingView will auto-scale everything on the chart between the highest and lowest value. The only thing that I can think of is muliplying your `hist` plot with a factor. – Bjorn Mistiaen Jan 25 '22 at 15:25
  • Okay! That makes complete sense! I multiplied the hist with 0.1 and I'm able to view it on the same graph now! Do you know how I could align the middle value of the histogram with the 'middle band in RSI' ? – rohanx Jan 25 '22 at 15:59
  • The MACD neutral line starts at '0' and the RSI neutral line is at '50' This is the hist code `plot(hist*0.1, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))` – rohanx Jan 25 '22 at 16:06
  • I don't think you can do that, because a histogram always plots with a center line of 0. Unless you move the RSI neutral line to 0 too ? – Bjorn Mistiaen Jan 25 '22 at 16:07
  • Ahhh got it! I've figured how to move the range (80,20) and RSI in relation to 0 basis the hist. Do you know how I can plot the lines given the 'new' 'neutral' ? For instance the `plot(k, title="%K", color=#2196F3)` Not sure if we can move this to a custom starting point – rohanx Jan 25 '22 at 16:17
  • Do you think we can use `histbase` ? – rohanx Jan 25 '22 at 16:21
  • Ah yes. Never used that one before. It seems to work : `plot(rsi+hist, histbase=50, title="Histogram"`... – Bjorn Mistiaen Jan 25 '22 at 16:25
  • Hey! Just edited the code! `histbase` works! – rohanx Jan 25 '22 at 16:26
  • Yes, thanks! This is really cool!! 2 follow up questions on this if you don't mind? The scale is working disproportionately. It's showing from 100 to '-x' . Is there a way I can modify this? And second - the RSI/Stoch lines are getting hidden behind the histograms. How can I bring them forward? – rohanx Jan 25 '22 at 16:31
  • Just reviewed the plotting again. The histbase function doesn't work correctly. It doesn't move the true '0' to 50, instead plots an alternative hist from 50 as the neutral line which gives contorted values. – rohanx Jan 25 '22 at 16:36