OPEN-SOURCE SCRIPT

Givemepeace EMA + Market Structure

235
//version=6
indicator("Givemepeace EMA + Market Structure", overlay=true, max_lines_count=20, max_labels_count=50)

//Settings
length = input.int(21, "Swing Length", minval=5, maxval=50)
ema_length = input.int(55, "EMA Length", minval=1)
show_ema = input.bool(true, "Show EMA Lines")
show_swing = input.bool(true, "Show Swing Points")
show_fibo = input.bool(true, "Show Fibonacci Levels")
show_weekly = input.bool(true, "Show Weekly Levels")
show_monday = input.bool(true, "Show Monday High/Low")
show_prev_weekly = input.bool(true, "Show Previous Weekly Levels")

//Style
swinghCss = input.color(color.red, 'Swing High Color', group = 'Style')
swinglCss = input.color(color.teal, 'Swing Low Color', group = 'Style')

// EMA Lines
ema_15m = request.security(syminfo.tickerid, "15", ta.ema(close, ema_length))
ema_1h = request.security(syminfo.tickerid, "60", ta.ema(close, ema_length))
ema_4h = request.security(syminfo.tickerid, "240", ta.ema(close, ema_length))

// Plot EMA lines
plot(show_ema ? ema_15m : na, title="EMA 15m", color=color.purple, linewidth=1)
plot(show_ema ? ema_1h : na, title="EMA 1H", color=color.blue, linewidth=2)
plot(show_ema ? ema_4h : na, title="EMA 4H", color=color.red, linewidth=3)

// Weekly Levels
weekly_open = request.security(syminfo.tickerid, "1W", open, lookahead=barmerge.lookahead_on)
prev_weekly_open = request.security(syminfo.tickerid, "1W", open[1], lookahead=barmerge.lookahead_on)
prev_weekly_high = request.security(syminfo.tickerid, "1W", high[1], lookahead=barmerge.lookahead_on)
prev_weekly_low = request.security(syminfo.tickerid, "1W", low[1], lookahead=barmerge.lookahead_on)

// Monday High/Low (first day of week)
is_monday = dayofweek == dayofweek.monday
var float monday_high = na
var float monday_low = na

if is_monday and na(monday_high[1])
monday_high := high
monday_low := low
else if is_monday
monday_high := math.max(monday_high, high)
monday_low := math.min(monday_low, low)

// Reset Monday levels on new week
if ta.change(time("1W")) != 0
monday_high := na
monday_low := na

// Draw horizontal lines with labels (only latest ones)
var line wo_line = na
var line mh_line = na
var line ml_line = na
var line pwo_line = na
var line pwh_line = na
var line pwl_line = na
var label wo_label = na
var label mh_label = na
var label ml_label = na
var label pwo_label = na
var label pwh_label = na
var label pwl_label = na

if show_weekly and not na(weekly_open) and barstate.islast
if not na(wo_line)
line.delete(wo_line)
label.delete(wo_label)
wo_line := line.new(bar_index, weekly_open, bar_index + 10, weekly_open, color=color.black, width=1, style=line.style_solid)
wo_label := label.new(bar_index + 10, weekly_open, "WO", style=label.style_none, color=color(na), textcolor=color.black, size=size.small)

if show_monday and not na(monday_high) and barstate.islast
if not na(mh_line)
line.delete(mh_line)
label.delete(mh_label)
mh_line := line.new(bar_index, monday_high, bar_index + 10, monday_high, color=color.red, width=1, style=line.style_solid)
mh_label := label.new(bar_index + 10, monday_high, "MH", style=label.style_none, color=color(na), textcolor=color.red, size=size.small)

if show_monday and not na(monday_low) and barstate.islast
if not na(ml_line)
line.delete(ml_line)
label.delete(ml_label)
ml_line := line.new(bar_index, monday_low, bar_index + 10, monday_low, color=color.red, width=1, style=line.style_solid)
ml_label := label.new(bar_index + 10, monday_low, "ML", style=label.style_none, color=color(na), textcolor=color.red, size=size.small)

// Previous Weekly Levels
if show_prev_weekly and not na(prev_weekly_open) and barstate.islast
if not na(pwo_line)
line.delete(pwo_line)
label.delete(pwo_label)
pwo_line := line.new(bar_index, prev_weekly_open, bar_index + 10, prev_weekly_open, color=color.gray, width=1, style=line.style_dashed)
pwo_label := label.new(bar_index + 10, prev_weekly_open, "PWO", style=label.style_none, color=color(na), textcolor=color.gray, size=size.small)

if show_prev_weekly and not na(prev_weekly_high) and barstate.islast
if not na(pwh_line)
line.delete(pwh_line)
label.delete(pwh_label)
pwh_line := line.new(bar_index, prev_weekly_high, bar_index + 10, prev_weekly_high, color=color.gray, width=1, style=line.style_dashed)
pwh_label := label.new(bar_index + 10, prev_weekly_high, "PWH", style=label.style_none, color=color(na), textcolor=color.gray, size=size.small)

