Trapped Traders Order Blocks

mindyourbuisness · study · 148 行 · 点赞 3,265 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator('Trapped Traders Order Blocks', "TTOB", overlay = true)
 
// User inputs with prefix
trapped_blocks_max_age = input.int(52, title = 'Max Box Age (Bars)', minval = 1)
trapped_blocks_show_boxes = input.bool(true, title = 'Show Trap Boxes')
trapped_blocks_show_sweep_visuals = input.bool(true, title = 'Show Sweep Visuals')
trapped_blocks_bullish_box_color = input.color(color.green, title = 'Bullish Box Color')
trapped_blocks_bullish_border_color = input.color(color.green, title = 'Bullish Border Color')
trapped_blocks_bearish_box_color = input.color(color.red, title = 'Bearish Box Color')
trapped_blocks_bearish_border_color = input.color(color.red, title = 'Bearish Border Color')
 
 
import mindyourbuisness/CustomAlertLib/3 as CAL
 
// Alert Settings
alertFormat = input.text_area('{"ticker":"{ticker}", "interval":"{interval}", "alert":"{default}"}', "Custom alert message format", group="Alert Settings")
 
// Function to check if a candle is a doji
is_doji() =>
    body = math.abs(close - open)
    candle_range = high - low
    body <= 0.1 * candle_range // A doji has a very small body relative to the range
 
// Bullish Trapped Block Logic
bearish_candle = close[1] < open[1] // Candle A is bearish
sweeps_high = high[1] > high[2] // Candle A sweeps the high of Candle B
upper_wick_longer = high[1] - math.max(open[1], close[1]) > math.min(open[1], close[1]) - low[1] // Upper wick is longer
not_doji = not is_doji()[1] // Candle A is not a doji
next_candle_bullish = close > math.max(open[1], close[1]) // Candle C closes above Candle A's body
 
bullish_trap_condition = bearish_candle and sweeps_high and upper_wick_longer and not_doji and next_candle_bullish
 
// Bearish Trapped Block Logic
bullish_candle = close[1] > open[1] // Candle A is bullish
sweeps_low = low[1] < low[2] // Candle A sweeps the low of Candle B
lower_wick_longer = math.min(open[1], close[1]) - low[1] > high[1] - math.max(open[1], close[1]) // Lower wick is longer
next_candle_bearish = close < math.min(open[1], close[1]) // Candle C closes below Candle A's body
 
bearish_trap_condition = bullish_candle and sweeps_low and lower_wick_longer and not_doji and next_candle_bearish
 
// Variables to store box objects for updating with prefix
var array<box> trapped_blocks_bullish_boxes = array.new_box(0)
var array<box> trapped_blocks_bearish_boxes = array.new_box(0)
 
// Draw and manage Bullish Trapped Blocks
if bullish_trap_condition
    // Create a new box for the bullish trap
    if trapped_blocks_show_boxes
        new_box = box.new(left = bar_index[1], top = math.max(open[1], close[1]), right = bar_index, bottom = math.min(open[1], close[1]), bgcolor = color.new(trapped_blocks_bullish_box_color, 80), border_color = trapped_blocks_bullish_border_color)
        array.push(trapped_blocks_bullish_boxes, new_box)
        // Add a label
    // Trigger alert for new bullish trap
    CAL.customAlert(alertFormat, "Bull Block Created")
 
// Draw and manage Bearish Trapped Blocks
if bearish_trap_condition
    // Create a new box for the bearish trap
    if trapped_blocks_show_boxes
        new_box = box.new(left = bar_index[1], top = math.max(open[1], close[1]), right = bar_index, bottom = math.min(open[1], close[1]), bgcolor = color.new(trapped_blocks_bearish_box_color, 80), border_color = trapped_blocks_bearish_border_color)
        array.push(trapped_blocks_bearish_boxes, new_box)
        // Add a label
    // Trigger alert for new bearish trap
    CAL.customAlert(alertFormat, "Bear Block Created")
// Variables to track sweep conditions with prefix
var bool trapped_blocks_bullish_sweep_detected = false
var bool trapped_blocks_bearish_sweep_detected = false
 
// Calculate wick lengths and range conditions for sweeps
lower_wick = math.min(open, close) - low
upper_wick = high - math.max(open, close)
bullish_wick_condition = lower_wick > upper_wick // For bullish sweep: lower wick > upper wick
bearish_wick_condition = upper_wick > lower_wick // For bearish sweep: upper wick > lower wick
candle_range = high - low
range_midpoint = low + candle_range / 2
bullish_range_condition = close > range_midpoint // For bullish sweep: close > 50% of range
bearish_range_condition = close < range_midpoint // For bearish sweep: close < 50% of range
 
