Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

40
LINES

< > BotCompany Repo | #1036598 // MACD + Levels [Pine Script]

Document

// MACD plus 2 user-defined levels (horizontal lines)

//@version=5
indicator(title="MACD + levels", shorttitle="MACD + levels", timeframe="", timeframe_gaps=true)
// 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"])

// Levels to mark
level1 = input.float(0, "Level 1")
level2 = input.float(0, "Level 2")

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

level1Plot = plot(level1, title="Level 1", color=color.blue)
level2Plot = plot(level2, title="Level 2", color=color.blue)
//plot(level3, title="Level 3", color=color.blue)
//plot(level4, title="Level 4", color=color.blue)

fill(level1Plot, level2Plot, title="Level 1 to level 2", color=color.new(color.blue, 50))

Author comment

Began life as a copy of #1036597

download  show line numbers   

Travelled to 1 computer(s): mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036598
Snippet name: MACD + Levels [Pine Script]
Eternal ID of this version: #1036598/1
Text MD5: bd29039d2da23c8c1380a343535e385a
Author: stefan
Category: pine script
Type: Document
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-04-17 07:52:59
Source code size: 2143 bytes / 40 lines
Pitched / IR pitched: No / No
Views / Downloads: 123 / 30
Referenced in: [show references]