Search code examples
pine-script

Combine 2 pine script (rsi+ macd)


I'm currently writing a strategy using Pine-script, which is also my first experience writing codes.
Here i want to combine 2 pine scripts.
I managed to combine it but in some low price stocks its show very small line. like that bellow : https://i.gyazo.com/be86143ab4ef34811929658b2afd1095.png

If i use only that indicator its shows perfectly like this https://i.gyazo.com/e8c607e3b0772f37f27e532662ad61c1.png

please help me.

indicator(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, timeframe="", timeframe_gaps=true)

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)
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")



/////  2nd  indicator      //////


// 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_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
hline(0, "Zero Line", color=color.new(#787B86, 50))
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)))

Solution

  • You have two problems with using those 2 indicators together :

    1. The RSI indicator will give you a value between 0 and 100
    2. The MACD indicator will give you positives and negatives values

    You should first transform your RSI so it gives you values between -50 and 50, so that the middle is 0. You then can multiply the MACD value by a number to adapt it's values between -50 and 50.

    The example below is ok for EUR/USD, but you have to adapt the MultiplicatorMACDvariable if you want to see another pair.

    The result : enter image description here

    //@version=5
    indicator(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
    
    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)
    
    BaseRSI = 50
    MultiplicatorMACD = 100000
    
    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(-BaseRSI+rsi, "RSI", color=#7E57C2)
    plot(-BaseRSI +rsiMA, "RSI-based MA", color=color.yellow)
    rsiUpperBand = hline(70-BaseRSI, "RSI Upper Band", color=#787B86)
    hline(50-BaseRSI, "RSI Middle Band", color=color.new(#787B86, 50))
    rsiLowerBand = hline(30-BaseRSI, "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")
    
    
    
    /////  2nd  indicator      //////
    
    
    // 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_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
    hline(0, "Zero Line", color=color.new(#787B86, 50))
    plot(hist*MultiplicatorMACD, 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)))