OPEN-SOURCE SCRIPT
EMA Market Structure [BOSWaves]

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at mozilla.org/MPL/2.0/
// Join our channel for more free tools: t.me/simpleforextools
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at mozilla.org/MPL/2.0/
// © BOSWaves
//version=6
indicator("EMA Market Structure [BOSWaves]", overlay=true, max_lines_count=500, max_labels_count=500, max_boxes_count=500)
// ============================================================================
// Inputs
// ============================================================================
// Ema settings
emaLength = input.int(50, "EMA Length", minval=1, tooltip="Period for the Exponential Moving Average calculation")
emaSource = input.source(close, "EMA Source", tooltip="Price source for EMA calculation (close, open, high, low, etc.)")
colorSmooth = input.int(3, "Color Smoothing", minval=1, group="EMA Style", tooltip="Smoothing period for the EMA color gradient transition")
showEmaGlow = input.bool(true, "EMA Glow Effect", group="EMA Style", tooltip="Display glowing halo effect around the EMA line for enhanced visibility")
// Structure settings
swingLength = input.int(5, "Swing Detection Length", minval=2, group="Structure", tooltip="Number of bars to the left and right to identify swing highs and lows")
swingCooloff = input.int(10, "Swing Marker Cooloff (Bars)", minval=1, group="Structure", tooltip="Minimum number of bars between consecutive swing point markers to reduce visual clutter")
showSwingLines = input.bool(true, "Show Structure Lines", group="Structure", tooltip="Display lines connecting swing highs and swing lows")
showSwingZones = input.bool(true, "Show Structure Zones", group="Structure", tooltip="Display shaded zones between consecutive swing points")
showBOS = input.bool(true, "Show Break of Structure", group="Structure", tooltip="Display BOS labels and stop loss levels when price breaks structure")
bosCooloff = input.int(15, "BOS Cooloff (Bars)", minval=5, maxval=50, group="Structure", tooltip="Minimum number of bars required between consecutive BOS signals to avoid signal spam")
slExtension = input.int(20, "SL Line Extension (Bars)", minval=5, maxval=100, group="Structure", tooltip="Number of bars to extend the stop loss line into the future for visibility")
slBuffer = input.float(0.1, "SL Buffer %", minval=0, maxval=2, step=0.05, group="Structure", tooltip="Additional buffer percentage to add to stop loss level for safety margin")
// Background settings
showBG = input.bool(true, "Show Trend Background", group="EMA Style", tooltip="Display background color based on EMA trend direction")
bgBullColor = input.color(color.new(#00ff88, 96), "Bullish BG", group="EMA Style", tooltip="Background color when EMA is in bullish trend")
bgBearColor = input.color(color.new(#ff3366, 96), "Bearish BG", group="EMA Style", tooltip="Background color when EMA is in bearish trend")
// ============================================================================
// Ema trend filter with gradient color
// ============================================================================
ema = ta.ema(emaSource, emaLength)
// Calculate EMA acceleration for gradient color
emaChange = ema - ema[1]
emaAccel = ta.ema(emaChange, colorSmooth)
// Manual tanh function for normalization
tanh(x) =>
ex = math.exp(2 * x)
(ex - 1) / (ex + 1)
accelNorm = tanh(emaAccel / (ta.atr(14) * 0.01))
// Map normalized accel to hue (60 = green, 120 = yellow/red)
hueRaw = 60 + accelNorm * 60
hue = na(hueRaw[1]) ? hueRaw : (hueRaw + hueRaw[1]) / 2
sat = 1.0
val = 1.0
// HSV to RGB conversion
hsv_to_rgb(h, s, v) =>
c = v * s
x = c * (1 - math.abs((h / 60) % 2 - 1))
m = v - c
r = 0.0
g = 0.0
b = 0.0
if (h < 60)
r := c
g := x
b := 0
else if (h < 120)
r := x
g := c
b := 0
else if (h < 180)
r := 0
g := c
b := x
else if (h < 240)
r := 0
g := x
b := c
else if (h < 300)
r := x
g := 0
b := c
else
r := c
g := 0
b := x
color.rgb(int((r + m) * 255), int((g + m) * 255), int((b + m) * 255))
emaColor = hsv_to_rgb(hue, sat, val)
emaTrend = ema > ema[1] ? 1 : ema < ema[1] ? -1 : 0
// EMA with enhanced glow effect using fills
glowOffset = ta.atr(14) * 0.25
emaGlow8 = plot(showEmaGlow ? ema + glowOffset * 8 : na, "EMA Glow 8", color.new(emaColor, 100), 1, display=display.none)
emaGlow7 = plot(showEmaGlow ? ema + glowOffset * 7 : na, "EMA Glow 7", color.new(emaColor, 100), 1, display=display.none)
emaGlow6 = plot(showEmaGlow ? ema + glowOffset * 6 : na, "EMA Glow 6", color.new(emaColor, 100), 1, display=display.none)
emaGlow5 = plot(showEmaGlow ? ema + glowOffset * 5 : na, "EMA Glow 5", color.new(emaColor, 100), 1, display=display.none)
emaGlow4 = plot(showEmaGlow ? ema + glowOffset * 4 : na, "EMA Glow 4", color.new(emaColor, 100), 1, display=display.none)
emaGlow3 = plot(showEmaGlow ? ema + glowOffset * 3 : na, "EMA Glow 3", color.new(emaColor, 100), 1, display=display.none)
emaGlow2 = plot(showEmaGlow ? ema + glowOffset * 2 : na, "EMA Glow 2", color.new(emaColor, 100), 1, display=display.none)
emaGlow1 = plot(showEmaGlow ? ema + glowOffset * 1 : na, "EMA Glow 1", color.new(emaColor, 100), 1, display=display.none)
emaCore = plot(ema, "EMA Core", emaColor, 3)
emaGlow1b = plot(showEmaGlow ? ema - glowOffset * 1 : na, "EMA Glow 1b", color.new(emaColor, 100), 1, display=display.none)
emaGlow2b = plot(showEmaGlow ? ema - glowOffset * 2 : na, "EMA Glow 2b", color.new(emaColor, 100), 1, display=display.none)
emaGlow3b = plot(showEmaGlow ? ema - glowOffset * 3 : na, "EMA Glow 3b", color.new(emaColor, 100), 1, display=display.none)
emaGlow4b = plot(showEmaGlow ? ema - glowOffset * 4 : na, "EMA Glow 4b", color.new(emaColor, 100), 1, display=display.none)
emaGlow5b = plot(showEmaGlow ? ema - glowOffset * 5 : na, "EMA Glow 5b", color.new(emaColor, 100), 1, display=display.none)
emaGlow6b = plot(showEmaGlow ? ema - glowOffset * 6 : na, "EMA Glow 6b", color.new(emaColor, 100), 1, display=display.none)
emaGlow7b = plot(showEmaGlow ? ema - glowOffset * 7 : na, "EMA Glow 7b", color.new(emaColor, 100), 1, display=display.none)
emaGlow8b = plot(showEmaGlow ? ema - glowOffset * 8 : na, "EMA Glow 8b", color.new(emaColor, 100), 1, display=display.none)
// Create glow layers with fills (from outermost to innermost)
fill(emaGlow8, emaGlow7, showEmaGlow ? color.new(emaColor, 97) : na)
fill(emaGlow7, emaGlow6, showEmaGlow ? color.new(emaColor, 95) : na)
fill(emaGlow6, emaGlow5, showEmaGlow ? color.new(emaColor, 93) : na)
fill(emaGlow5, emaGlow4, showEmaGlow ? color.new(emaColor, 90) : na)
fill(emaGlow4, emaGlow3, showEmaGlow ? color.new(emaColor, 87) : na)
fill(emaGlow3, emaGlow2, showEmaGlow ? color.new(emaColor, 83) : na)
fill(emaGlow2, emaGlow1, showEmaGlow ? color.new(emaColor, 78) : na)
fill(emaGlow1, emaCore, showEmaGlow ? color.new(emaColor, 70) : na)
fill(emaCore, emaGlow1b, showEmaGlow ? color.new(emaColor, 70) : na)
fill(emaGlow1b, emaGlow2b, showEmaGlow ? color.new(emaColor, 78) : na)
fill(emaGlow2b, emaGlow3b, showEmaGlow ? color.new(emaColor, 83) : na)
fill(emaGlow3b, emaGlow4b, showEmaGlow ? color.new(emaColor, 87) : na)
fill(emaGlow4b, emaGlow5b, showEmaGlow ? color.new(emaColor, 90) : na)
fill(emaGlow5b, emaGlow6b, showEmaGlow ? color.new(emaColor, 93) : na)
fill(emaGlow6b, emaGlow7b, showEmaGlow ? color.new(emaColor, 95) : na)
fill(emaGlow7b, emaGlow8b, showEmaGlow ? color.new(emaColor, 97) : na)
// ============================================================================
// Swing high/low detection
// ============================================================================
// Swing High/Low Detection
swingHigh = ta.pivothigh(high, swingLength, swingLength)
swingLow = ta.pivotlow(low, swingLength, swingLength)
// Cooloff tracking
var int lastSwingHighPlot = na
var int lastSwingLowPlot = na
// Check if cooloff period has passed
canPlotHigh = na(lastSwingHighPlot) or (bar_index - lastSwingHighPlot) >= swingCooloff
canPlotLow = na(lastSwingLowPlot) or (bar_index - lastSwingLowPlot) >= swingCooloff
// Store swing points
var float lastSwingHigh = na
var int lastSwingHighBar = na
var float lastSwingLow = na
var int lastSwingLowBar = na
// Track previous swing for BOS detection
var float prevSwingHigh = na
var float prevSwingLow = na
// Update swing highs with cooloff
if not na(swingHigh) and canPlotHigh
prevSwingHigh := lastSwingHigh
lastSwingHigh := swingHigh
lastSwingHighBar := bar_index - swingLength
lastSwingHighPlot := bar_index
// Update swing lows with cooloff
if not na(swingLow) and canPlotLow
prevSwingLow := lastSwingLow
lastSwingLow := swingLow
lastSwingLowBar := bar_index - swingLength
lastSwingLowPlot := bar_index
// ============================================================================
// Structure lines & zones
// ============================================================================
var line swingHighLine = na
var line swingLowLine = na
var box swingHighZone = na
var box swingLowZone = na
if showSwingLines
// Draw line connecting swing highs with zones
if not na(swingHigh) and canPlotHigh and not na(prevSwingHigh)
if not na(lastSwingHighBar)
line.delete(swingHighLine)
swingHighLine := line.new(lastSwingHighBar, lastSwingHigh, bar_index - swingLength, swingHigh, color=color.new(#ff3366, 0), width=2, style=line.style_solid)
// Create resistance zone
if showSwingZones
box.delete(swingHighZone)
zoneTop = math.max(lastSwingHigh, swingHigh)
zoneBottom = math.min(lastSwingHigh, swingHigh)
swingHighZone := box.new(lastSwingHighBar, zoneTop, bar_index - swingLength, zoneBottom, border_color=color.new(#ff3366, 80), bgcolor=color.new(#ff3366, 92))
// Draw line connecting swing lows with zones
if not na(swingLow) and canPlotLow and not na(prevSwingLow)
if not na(lastSwingLowBar)
line.delete(swingLowLine)
swingLowLine := line.new(lastSwingLowBar, lastSwingLow, bar_index - swingLength, swingLow, color=color.new(#00ff88, 0), width=2, style=line.style_solid)
// Create support zone
if showSwingZones
box.delete(swingLowZone)
zoneTop = math.max(lastSwingLow, swingLow)
zoneBottom = math.min(lastSwingLow, swingLow)
swingLowZone := box.new(lastSwingLowBar, zoneTop, bar_index - swingLength, zoneBottom, border_color=color.new(#00ff88, 80), bgcolor=color.new(#00ff88, 92))
// ============================================================================
// Break of structure (bos)
// ============================================================================
// Track last BOS bar for cooloff
var int lastBullishBOS = na
var int lastBearishBOS = na
// Check if cooloff period has passed
canPlotBullishBOS = na(lastBullishBOS) or (bar_index - lastBullishBOS) >= bosCooloff
canPlotBearishBOS = na(lastBearishBOS) or (bar_index - lastBearishBOS) >= bosCooloff
// Bullish BOS: Price breaks above previous swing high while EMA is bullish
bullishBOS = showBOS and canPlotBullishBOS and emaTrend == 1 and not na(prevSwingHigh) and close > prevSwingHigh and close[1] <= prevSwingHigh
// Bearish BOS: Price breaks below previous swing low while EMA is bearish
bearishBOS = showBOS and canPlotBearishBOS and emaTrend == -1 and not na(prevSwingLow) and close < prevSwingLow and close[1] >= prevSwingLow
// Update last BOS bars
if bullishBOS
lastBullishBOS := bar_index
if bearishBOS
lastBearishBOS := bar_index
// Plot BOS with enhanced visuals and SL at the candle wick
if bullishBOS
// Calculate SL at the low of the current candle (bottom of wick) with buffer
slLevel = low * (1 - slBuffer/100)
// BOS Label with shadow effect
label.new(bar_index, low, "BOS", style=label.style_label_up, color=color.new(#00ff88, 0), textcolor=color.black, size=size.normal, tooltip="Bullish Break of Structure\nSL: " + str.tostring(slLevel))
// Main SL line at candle low
line.new(bar_index, slLevel, bar_index + slExtension, slLevel, color=color.new(#00ff88, 0), width=2, style=line.style_dashed, extend=extend.none)
// SL zone box for visual emphasis
box.new(bar_index, slLevel + (slLevel * 0.002), bar_index + slExtension, slLevel - (slLevel * 0.002), border_color=color.new(#00ff88, 60), bgcolor=color.new(#00ff88, 85))
// S/R label
label.new(bar_index + slExtension, slLevel, "S/R", style=label.style_label_left, color=color.new(#00ff88, 0), textcolor=color.black, size=size.tiny)
if bearishBOS
// Calculate SL at the high of the current candle (top of wick) with buffer
slLevel = high * (1 + slBuffer/100)
// BOS Label with shadow effect
label.new(bar_index, high, "BOS", style=label.style_label_down, color=color.new(#ff3366, 0), textcolor=color.white, size=size.normal, tooltip="Bearish Break of Structure\nSL: " + str.tostring(slLevel))
// Main SL line at candle high
line.new(bar_index, slLevel, bar_index + slExtension, slLevel, color=color.new(#ff3366, 0), width=2, style=line.style_dashed, extend=extend.none)
// SL zone box for visual emphasis
box.new(bar_index, slLevel + (slLevel * 0.002), bar_index + slExtension, slLevel - (slLevel * 0.002), border_color=color.new(#ff3366, 60), bgcolor=color.new(#ff3366, 85))
// S/R label
label.new(bar_index + slExtension, slLevel, "S/R", style=label.style_label_left, color=color.new(#ff3366, 0), textcolor=color.white, size=size.tiny)
// ============================================================================
// Dynamic background zones
// ============================================================================
bgcolor(showBG and emaTrend == 1 ? bgBullColor : showBG and emaTrend == -1 ? bgBearColor : na)
// ============================================================================
// Alerts
// ============================================================================
alertcondition(bullishBOS, "Bullish BOS", "Bullish Break of Structure detected!")
alertcondition(bearishBOS, "Bearish BOS", "Bearish Break of Structure detected!")
alertcondition(emaTrend == 1 and emaTrend[1] != 1, "EMA Bullish", "EMA turned bullish")
alertcondition(emaTrend == -1 and emaTrend[1] != -1, "EMA Bearish", "EMA turned bearish")
// ╔════════════════════════════════╗
// ║ Download at ║
// ╚════════════════════════════════╝
// ███████╗██╗███╗ ███╗██████╗ ██╗ ███████╗
// ██╔════╝██║████╗ ████║██╔══██╗██║ ██╔════╝
// ███████╗██║██╔████╔██║██████╔╝██║ █████╗
// ╚════██║██║██║╚██╔╝██║██╔═══╝ ██║ ██╔══╝
// ███████║██║██║ ╚═╝ ██║██║ ███████╗███████╗
// ╚══════╝╚═╝╚═╝ ╚═╝╚═╝ ╚══════╝╚══════╝
// ███████╗ ██████╗ ██████╗ ███████╗██╗ ██╗
// ██╔════╝██╔═══██╗██╔══██╗██╔════╝╚██╗██╔╝
// █████╗ ██║ ██║██████╔╝█████╗ ╚███╔╝
// ██╔══╝ ██║ ██║██╔══██╗██╔══╝ ██╔██╗
// ██║ ╚██████╔╝██║ ██║███████╗██╔╝ ██╗
// ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
// ████████╗ ██████╗ ██████╗ ██╗ ███████╗
// ╚══██╔══╝██╔═══██╗██╔═══██╗██║ ██╔════╝
// ██║ ██║ ██║██║ ██║██║ ███████╗
// ██║ ██║ ██║██║ ██║██║ ╚════██║
// ██║ ╚██████╔╝╚██████╔╝███████╗███████║
// ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝╚══════╝
// ==========================================================================================
// Join our channel for more free tools: t.me/simpleforextools
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at mozilla.org/MPL/2.0/
// © BOSWaves
//version=6
indicator("EMA Market Structure [BOSWaves]", overlay=true, max_lines_count=500, max_labels_count=500, max_boxes_count=500)
// ============================================================================
// Inputs
// ============================================================================
// Ema settings
emaLength = input.int(50, "EMA Length", minval=1, tooltip="Period for the Exponential Moving Average calculation")
emaSource = input.source(close, "EMA Source", tooltip="Price source for EMA calculation (close, open, high, low, etc.)")
colorSmooth = input.int(3, "Color Smoothing", minval=1, group="EMA Style", tooltip="Smoothing period for the EMA color gradient transition")
showEmaGlow = input.bool(true, "EMA Glow Effect", group="EMA Style", tooltip="Display glowing halo effect around the EMA line for enhanced visibility")
// Structure settings
swingLength = input.int(5, "Swing Detection Length", minval=2, group="Structure", tooltip="Number of bars to the left and right to identify swing highs and lows")
swingCooloff = input.int(10, "Swing Marker Cooloff (Bars)", minval=1, group="Structure", tooltip="Minimum number of bars between consecutive swing point markers to reduce visual clutter")
showSwingLines = input.bool(true, "Show Structure Lines", group="Structure", tooltip="Display lines connecting swing highs and swing lows")
showSwingZones = input.bool(true, "Show Structure Zones", group="Structure", tooltip="Display shaded zones between consecutive swing points")
showBOS = input.bool(true, "Show Break of Structure", group="Structure", tooltip="Display BOS labels and stop loss levels when price breaks structure")
bosCooloff = input.int(15, "BOS Cooloff (Bars)", minval=5, maxval=50, group="Structure", tooltip="Minimum number of bars required between consecutive BOS signals to avoid signal spam")
slExtension = input.int(20, "SL Line Extension (Bars)", minval=5, maxval=100, group="Structure", tooltip="Number of bars to extend the stop loss line into the future for visibility")
slBuffer = input.float(0.1, "SL Buffer %", minval=0, maxval=2, step=0.05, group="Structure", tooltip="Additional buffer percentage to add to stop loss level for safety margin")
// Background settings
showBG = input.bool(true, "Show Trend Background", group="EMA Style", tooltip="Display background color based on EMA trend direction")
bgBullColor = input.color(color.new(#00ff88, 96), "Bullish BG", group="EMA Style", tooltip="Background color when EMA is in bullish trend")
bgBearColor = input.color(color.new(#ff3366, 96), "Bearish BG", group="EMA Style", tooltip="Background color when EMA is in bearish trend")
// ============================================================================
// Ema trend filter with gradient color
// ============================================================================
ema = ta.ema(emaSource, emaLength)
// Calculate EMA acceleration for gradient color
emaChange = ema - ema[1]
emaAccel = ta.ema(emaChange, colorSmooth)
// Manual tanh function for normalization
tanh(x) =>
ex = math.exp(2 * x)
(ex - 1) / (ex + 1)
accelNorm = tanh(emaAccel / (ta.atr(14) * 0.01))
// Map normalized accel to hue (60 = green, 120 = yellow/red)
hueRaw = 60 + accelNorm * 60
hue = na(hueRaw[1]) ? hueRaw : (hueRaw + hueRaw[1]) / 2
sat = 1.0
val = 1.0
// HSV to RGB conversion
hsv_to_rgb(h, s, v) =>
c = v * s
x = c * (1 - math.abs((h / 60) % 2 - 1))
m = v - c
r = 0.0
g = 0.0
b = 0.0
if (h < 60)
r := c
g := x
b := 0
else if (h < 120)
r := x
g := c
b := 0
else if (h < 180)
r := 0
g := c
b := x
else if (h < 240)
r := 0
g := x
b := c
else if (h < 300)
r := x
g := 0
b := c
else
r := c
g := 0
b := x
color.rgb(int((r + m) * 255), int((g + m) * 255), int((b + m) * 255))
emaColor = hsv_to_rgb(hue, sat, val)
emaTrend = ema > ema[1] ? 1 : ema < ema[1] ? -1 : 0
// EMA with enhanced glow effect using fills
glowOffset = ta.atr(14) * 0.25
emaGlow8 = plot(showEmaGlow ? ema + glowOffset * 8 : na, "EMA Glow 8", color.new(emaColor, 100), 1, display=display.none)
emaGlow7 = plot(showEmaGlow ? ema + glowOffset * 7 : na, "EMA Glow 7", color.new(emaColor, 100), 1, display=display.none)
emaGlow6 = plot(showEmaGlow ? ema + glowOffset * 6 : na, "EMA Glow 6", color.new(emaColor, 100), 1, display=display.none)
emaGlow5 = plot(showEmaGlow ? ema + glowOffset * 5 : na, "EMA Glow 5", color.new(emaColor, 100), 1, display=display.none)
emaGlow4 = plot(showEmaGlow ? ema + glowOffset * 4 : na, "EMA Glow 4", color.new(emaColor, 100), 1, display=display.none)
emaGlow3 = plot(showEmaGlow ? ema + glowOffset * 3 : na, "EMA Glow 3", color.new(emaColor, 100), 1, display=display.none)
emaGlow2 = plot(showEmaGlow ? ema + glowOffset * 2 : na, "EMA Glow 2", color.new(emaColor, 100), 1, display=display.none)
emaGlow1 = plot(showEmaGlow ? ema + glowOffset * 1 : na, "EMA Glow 1", color.new(emaColor, 100), 1, display=display.none)
emaCore = plot(ema, "EMA Core", emaColor, 3)
emaGlow1b = plot(showEmaGlow ? ema - glowOffset * 1 : na, "EMA Glow 1b", color.new(emaColor, 100), 1, display=display.none)
emaGlow2b = plot(showEmaGlow ? ema - glowOffset * 2 : na, "EMA Glow 2b", color.new(emaColor, 100), 1, display=display.none)
emaGlow3b = plot(showEmaGlow ? ema - glowOffset * 3 : na, "EMA Glow 3b", color.new(emaColor, 100), 1, display=display.none)
emaGlow4b = plot(showEmaGlow ? ema - glowOffset * 4 : na, "EMA Glow 4b", color.new(emaColor, 100), 1, display=display.none)
emaGlow5b = plot(showEmaGlow ? ema - glowOffset * 5 : na, "EMA Glow 5b", color.new(emaColor, 100), 1, display=display.none)
emaGlow6b = plot(showEmaGlow ? ema - glowOffset * 6 : na, "EMA Glow 6b", color.new(emaColor, 100), 1, display=display.none)
emaGlow7b = plot(showEmaGlow ? ema - glowOffset * 7 : na, "EMA Glow 7b", color.new(emaColor, 100), 1, display=display.none)
emaGlow8b = plot(showEmaGlow ? ema - glowOffset * 8 : na, "EMA Glow 8b", color.new(emaColor, 100), 1, display=display.none)
// Create glow layers with fills (from outermost to innermost)
fill(emaGlow8, emaGlow7, showEmaGlow ? color.new(emaColor, 97) : na)
fill(emaGlow7, emaGlow6, showEmaGlow ? color.new(emaColor, 95) : na)
fill(emaGlow6, emaGlow5, showEmaGlow ? color.new(emaColor, 93) : na)
fill(emaGlow5, emaGlow4, showEmaGlow ? color.new(emaColor, 90) : na)
fill(emaGlow4, emaGlow3, showEmaGlow ? color.new(emaColor, 87) : na)
fill(emaGlow3, emaGlow2, showEmaGlow ? color.new(emaColor, 83) : na)
fill(emaGlow2, emaGlow1, showEmaGlow ? color.new(emaColor, 78) : na)
fill(emaGlow1, emaCore, showEmaGlow ? color.new(emaColor, 70) : na)
fill(emaCore, emaGlow1b, showEmaGlow ? color.new(emaColor, 70) : na)
fill(emaGlow1b, emaGlow2b, showEmaGlow ? color.new(emaColor, 78) : na)
fill(emaGlow2b, emaGlow3b, showEmaGlow ? color.new(emaColor, 83) : na)
fill(emaGlow3b, emaGlow4b, showEmaGlow ? color.new(emaColor, 87) : na)
fill(emaGlow4b, emaGlow5b, showEmaGlow ? color.new(emaColor, 90) : na)
fill(emaGlow5b, emaGlow6b, showEmaGlow ? color.new(emaColor, 93) : na)
fill(emaGlow6b, emaGlow7b, showEmaGlow ? color.new(emaColor, 95) : na)
fill(emaGlow7b, emaGlow8b, showEmaGlow ? color.new(emaColor, 97) : na)
// ============================================================================
// Swing high/low detection
// ============================================================================
// Swing High/Low Detection
swingHigh = ta.pivothigh(high, swingLength, swingLength)
swingLow = ta.pivotlow(low, swingLength, swingLength)
// Cooloff tracking
var int lastSwingHighPlot = na
var int lastSwingLowPlot = na
// Check if cooloff period has passed
canPlotHigh = na(lastSwingHighPlot) or (bar_index - lastSwingHighPlot) >= swingCooloff
canPlotLow = na(lastSwingLowPlot) or (bar_index - lastSwingLowPlot) >= swingCooloff
// Store swing points
var float lastSwingHigh = na
var int lastSwingHighBar = na
var float lastSwingLow = na
var int lastSwingLowBar = na
// Track previous swing for BOS detection
var float prevSwingHigh = na
var float prevSwingLow = na
// Update swing highs with cooloff
if not na(swingHigh) and canPlotHigh
prevSwingHigh := lastSwingHigh
lastSwingHigh := swingHigh
lastSwingHighBar := bar_index - swingLength
lastSwingHighPlot := bar_index
// Update swing lows with cooloff
if not na(swingLow) and canPlotLow
prevSwingLow := lastSwingLow
lastSwingLow := swingLow
lastSwingLowBar := bar_index - swingLength
lastSwingLowPlot := bar_index
// ============================================================================
// Structure lines & zones
// ============================================================================
var line swingHighLine = na
var line swingLowLine = na
var box swingHighZone = na
var box swingLowZone = na
if showSwingLines
// Draw line connecting swing highs with zones
if not na(swingHigh) and canPlotHigh and not na(prevSwingHigh)
if not na(lastSwingHighBar)
line.delete(swingHighLine)
swingHighLine := line.new(lastSwingHighBar, lastSwingHigh, bar_index - swingLength, swingHigh, color=color.new(#ff3366, 0), width=2, style=line.style_solid)
// Create resistance zone
if showSwingZones
box.delete(swingHighZone)
zoneTop = math.max(lastSwingHigh, swingHigh)
zoneBottom = math.min(lastSwingHigh, swingHigh)
swingHighZone := box.new(lastSwingHighBar, zoneTop, bar_index - swingLength, zoneBottom, border_color=color.new(#ff3366, 80), bgcolor=color.new(#ff3366, 92))
// Draw line connecting swing lows with zones
if not na(swingLow) and canPlotLow and not na(prevSwingLow)
if not na(lastSwingLowBar)
line.delete(swingLowLine)
swingLowLine := line.new(lastSwingLowBar, lastSwingLow, bar_index - swingLength, swingLow, color=color.new(#00ff88, 0), width=2, style=line.style_solid)
// Create support zone
if showSwingZones
box.delete(swingLowZone)
zoneTop = math.max(lastSwingLow, swingLow)
zoneBottom = math.min(lastSwingLow, swingLow)
swingLowZone := box.new(lastSwingLowBar, zoneTop, bar_index - swingLength, zoneBottom, border_color=color.new(#00ff88, 80), bgcolor=color.new(#00ff88, 92))
// ============================================================================
// Break of structure (bos)
// ============================================================================
// Track last BOS bar for cooloff
var int lastBullishBOS = na
var int lastBearishBOS = na
// Check if cooloff period has passed
canPlotBullishBOS = na(lastBullishBOS) or (bar_index - lastBullishBOS) >= bosCooloff
canPlotBearishBOS = na(lastBearishBOS) or (bar_index - lastBearishBOS) >= bosCooloff
// Bullish BOS: Price breaks above previous swing high while EMA is bullish
bullishBOS = showBOS and canPlotBullishBOS and emaTrend == 1 and not na(prevSwingHigh) and close > prevSwingHigh and close[1] <= prevSwingHigh
// Bearish BOS: Price breaks below previous swing low while EMA is bearish
bearishBOS = showBOS and canPlotBearishBOS and emaTrend == -1 and not na(prevSwingLow) and close < prevSwingLow and close[1] >= prevSwingLow
// Update last BOS bars
if bullishBOS
lastBullishBOS := bar_index
if bearishBOS
lastBearishBOS := bar_index
// Plot BOS with enhanced visuals and SL at the candle wick
if bullishBOS
// Calculate SL at the low of the current candle (bottom of wick) with buffer
slLevel = low * (1 - slBuffer/100)
// BOS Label with shadow effect
label.new(bar_index, low, "BOS", style=label.style_label_up, color=color.new(#00ff88, 0), textcolor=color.black, size=size.normal, tooltip="Bullish Break of Structure\nSL: " + str.tostring(slLevel))
// Main SL line at candle low
line.new(bar_index, slLevel, bar_index + slExtension, slLevel, color=color.new(#00ff88, 0), width=2, style=line.style_dashed, extend=extend.none)
// SL zone box for visual emphasis
box.new(bar_index, slLevel + (slLevel * 0.002), bar_index + slExtension, slLevel - (slLevel * 0.002), border_color=color.new(#00ff88, 60), bgcolor=color.new(#00ff88, 85))
// S/R label
label.new(bar_index + slExtension, slLevel, "S/R", style=label.style_label_left, color=color.new(#00ff88, 0), textcolor=color.black, size=size.tiny)
if bearishBOS
// Calculate SL at the high of the current candle (top of wick) with buffer
slLevel = high * (1 + slBuffer/100)
// BOS Label with shadow effect
label.new(bar_index, high, "BOS", style=label.style_label_down, color=color.new(#ff3366, 0), textcolor=color.white, size=size.normal, tooltip="Bearish Break of Structure\nSL: " + str.tostring(slLevel))
// Main SL line at candle high
line.new(bar_index, slLevel, bar_index + slExtension, slLevel, color=color.new(#ff3366, 0), width=2, style=line.style_dashed, extend=extend.none)
// SL zone box for visual emphasis
box.new(bar_index, slLevel + (slLevel * 0.002), bar_index + slExtension, slLevel - (slLevel * 0.002), border_color=color.new(#ff3366, 60), bgcolor=color.new(#ff3366, 85))
// S/R label
label.new(bar_index + slExtension, slLevel, "S/R", style=label.style_label_left, color=color.new(#ff3366, 0), textcolor=color.white, size=size.tiny)
// ============================================================================
// Dynamic background zones
// ============================================================================
bgcolor(showBG and emaTrend == 1 ? bgBullColor : showBG and emaTrend == -1 ? bgBearColor : na)
// ============================================================================
// Alerts
// ============================================================================
alertcondition(bullishBOS, "Bullish BOS", "Bullish Break of Structure detected!")
alertcondition(bearishBOS, "Bearish BOS", "Bearish Break of Structure detected!")
alertcondition(emaTrend == 1 and emaTrend[1] != 1, "EMA Bullish", "EMA turned bullish")
alertcondition(emaTrend == -1 and emaTrend[1] != -1, "EMA Bearish", "EMA turned bearish")
// ╔════════════════════════════════╗
// ║ Download at ║
// ╚════════════════════════════════╝
// ███████╗██╗███╗ ███╗██████╗ ██╗ ███████╗
// ██╔════╝██║████╗ ████║██╔══██╗██║ ██╔════╝
// ███████╗██║██╔████╔██║██████╔╝██║ █████╗
// ╚════██║██║██║╚██╔╝██║██╔═══╝ ██║ ██╔══╝
// ███████║██║██║ ╚═╝ ██║██║ ███████╗███████╗
// ╚══════╝╚═╝╚═╝ ╚═╝╚═╝ ╚══════╝╚══════╝
// ███████╗ ██████╗ ██████╗ ███████╗██╗ ██╗
// ██╔════╝██╔═══██╗██╔══██╗██╔════╝╚██╗██╔╝
// █████╗ ██║ ██║██████╔╝█████╗ ╚███╔╝
// ██╔══╝ ██║ ██║██╔══██╗██╔══╝ ██╔██╗
// ██║ ╚██████╔╝██║ ██║███████╗██╔╝ ██╗
// ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝
// ████████╗ ██████╗ ██████╗ ██╗ ███████╗
// ╚══██╔══╝██╔═══██╗██╔═══██╗██║ ██╔════╝
// ██║ ██║ ██║██║ ██║██║ ███████╗
// ██║ ██║ ██║██║ ██║██║ ╚════██║
// ██║ ╚██████╔╝╚██████╔╝███████╗███████║
// ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝╚══════╝
// ==========================================================================================
オープンソーススクリプト
TradingViewの精神に則り、このスクリプトの作者はコードをオープンソースとして公開してくれました。トレーダーが内容を確認・検証できるようにという配慮です。作者に拍手を送りましょう!無料で利用できますが、コードの再公開はハウスルールに従う必要があります。
免責事項
この情報および投稿は、TradingViewが提供または推奨する金融、投資、トレード、その他のアドバイスや推奨を意図するものではなく、それらを構成するものでもありません。詳細は利用規約をご覧ください。
オープンソーススクリプト
TradingViewの精神に則り、このスクリプトの作者はコードをオープンソースとして公開してくれました。トレーダーが内容を確認・検証できるようにという配慮です。作者に拍手を送りましょう!無料で利用できますが、コードの再公開はハウスルールに従う必要があります。
免責事項
この情報および投稿は、TradingViewが提供または推奨する金融、投資、トレード、その他のアドバイスや推奨を意図するものではなく、それらを構成するものでもありません。詳細は利用規約をご覧ください。