Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // https://www.investopedia.com/
- // There are a number of very clever people I have taken bits of code and ideas, thanks to you all :)
- // © raul3429
- //@version=4
- study(title="MACD+RSI+Flag v3.3 by RM", shorttitle="MACD+RSI+Flg v3.3 by RM", resolution="", overlay = false )
- // This indicator has line and the band changing color during oversold -green- /overbought -red- with Flags for first candle after direction change shown
- // shown as triangles also text is available with the flag. The MACD 12 and MACD 26 are shown with line values and their histogram differences. The crossings marked as diamonds.
- // RSI has been scaled down by the "scaleRSI" parameter to enable plotting alongside MACD.
- // User may need to adjust this scaleRSI as required to shown RSI & MACD in the proper combination.
- // RSI: The relative strength index (RSI) is a momentum indicator measures recent price changes to evaluate overbought or oversold conditions.
- // MACD: Moving average convergence divergence (MACD) is a trend-following momentum indicator that shows the relationship between
- // two moving averages of a security’s price. The MACD is = 12EMA - 26EMA, The histogram represent this difference.
- // Added divergence indicator for Price-RSI slope
- // Revised plots' scaleRSI input
- // Disclaimer: This is not a Financial advisory tool. For education purposes only. Use at your own risk.
- //Source
- src = input(title="Source", type=input.source, defval=close)
- // MACD inputs
- fast_length = input(title="MACD Fast Length", type=input.integer, defval=12)
- slow_length = input(title="MACD Slow Length", type=input.integer, defval=26)
- signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
- sma_source = input(title="Oscillator MA Type", type=input.string, defval="EMA", options=["SMA", "EMA"])
- sma_signal = input(title="Signal Line MA Type", type=input.string, defval="EMA", options=["SMA", "EMA"])
- // RSI inputs
- len = input(14, minval=1, title="RSI Length")
- osRSI = input(title="OverSold RSI", defval=30, minval = 1, maxval =100)
- obRSI = input(title="OverBought RSI", defval=70, minval = 1, maxval =100)
- scaleRSI = input(title="RSI Plot Scale Factor [%]", defval=10, minval = 1, maxval =100) / 100
- flagRSI = input(title="Flag and Text os/ob RSI", defval=true, type= input.bool)
- txtRSI = input(title="Show Text os/ob RSI", defval=false, type= input.bool)
- // Plot color inputs
- col_macd = #2962FF
- col_signal = #FF6D00
- col_grow_above = #26A69A
- col_fall_above = #B2DFDB
- col_grow_below = #FFCDD2
- col_fall_below = #FF5252
- //col_macd = input(#2962FF, "MACD Line ", input.color, group="Color Settings", inline="MACD")
- //col_signal = input(#FF6D00, "Signal Line ", input.color, group="Color Settings", inline="Signal")
- //col_grow_above = input(#26A69A, "Above Grow", input.color, group="Histogram", inline="Above")
- //col_fall_above = input(#B2DFDB, "Fall", input.color, group="Histogram", inline="Above")
- //col_grow_below = input(#FFCDD2, "Below Grow", input.color, group="Histogram", inline="Below")
- //col_fall_below = input(#FF5252, "Fall", input.color, group="Histogram", inline="Below")
- // colors
- c_white = #ffffff //White
- c_green = #006400 //Green
- c_green100 = #006400FF//Green 100%
- c_green90 = #00640090//Green 90%
- c_greenLight = #388e3c //Green Light
- c_red = #8B0000 //Red
- c_red100 = #8B0000FF//Red 100%
- c_red90 = #8B000090//Red 90%
- c_redLight = #b71c1c //Red Light
- c_orange = #f57f17 //Orange
- c_orange90 = #f57f1790 //Orange90%
- // MACD
- fast_ma = sma_source == "SMA" ? sma(src, fast_length) : ema(src, fast_length)
- slow_ma = sma_source == "SMA" ? sma(src, slow_length) : ema(src, slow_length)
- macd = fast_ma - slow_ma
- signal = sma_signal == "SMA" ? sma(macd, signal_length) : 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)
- plotshape(crossover (signal, macd) , title="McD CrossOver", style=shape.diamond, location=location.top, color=c_orange90, size=size.tiny)
- plotshape(crossunder (signal, macd), title="McD CrossUnder", style=shape.diamond, location=location.bottom, color=c_orange90, size=size.tiny)
- //RSI
- uprsi = rma(max(change(src), 0), len)
- downrsi = rma(-min(change(src), 0), len)
- rsi = downrsi == 0 ? 100 : uprsi == 0 ? 0 : 100 - (100 / (1 + uprsi / downrsi))
- f_rsiCond = rsi >= obRSI ? 1 : rsi <= osRSI ? -1 : 0
- f_rsiCol= f_rsiCond == -1 ? c_red :
- f_rsiCond == 1 ? c_green : c_white
- p2 =plot(rsi * scaleRSI , "RSI Scaled", color= f_rsiCol , linewidth= 1)
- pm =plot(50 * scaleRSI , "RSI Mid", color= f_rsiCol , linewidth= 1)
- fill(p2, pm, color=color.new(f_rsiCol, 85) , title="Background")
- // Trade signals per RSI
- pine_rsi_prev(x, y) =>
- u = max(x[1] - x[2], 0)
- d = max(x[2] - x[1], 0)
- rs = rma(u, y) / rma(d, y)
- res = 100 - 100 / (1 + rs)
- isRedCandle = close < open
- isGreenCandle = close > open
- prevCandleIsRed = close[1] < open[1]
- prevCandleIsGreen = open[1] < close[1]
- isRsiUnder_osRSI= pine_rsi_prev(close, 14) < 30
- isRsiOver_obRSI = pine_rsi_prev(close, 14) > 70
- enterCallTrade = isGreenCandle and prevCandleIsRed and isRsiUnder_osRSI and flagRSI
- enterPutTrade = isRedCandle and prevCandleIsGreen and isRsiOver_obRSI and flagRSI
- plotshape(enterCallTrade, title = "Buy Flag", text = " ", textcolor = c_white, color = c_green90, style = shape.triangleup, size = size.tiny, location = location.bottom , editable =false)
- plotshape(enterPutTrade, title= "Sell Flag", text = " ", textcolor = c_white, color = c_red90, style = shape.triangledown, size = size.tiny, location = location.top , editable =false)
- plotshape(enterCallTrade and txtRSI, title = "Buy Text", text = "OS Call" , textcolor = c_white, color = c_green90, style = shape.triangleup, size = size.tiny, location = location.bottom, editable =false)
- plotshape(enterPutTrade and txtRSI, title= "Sell Text", text = "OB Put" , textcolor = c_white, color = c_red90, style = shape.triangledown, size = size.tiny, location = location.top, editable =false)
- // Divergence Indicator
- //@version=4
- //study(title="Divergence Indicator", format=format.price, resolution="")
- lbR = input(title="Pivot Lookback Right", defval=5)
- lbL = input(title="Pivot Lookback Left", defval=5)
- rangeUpper = input(title="Max of Lookback Range", defval=60)
- rangeLower = input(title="Min of Lookback Range", defval=5)
- plotBull = input(title="Plot Bullish", defval=true)
- plotHiddenBull = input(title="Plot Hidden Bullish", defval=false)
- plotBear = input(title="Plot Bearish", defval=true)
- plotHiddenBear = input(title="Plot Hidden Bearish", defval=false)
- bearColor = c_red
- bullColor = c_green
- hiddenBullColor = color.new(c_green, 80)
- hiddenBearColor = color.new(c_red, 80)
- textColor = c_white
- noneColor = color.new(c_white, 100)
- osc = rsi(src, len)
- plot(osc * scaleRSI , title="RSI Do not show", linewidth=2, color=#2962FF, display = display.none)
- plFound = na(pivotlow(osc, lbL, lbR)) ? false : true
- phFound = na(pivothigh(osc, lbL, lbR)) ? false : true
- _inRange(cond) =>
- bars = barssince(cond == true)
- rangeLower <= bars and bars <= rangeUpper
- //------------------------------------------------------------------------------
- // Regular Bullish
- // Osc: Higher Low
- oscHL = osc[lbR] > valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
- // Price: Lower Low
- priceLL = low[lbR] < valuewhen(plFound, low[lbR], 1)
- bullCond = plotBull and priceLL and oscHL and plFound
- plot(
- plFound ? osc[lbR] * scaleRSI : na,
- offset=-lbR,
- title="Regular Bullish",
- linewidth=2,
- color=(bullCond ? bullColor : noneColor)
- )
- plotshape(
- bullCond ? osc[lbR] * scaleRSI : na,
- offset=-lbR,
- title="Regular Bullish Label",
- text=" Bull ",
- style=shape.labelup,
- location=location.absolute,
- color=bullColor,
- textcolor=textColor
- )
- //------------------------------------------------------------------------------
- // Hidden Bullish
- // Osc: Lower Low
- oscLL = osc[lbR] < valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])
- // Price: Higher Low
- priceHL = low[lbR] > valuewhen(plFound, low[lbR], 1)
- hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound
- plot(
- plFound ? osc[lbR] * scaleRSI : na,
- offset=-lbR,
- title="Hidden Bullish",
- linewidth=2,
- color=(hiddenBullCond ? hiddenBullColor : noneColor)
- )
- plotshape(
- hiddenBullCond ? osc[lbR] * scaleRSI : na,
- offset=-lbR,
- title="Hidden Bullish Label",
- text=" H Bull ",
- style=shape.labelup,
- location=location.absolute,
- color=bullColor,
- textcolor=textColor
- )
- //------------------------------------------------------------------------------
- // Regular Bearish
- // Osc: Lower High
- oscLH = osc[lbR] < valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
- // Price: Higher High
- priceHH = high[lbR] > valuewhen(phFound, high[lbR], 1)
- bearCond = plotBear and priceHH and oscLH and phFound
- plot(
- phFound ? osc[lbR] * scaleRSI : na,
- offset=-lbR,
- title="Regular Bearish",
- linewidth=2,
- color=(bearCond ? bearColor : noneColor)
- )
- plotshape(
- bearCond ? osc[lbR] * scaleRSI : na,
- offset=-lbR,
- title="Regular Bearish Label",
- text=" Bear ",
- style=shape.labeldown,
- location=location.absolute,
- color=bearColor,
- textcolor=textColor
- )
- //------------------------------------------------------------------------------
- // Hidden Bearish
- // Osc: Higher High
- oscHH = osc[lbR] > valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])
- // Price: Lower High
- priceLH = high[lbR] < valuewhen(phFound, high[lbR], 1)
- hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound
- plot(
- phFound ? osc[lbR] * scaleRSI : na,
- offset=-lbR,
- title="Hidden Bearish",
- linewidth=2,
- color=(hiddenBearCond ? hiddenBearColor : noneColor)
- )
- plotshape(
- hiddenBearCond ? osc[lbR] * scaleRSI : na,
- offset=-lbR,
- title="Hidden Bearish Label",
- text=" H Bear ",
- style=shape.labeldown,
- location=location.absolute,
- color=bearColor,
- textcolor=textColor
- )
Advertisement
Add Comment
Please, Sign In to add comment