if show_prev_weekly and not na(prev_weekly_low) and barstate.islast
if not na(pwl_line)
line.delete(pwl_line)
label.delete(pwl_label)
pwl_line := line.new(bar_index, prev_weekly_low, bar_index + 10, prev_weekly_low, color=color.gray, width=1, style=line.style_dashed)
pwl_label := label.new(bar_index + 10, prev_weekly_low, "PWL", style=label.style_none, color=color(na), textcolor=color.gray, size=size.small)

// Swing Detection (LuxAlgo style)
var float phy = na // Previous high
var float ply = na // Previous low
var float last_hh = na // Last Higher High
var float last_hl = na // Last Higher Low
var int last_hh_bar = na
var int last_hl_bar = na

ph = ta.pivothigh(length, length)
pl = ta.pivotlow(length, length)
n = bar_index

// Process Swing Highs
if not na(ph)
H = ph > phy ? 'HH' : 'LH'

if show_swing
label.new(n[length], ph, H, color = color(na), style = label.style_label_down, textcolor = swinghCss, size = size.normal)

// Track HH for Fibonacci
if H == 'HH'
last_hh := ph
last_hh_bar := n[length]

phy := ph

// Process Swing Lows
if not na(pl)
L = pl < ply ? 'LL' : 'HL'

if show_swing
label.new(n[length], pl, L, color = color(na), style = label.style_label_up, textcolor = swinglCss, size = size.normal)

// Track HL and draw Fibonacci from last HH to this HL
if L == 'HL' and show_fibo and not na(last_hh)
last_hl := pl
last_hl_bar := n[length]

// Calculate Fibonacci levels from HH to HL
swing_range = last_hh - last_hl
fib_236 = last_hl + (swing_range * 0.236)
fib_382 = last_hl + (swing_range * 0.382)
fib_500 = last_hl + (swing_range * 0.500)
fib_618 = last_hl + (swing_range * 0.618)
fib_705 = last_hl + (swing_range * 0.705)
fib_786 = last_hl + (swing_range * 0.786)

// Draw Fibonacci lines from HH to future
start_bar = last_hh_bar
end_bar = bar_index + 30

// Draw key Fibonacci levels
line.new(start_bar, fib_236, end_bar, fib_236, color=color.gray, width=1, style=line.style_dotted)
line.new(start_bar, fib_382, end_bar, fib_382, color=color.blue, width=1, style=line.style_dashed)
line.new(start_bar, fib_500, end_bar, fib_500, color=color.purple, width=2, style=line.style_solid)
line.new(start_bar, fib_618, end_bar, fib_618, color=color.yellow, width=2, style=line.style_dashed)
line.new(start_bar, fib_705, end_bar, fib_705, color=color.orange, width=2, style=line.style_dashed)
line.new(start_bar, fib_786, end_bar, fib_786, color=color.red, width=1, style=line.style_dashed)

// Add Fibonacci labels
label.new(end_bar - 5, fib_236, "23.6", style=label.style_label_left, color=color.gray, textcolor=color.white, size=size.small)
label.new(end_bar - 5, fib_382, "38.2", style=label.style_label_left, color=color.blue, textcolor=color.white, size=size.small)
label.new(end_bar - 5, fib_500, "50.0", style=label.style_label_left, color=color.purple, textcolor=color.white, size=size.small)
label.new(end_bar - 5, fib_618, "61.8", style=label.style_label_left, color=color.yellow, textcolor=color.black, size=size.small)
label.new(end_bar - 5, fib_705, "70.5", style=label.style_label_left, color=color.orange, textcolor=color.black, size=size.small)
label.new(end_bar - 5, fib_786, "78.6", style=label.style_label_left, color=color.red, textcolor=color.white, size=size.small)

// Add info box
if barstate.islast
var table info_table = table.new(position.top_right, 2, 5, bgcolor=color.white, border_width=1)
table.cell(info_table, 0, 0, "Last HH:", text_color=color.black, text_size=size.small)
table.cell(info_table, 1, 0, str.tostring(last_hh, '#.####'), text_color=color.red, text_size=size.small)
table.cell(info_table, 0, 1, "Last HL:", text_color=color.black, text_size=size.small)
table.cell(info_table, 1, 1, str.tostring(last_hl, '#.####'), text_color=color.teal, text_size=size.small)
table.cell(info_table, 0, 2, "Fib Range:", text_color=color.black, text_size=size.small)
table.cell(info_table, 1, 2, str.tostring(swing_range, '#.####'), text_color=color.blue, text_size=size.small)
table.cell(info_table, 0, 3, "WO:", text_color=color.black, text_size=size.small)
table.cell(info_table, 1, 3, str.tostring(weekly_open, '#.####'), text_color=color.black, text_size=size.small)
table.cell(info_table, 0, 4, "Mon H/L:", text_color=color.black, text_size=size.small)
table.cell(info_table, 1, 4, str.tostring(monday_high, '#.##') + "/" + str.tostring(monday_low, '#.##'), text_color=color.red, text_size=size.small)

ply := pl

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.