// Check for sweeps of bullish boxes
if array.size(trapped_blocks_bullish_boxes) > 0
    for i = 0 to array.size(trapped_blocks_bullish_boxes) - 1 by 1
        current_box = array.get(trapped_blocks_bullish_boxes, i)
        box_left = box.get_left(current_box)
        age = bar_index - box_left
        // Update the right side of the box
        if age <= trapped_blocks_max_age
            box.set_right(current_box, bar_index)
        else
            box.set_right(current_box, box_left + trapped_blocks_max_age)
        // Check for sweeps only if the box is not too old
        if age <= trapped_blocks_max_age
            box_top = box.get_top(current_box)
            box_bottom = box.get_bottom(current_box)
            // Bullish sweep: open > top, low < top, close > top, lower wick > upper wick, and close > 50% of range
            bullish_sweep = open > box_top and low < box_top and close > box_top and bullish_wick_condition and bullish_range_condition
            // Bearish sweep: open < bottom, high > bottom, close < bottom, upper wick > lower wick, and close < 50% of range
            bearish_sweep = open < box_bottom and high > box_bottom and close < box_bottom and bearish_wick_condition and bearish_range_condition
            if bullish_sweep and not bullish_sweep[1]
                trapped_blocks_bullish_sweep_detected := true
                // Add a label for the bullish sweep if enabled (at the low, pointing up)
                if trapped_blocks_show_sweep_visuals
                    label.new(bar_index, low, text = 'Bullish Sweep', style = label.style_label_up, color = trapped_blocks_bullish_box_color, textcolor = color.white)
            if bearish_sweep and not bearish_sweep[1]
                trapped_blocks_bearish_sweep_detected := true
                // Add a label for the bearish sweep if enabled (at the high, pointing down)
                if trapped_blocks_show_sweep_visuals
                    label.new(bar_index, high, text = 'Bearish Sweep', style = label.style_label_down, color = trapped_blocks_bearish_box_color, textcolor = color.white)
 
// Check for sweeps of bearish boxes
if array.size(trapped_blocks_bearish_boxes) > 0
    for i = 0 to array.size(trapped_blocks_bearish_boxes) - 1 by 1
        current_box = array.get(trapped_blocks_bearish_boxes, i)
        box_left = box.get_left(current_box)
        age = bar_index - box_left
        // Update the right side of the box
        if age <= trapped_blocks_max_age
            box.set_right(current_box, bar_index)
        else
            box.set_right(current_box, box_left + trapped_blocks_max_age)
        // Check for sweeps only if the box is not too old
        if age <= trapped_blocks_max_age
            box_top = box.get_top(current_box)
            box_bottom = box.get_bottom(current_box)
            // Bullish sweep: open > top, low < top, close > top, lower wick > upper wick, and close > 50% of range
            bullish_sweep = open > box_top and low < box_top and close > box_top and bullish_wick_condition and bullish_range_condition
            // Bearish sweep: open < bottom, high > bottom, close < bottom, upper wick > lower wick, and close < 50% of range
            bearish_sweep = open < box_bottom and high > box_bottom and close < box_bottom and bearish_wick_condition and bearish_range_condition
            if bullish_sweep and not bullish_sweep[1]
                trapped_blocks_bullish_sweep_detected := true
                // Add a label for the bullish sweep if enabled (at the low, pointing up)
                if trapped_blocks_show_sweep_visuals
                    label.new(bar_index, low, text = 'Bullish Sweep', style = label.style_label_up, color = trapped_blocks_bullish_box_color, textcolor = color.white)
            if bearish_sweep and not bearish_sweep[1]
                trapped_blocks_bearish_sweep_detected := true
                // Add a label for the bearish sweep if enabled (at the high, pointing down)
                if trapped_blocks_show_sweep_visuals
                    label.new(bar_index, high, text = 'Bearish Sweep', style = label.style_label_down, color = trapped_blocks_bearish_box_color, textcolor = color.white)
 
// Trigger alerts for sweeps using alert()
if trapped_blocks_bullish_sweep_detected
    CAL.customAlert(alertFormat,"Bullish TBlock Sweep")
 
if trapped_blocks_bearish_sweep_detected
    CAL.customAlert(alertFormat,"Bearish TBlock Sweep")
 
// Reset sweep detection for the next bar
trapped_blocks_bullish_sweep_detected := false
trapped_blocks_bearish_sweep_detected := false

← 返回列表