0% found this document useful (0 votes)
24 views8 pages

Rrrsi

Uploaded by

atharvg1612
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views8 pages

Rrrsi

Uploaded by

atharvg1612
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

//@version=6

indicator("RSI, Volume, MA Crossovers, MACD with Signals", overlay=true)

calculateDivergence = input.bool(false, title="Calculate Divergence", group="RSI Settings", display =


display.data_window, tooltip = "Calculating divergences is needed in order for divergence alerts to
fire.")

// Inputs

rsiLength = input.int(14, "RSI Length")

rsiUpperBand = input.int(70, "RSI Upper Band Level")

rsiMidLine = input.int(50, "RSI Middle Band Level")

rsiLowerBand = input.int(30, "RSI Lower Band Level")

maLength = input.int(200, "MA Length")

maType = input.string("SMA", "MA Type", options=["SMA", "EMA", "WMA"])

macdFastLength = input.int(12, "MACD Fast Length")

macdSlowLength = input.int(26, "MACD Slow Length")

macdSignalLength = input.int(9, "MACD Signal Length")

volumeLength = input.int(5, "Average Volume Length")

// RSI

rsi = ta.rsi(close, rsiLength)

rsiPlot=plot(rsi, "RSI", color=color.blue, linewidth=1)

rsiUB=hline(rsiUpperBand, "RSI Upper Band", color=color.red, linestyle=hline.style_dashed)

rsiMB=hline(rsiMidLine, "RSI Middle Band", color=#ffffff, linestyle=hline.style_dashed)

rsiLB=hline(rsiLowerBand, "RSI Lower Band", color=color.green, linestyle=hline.style_dashed)

fill(rsiUB, rsiLB, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")

midLinePlot = plot(50, color = na, editable = false, display = display.none)

fill(rsiPlot, midLinePlot, 100, 70, top_color = color.new(color.green, 0), bottom_color =


color.new(color.green, 100), title = "Overbought Gradient Fill")
fill(rsiPlot, midLinePlot, 30, 0, top_color = color.new(color.red, 100), bottom_color =
color.new(color.red, 0), title = "Oversold Gradient Fill")

// Smoothing MA inputs

GRP = "Moving Average"

TT_BB = "Only applies when 'SMA + Bollinger Bands' is selected. Determines the distance between
the SMA and the bands."

maTypeInput = input.string("SMA", "Type", options = ["None", "SMA", "SMA + Bollinger Bands",


"EMA", "SMMA (RMA)", "WMA", "VWMA"], group = GRP, display = display.data_window)

maLengthInput = input.int(14, "Length", group = GRP, display = display.data_window)

bbMultInput = input.float(2.0, "BB StdDev", minval = 0.001, maxval = 50, step = 0.5, tooltip = TT_BB,
group = GRP, display = display.data_window)

var enableMA = maTypeInput != "None"

var isBB = maTypeInput == "SMA + Bollinger Bands"

// Smoothing MA Calculation

ma(source, length, MAtype) =>

switch MAtype

"SMA" => ta.sma(source, length)

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

// Smoothing MA plots

smoothingMA = enableMA ? ma(rsi, maLengthInput, maTypeInput) : na

smoothingStDev = isBB ? ta.stdev(rsi, maLengthInput) * bbMultInput : na

plot(smoothingMA, "RSI-based MA", color=color.yellow, display = enableMA ? display.all :


display.none)

bbUpperBand = plot(smoothingMA + smoothingStDev, title = "Upper Bollinger Band",


color=color.green, display = isBB ? display.all : display.none)
bbLowerBand = plot(smoothingMA - smoothingStDev, title = "Lower Bollinger Band",
color=color.green, display = isBB ? display.all : display.none)

fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na, title="Bollinger Bands


Background Fill", display = isBB ? display.all : display.none)

// Divergence

lookbackRight = 5

lookbackLeft = 5

rangeUpper = 60

rangeLower = 5

bearColor = color.red

bullColor = color.green

textColor = color.white

noneColor = color.new(color.white, 100)

_inRange(bool cond) =>

bars = ta.barssince(cond)

rangeLower <= bars and bars <= rangeUpper

plFound = false

phFound = false

bullCond = false

bearCond = false

rsiLBR = rsi[lookbackRight]

if calculateDivergence

//------------------------------------------------------------------------------

// Regular Bullish

// rsi: Higher Low

plFound := not na(ta.pivotlow(rsi, lookbackLeft, lookbackRight))


rsiHL = rsiLBR > ta.valuewhen(plFound, rsiLBR, 1) and _inRange(plFound[1])

// Price: Lower Low

lowLBR = low[lookbackRight]

priceLL = lowLBR < ta.valuewhen(plFound, lowLBR, 1)

bullCond := priceLL and rsiHL and plFound

//------------------------------------------------------------------------------

// Regular Bearish

// rsi: Lower High

phFound := not na(ta.pivothigh(rsi, lookbackLeft, lookbackRight))

rsiLH = rsiLBR < ta.valuewhen(phFound, rsiLBR, 1) and _inRange(phFound[1])

// Price: Higher High

highLBR = high[lookbackRight]

priceHH = highLBR > ta.valuewhen(phFound, highLBR, 1)

bearCond := priceHH and rsiLH and phFound

plot(

plFound ? rsiLBR : na,

offset=-lookbackRight,

title="Regular Bullish",

linewidth=2,

color=(bullCond ? bullColor : noneColor),

display = display.pane

plotshape(

bullCond ? rsiLBR : na,

offset=-lookbackRight,

title="Regular Bullish Label",

text=" Bull ",

style=shape.labelup,
location=location.absolute,

color=bullColor,

textcolor=textColor

plot(

phFound ? rsiLBR : na,

offset=-lookbackRight,

title="Regular Bearish",

linewidth=2,

color=(bearCond ? bearColor : noneColor),

display = display.pane

plotshape(

bearCond ? rsiLBR : na,

offset=-lookbackRight,

title="Regular Bearish Label",

text=" Bear ",

style=shape.labeldown,

location=location.absolute,

color=bearColor,

textcolor=textColor

alertcondition(bullCond, title='Regular Bullish Divergence', message="Found a new Regular Bullish


Divergence, `Pivot Lookback Right` number of bars to the left of the current bar.")

alertcondition(bearCond, title='Regular Bearish Divergence', message='Found a new Regular Bearish


Divergence, `Pivot Lookback Right` number of bars to the left of the current bar.')

// Volume
volumeAvg = ta.sma(volume, volumeLength)

plot(volume, "Volume", color=color.purple,trackprice = true, style = plot.style_histogram,display =


display.price_scale,force_overlay = false)

plot(volumeAvg, "5-day Avg Volume", color=color.orange, trackprice = true, style =


plot.style_histogram,display = display.price_scale,force_overlay = false)

// Moving Average

ma = maType == "SMA" ? ta.sma(close, maLength) :

maType == "EMA" ? ta.ema(close, maLength) :

ta.wma(close, maLength)

plot(ma, "Moving Average", color=color.blue, linewidth=2)

// MA Crossovers

maShort = ta.ema(close, 20) // Example shorter MA for crossover

maLong = ta.ema(close, 50) // Example longer MA for crossover

plot(maShort, "Short MA (20)", color=color.green,linewidth=2)

plot(maLong, "Long MA (50)", color=color.red,linewidth=2)

crossoverSignal = ta.crossover(maShort, maLong)

crossunderSignal = ta.crossunder(maShort, maLong)

plotshape(crossoverSignal, style=shape.xcross, location=location.belowbar, color=color.green,


text="Buy",textcolor = #4caf50)

plotshape(crossunderSignal, style=shape.xcross, location=location.abovebar, color=color.red,


text="Sell",textcolor = #ff5252)

// MACD

[macdLine, signalLine, hist] = ta.macd(close, macdFastLength, macdSlowLength, macdSignalLength)

//plot(macdLine, "MACD Line", color=color.blue, linewidth=2)

//plot(signalLine, "Signal Line", color=color.orange, linewidth=2)


//histoColor = hist >= 0 ? color.green : color.red

//plot(hist, "MACD Histogram", color=histoColor, style=plot.style_histogram)

// MACD Buy/Sell Signals

macdBuySignal = ta.crossover(macdLine, signalLine)

macdSellSignal = ta.crossunder(macdLine, signalLine)

plotshape(macdBuySignal, style=shape.triangleup, location=location.belowbar, color=#4caf50,


size=size.small, text="MACD Buy", textcolor = #4caf50)

plotshape(macdSellSignal, style=shape.triangledown, location=location.abovebar, color=#ff5252,


size=size.small, text="MACD Sell", textcolor = #ff5252)

//plot(ta.crossover(ma1, ma2) ? ma1 : na, color=#4caf50, style = plot.style_cross, linewidth = 3,


title="crossover MA #1 & MA #2")

//plot(ta.crossunder(ma1, ma2) ? ma1 : na, color=#2962ff, style = plot.style_cross, linewidth = 3,


title="crossunder MA #1 & MA #2")

//plot(ta.crossover(ma3, ma4) ? ma3 : na, color=#ffeb3b, style = plot.style_cross, linewidth = 3,


title="crossover MA #3 & MA #4")

//plot(ta.crossunder(ma3, ma4) ? ma3 : na, color=#f48fb1, style = plot.style_cross, linewidth = 3,


title="crossunder MA #3 & MA #4")

//plot(ta.crossover(ma5, ma6) ? ma5 : na, color=#f57c00, style = plot.style_cross, linewidth = 3,


title="crossover MA #5 & MA #6")

//plot(ta.crossunder(ma5, ma6) ? ma5 : na, color=#f23645, style = plot.style_cross, linewidth = 3,


title="crossunder MA #5 & MA #6")

//

//plotchar(ta.crossover(ma1, ma2),title="cross_01_up (MA1 & MA2)", char="x",


location=location.bottom, color=ma1 > ma2 ? #4caf50 : #2962ff, text="Cross 01", textcolor=ma1 >
ma2 ? #4caf50 : #4caf50)

//plotchar(ta.crossunder(ma1, ma2),title="cross_01_down (MA1 & MA2)", char="x",


location=location.top, color=ma1 < ma2 ? #2962ff : #4caf50, text="Cross 01", textcolor=ma1 < ma2 ?
#2962ff : #2962ff)

//plotchar(ta.crossover(ma3, ma4),title="cross_02_up (MA3 & MA4)", char="x",


location=location.bottom, color=ma3 > ma4 ? #ffeb3b : #f48fb1, text="Cross 02", textcolor=ma3 >
ma4 ? #ffeb3b : #ffeb3b)

//plotchar(ta.crossunder(ma3, ma4),title="cross_02_down (MA3 & MA4)", char="x",


location=location.top, color=ma3 < ma4 ? #f48fb1 : #ffeb3b, text="Cross 02", textcolor=ma3 < ma4 ?
#f48fb1 : #f48fb1)
//plotchar(ta.crossover(ma5, ma6),title="cross_03_up (MA5 & MA6)", char="x",
location=location.bottom, color=ma5 > ma6 ? #f57c00 : #f23645, text="Cross 03", textcolor=ma5 >
ma6 ? #f57c00 : #f57c00)

//plotchar(ta.crossunder(ma5, ma6),title="cross_03_down (MA5 & MA6)", char="x",


location=location.top, color=ma5 < ma6 ? #f23645 : #f57c00, text="Cross 03", textcolor=ma5 < ma6 ?
#f23645 : #f23645)

You might also like