Skip to content

Instantly share code, notes, and snippets.

@siralam
Last active May 22, 2024 03:34
Show Gist options
  • Save siralam/4e082c86c34dfb73385ecc3c4adf74cc to your computer and use it in GitHub Desktop.
Save siralam/4e082c86c34dfb73385ecc3c4adf74cc to your computer and use it in GitHub Desktop.
TradingView: ATR + RSI + MACD
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © szewa1992
//@version=4
study("MACD + ATR + RSI", resolution="", overlay=false)
displayATR = input(title="Display ATR?", type=input.bool, defval=true)
displayRSI = input(title="Display RSI?", type=input.bool, defval=false)
displayMACD = input(title="Display MACD?", type=input.bool, defval=true)
src = input(title="Source", type=input.source, defval=close)
//------------ATR-----------
length = input(title="ATR Look back period", defval=10, minval=1)
smoothing = input(title="ATR Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
ma_function(source, length) =>
if smoothing == "RMA"
rma(source, length)
else
if smoothing == "SMA"
sma(source, length)
else
if smoothing == "EMA"
ema(source, length)
else
wma(source, length)
plot(displayATR ? ma_function(tr(true), length) : na, title = "ATR", color=#991515, transp=0)
//---------RSI--------
//RSI value can be too high that squeeze the chart. So it will be divided by 10 here.
len = input(14, minval=1, title="RSI Period")
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(displayRSI ? rsi:na, "RSI", color=#8E1599, transp=40)
boundaryColor = color.new(#8E1599, 60)
band1 = hline(displayRSI?70:na, "RSI Upper Band", color=boundaryColor)
band0 = hline(displayRSI?30:na, "RSI Lower Band", color=boundaryColor)
bandMid = hline(displayRSI?50:na, "RSI Mid Band", color=boundaryColor)
fill(band1, band0, color=#9915FF, transp=95, title="RSI Background")
//--------------MACD--------------
// Getting 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="MACD Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
sma_source = input(title="MACD Simple MA(Oscillator)", type=input.bool, defval=false)
sma_signal = input(title="MACD Simple MA(Signal Line)", type=input.bool, defval=false)
// Plot colors
col_grow_above = #26A69A
col_grow_below = #FFCDD2
col_fall_above = #B2DFDB
col_fall_below = #EF5350
col_macd = #0094ff
col_signal = #ff6a00
// Calculating
fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal
plot(displayMACD?hist:na, title="MACD 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) ), transp=0 )
plot(displayMACD?macd:na, title="MACD", color=col_macd, transp=0)
plot(displayMACD?signal:na, title="MACD Signal", color=col_signal, transp=0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment