Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- indicator("XAUUSD Bollinger Breakout + Strong Moves - NY Session", overlay=true, max_labels_count=100)
- // --- New York Session Settings ---
- var string TZ = "America/New_York"
- var string SESSION = "0930-1900"
- bool inSession = not na(time(timeframe.period, SESSION, TZ))
- bool sessionStart = inSession and not inSession[1]
- // --- Bollinger Bands Input Parameters ---
- int bb_length = input.int(20, "Bollinger Length", minval=2)
- float bb_stddev = input.float(2.0, "Standard Deviation", minval=0.5, maxval=5.0, step=0.1)
- float breakout_threshold = input.float(10.0, "Breakout Threshold (USD)", minval=1.0, step=0.5)
- color bullish_color = input.color(color.green, "Bullish Color")
- color bearish_color = input.color(color.red, "Bearish Color")
- bool show_markers = input.bool(true, "Show Price Markers")
- bool show_extreme = input.bool(true, "Highlight Largest")
- // --- Strong Move Detection Parameters ---
- float min_move_percent = input.float(0.5, "Minimum Move %", minval=0.1, maxval=5.0, step=0.1)
- int lookback_bars = input.int(3, "Lookback Bars", minval=1, maxval=10)
- color strong_bullish_color = input.color(color.lime, "Strong Bullish Color")
- color strong_bearish_color = input.color(color.maroon, "Strong Bearish Color")
- bool show_strong_moves = input.bool(true, "Show Strong Moves")
- // --- Bollinger Bands Calculation ---
- float basis = ta.sma(close, bb_length)
- float dev = ta.stdev(close, bb_length)
- float upper_band = basis + bb_stddev * dev
- float lower_band = basis - bb_stddev * dev
- // Calculate distance from bands using HIGH and LOW
- float high_distance_from_upper = high - upper_band
- float low_distance_from_lower = lower_band - low
- // Breakout conditions - ONLY DURING NY SESSION
- bool bullish_breakout = high_distance_from_upper >= breakout_threshold and inSession
- bool bearish_breakout = low_distance_from_lower >= breakout_threshold and inSession
- // Track if breakout has occurred on current bar
- var bool current_bullish_breakout = false
- var bool current_bearish_breakout = false
- var float current_bullish_distance = 0.0
- var float current_bearish_distance = 0.0
- // Track largest breakout in the NY session
- var float largest_bullish = 0.0
- var float largest_bearish = 0.0
- var int largest_bullish_bar = 0
- var int largest_bearish_bar = 0
- // Update breakout status (real-time)
- if bullish_breakout
- current_bullish_breakout := true
- current_bullish_distance := high_distance_from_upper
- if bearish_breakout
- current_bearish_breakout := true
- current_bearish_distance := low_distance_from_lower
- // Update largest breakouts during NY session
- if bullish_breakout and high_distance_from_upper > largest_bullish
- largest_bullish := high_distance_from_upper
- largest_bullish_bar := bar_index
- if bearish_breakout and low_distance_from_lower > largest_bearish
- largest_bearish := low_distance_from_lower
- largest_bearish_bar := bar_index
- // Reset on new bar OR when session ends
- if barstate.isnew or (not inSession and inSession[1])
- current_bullish_breakout := false
- current_bearish_breakout := false
- current_bullish_distance := 0.0
- current_bearish_distance := 0.0
- // Reset largest breakouts when session ends
- if not inSession and inSession[1]
- largest_bullish := 0.0
- largest_bearish := 0.0
- largest_bullish_bar := 0
- largest_bearish_bar := 0
- // --- NY Session High/Low Reference Lines ---
- // Reference values for 9:30am candle
- var float refHigh = na
- var float refLow = na
- // Lines and shaded box
- var line hiLine = na
- var line loLine = na
- var box rngBox = na
- // When session starts (first bar >= 09:30 NY)
- if sessionStart
- refHigh := high
- refLow := low
- // Create lines for that range
- hiLine := line.new(bar_index, refHigh, bar_index, refHigh, extend=extend.none, width=2, color=color.new(color.teal, 0))
- loLine := line.new(bar_index, refLow, bar_index, refLow, extend=extend.none, width=2, color=color.new(color.orange, 0))
- // Shaded area between them
- rngBox := box.new(left=bar_index, top=refHigh, right=bar_index, bottom=refLow, border_color=color.new(color.gray, 80), bgcolor=color.new(color.gray, 90))
- // While we're still within the NY session, extend everything to the current bar
- if inSession
- if not na(hiLine)
- line.set_x2(hiLine, bar_index)
- line.set_y2(hiLine, refHigh)
- if not na(loLine)
- line.set_x2(loLine, bar_index)
- line.set_y2(loLine, refLow)
- if not na(rngBox)
- box.set_right(rngBox, bar_index)
- box.set_top(rngBox, refHigh)
- box.set_bottom(rngBox, refLow)
- // --- Strong Move Detection ---
- // Calculate move over the last N bars
- float highest_high = ta.highest(high, lookback_bars)
- float lowest_low = ta.lowest(low, lookback_bars)
- float move_size = highest_high - lowest_low
- float move_percent = (move_size / close) * 100
- // Strong move conditions
- bool strong_bullish_move = move_percent >= min_move_percent and close > open and inSession
- bool strong_bearish_move = move_percent >= min_move_percent and close < open and inSession
- // Track strong moves to avoid duplicate detection
- var int last_strong_move_bar = 0
- bool is_new_strong_move = bar_index > last_strong_move_bar + lookback_bars
- // Detect new strong moves
- if (strong_bullish_move or strong_bearish_move) and is_new_strong_move and barstate.isconfirmed and show_strong_moves
- last_strong_move_bar := bar_index
- // Draw lines for the move range
- line.new(bar_index - lookback_bars + 1, highest_high, bar_index, highest_high,
- color=strong_bullish_move ? strong_bullish_color : strong_bearish_color,
- width=2, style=line.style_solid)
- line.new(bar_index - lookback_bars + 1, lowest_low, bar_index, lowest_low,
- color=strong_bullish_move ? strong_bullish_color : strong_bearish_color,
- width=2, style=line.style_solid)
- // Connect the lines at the ends (create a rectangle)
- line.new(bar_index - lookback_bars + 1, highest_high, bar_index - lookback_bars + 1, lowest_low,
- color=strong_bullish_move ? strong_bullish_color : strong_bearish_color,
- width=1, style=line.style_dotted)
- line.new(bar_index, highest_high, bar_index, lowest_low,
- color=strong_bullish_move ? strong_bullish_color : strong_bearish_color,
- width=1, style=line.style_dotted)
- // Label with dollar amount
- string move_text = str.tostring(math.round(move_size * 100) / 100, "#.##") + "$"
- string percent_text = str.tostring(math.round(move_percent * 100) / 100, "#.##") + "%"
- string label_text = move_text + "\n" + percent_text
- // Position label in the middle of the move
- float label_y = lowest_low + (move_size / 2)
- if strong_bullish_move
- label.new(bar_index - math.round(lookback_bars / 2), label_y, label_text,
- color=strong_bullish_color, style=label.style_label_center,
- textcolor=color.white, size=size.normal,
- textalign=text.align_center)
- else
- label.new(bar_index - math.round(lookback_bars / 2), label_y, label_text,
- color=strong_bearish_color, style=label.style_label_center,
- textcolor=color.white, size=size.normal,
- textalign=text.align_center)
- // --- Bollinger Bands Visualization ---
- // Plot Bollinger Bands
- plot(basis, "Basis", color=color.orange, linewidth=1)
- p1 = plot(upper_band, "Upper Band", color=color.blue, linewidth=1)
- p2 = plot(lower_band, "Lower Band", color=color.blue, linewidth=1)
- fill(p1, p2, color=color.new(color.blue, 90))
- // --- Plot REAL-TIME breakout markers (ONLY DURING NY SESSION) ---
- if current_bullish_breakout and inSession
- // Marker on the price where breakout occurred
- if show_markers
- line.new(bar_index - 1, high, bar_index, high, color=bullish_color, width=2, style=line.style_solid)
- // Label with distance
- label_y = high + (high * 0.0005) // Small offset above price
- if bar_index == largest_bullish_bar and show_extreme
- label.new(bar_index, label_y, "▲ " + str.tostring(math.round(current_bullish_distance * 100) / 100, "#.##") + "$", color=bullish_color, style=label.style_label_down, textcolor=color.white, size=size.large)
- else
- label.new(bar_index, label_y, str.tostring(math.round(current_bullish_distance * 100) / 100, "#.##") + "$", color=bullish_color, style=label.style_label_down, textcolor=color.white, size=size.normal)
- if current_bearish_breakout and inSession
- // Marker on the price where breakout occurred
- if show_markers
- line.new(bar_index - 1, low, bar_index, low, color=bearish_color, width=2, style=line.style_solid)
- // Label with distance
- label_y = low - (low * 0.0005) // Small offset below price
- if bar_index == largest_bearish_bar and show_extreme
- label.new(bar_index, label_y, "▼ " + str.tostring(math.round(current_bearish_distance * 100) / 100, "#.##") + "$", color=bearish_color, style=label.style_label_up, textcolor=color.white, size=size.large)
- else
- label.new(bar_index, label_y, str.tostring(math.round(current_bearish_distance * 100) / 100, "#.##") + "$", color=bearish_color, style=label.style_label_up, textcolor=color.white, size=size.normal)
- // Plot historical breakouts (after bar closes) - ONLY DURING NY SESSION
- if barstate.isconfirmed and inSession[1]
- // Bullish breakout that occurred
- if high[1] - upper_band[1] >= breakout_threshold
- label.new(bar_index - 1, high[1], "✓", color=color.new(bullish_color, 30), style=label.style_label_down, textcolor=bullish_color, size=size.small)
- // Bearish breakout that occurred
- if lower_band[1] - low[1] >= breakout_threshold
- label.new(bar_index - 1, low[1], "✓", color=color.new(bearish_color, 30), style=label.style_label_up, textcolor=bearish_color, size=size.small)
- // --- Plot breakout threshold lines ---
- plot(upper_band + breakout_threshold, "Upper Threshold", color=color.new(color.green, 50), style=plot.style_circles, linewidth=1)
- plot(lower_band - breakout_threshold, "Lower Threshold", color=color.new(color.red, 50), style=plot.style_circles, linewidth=1)
- // --- Background color for NY Session ---
- bgcolor(inSession ? color.new(color.blue, 90) : na, title="NY Session Background")
- // --- Info table ---
- var table info_table = table.new(position.top_right, 1, 8, border_width=1)
- if barstate.islast
- table.cell(info_table, 0, 0, "XAUUSD Bollinger + Strong Moves", text_color=color.white, bgcolor=color.blue)
- table.cell(info_table, 0, 1, "NY Session: 9:30-19:00", text_color=color.white, bgcolor=color.gray)
- table.cell(info_table, 0, 2, "Threshold: " + str.tostring(breakout_threshold) + " USD", text_color=color.white, bgcolor=color.gray)
- table.cell(info_table, 0, 3, "Strong Move: ≥" + str.tostring(min_move_percent) + "%", text_color=color.white, bgcolor=color.gray)
- table.cell(info_table, 0, 4, "Timeframe: " + timeframe.period, text_color=color.white, bgcolor=color.gray)
- table.cell(info_table, 0, 5, "▲▼ = Largest in Session", text_color=color.white, bgcolor=color.gray)
- table.cell(info_table, 0, 6, "✓ = Confirmed", text_color=color.white, bgcolor=color.gray)
- table.cell(info_table, 0, 7, "Real-Time Detection", text_color=color.white, bgcolor=color.gray)
- // --- Alerts for breakouts (ONLY DURING NY SESSION) ---
- alertcondition(bullish_breakout, "Bullish Breakout NY", "Price moved above upper band by ${breakout_threshold} during NY session")
- alertcondition(bearish_breakout, "Bearish Breakout NY", "Price moved below lower band by ${breakout_threshold} during NY session")
- // --- NY Session High/Low Breakout Alerts ---
- bool breakHigh = inSession and ta.crossover(close, refHigh)
- bool breakLow = inSession and ta.crossunder(close, refLow)
- alertcondition(breakHigh, title="Break Above 9:30 High", message="Price broke above the 9:30am High (NY).")
- alertcondition(breakLow, title="Break Below 9:30 Low", message="Price broke below the 9:30am Low (NY).")
- // --- Strong Move Alerts ---
- alertcondition(strong_bullish_move and barstate.isconfirmed, "Strong Bullish Move", "Strong bullish move detected: ${move_percent}% over ${lookback_bars} bars")
- alertcondition(strong_bearish_move and barstate.isconfirmed, "Strong Bearish Move", "Strong bearish move detected: ${move_percent}% over ${lookback_bars} bars")
Advertisement
Add Comment
Please, Sign In to add comment