GOLD Consolidation Origin Zones (GCOZ)

EricBenhura · study · 185 行 · 点赞 10 · TradingView 原页

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

Pine Script

//@version=6
indicator("GOLD Consolidation Origin Zones (GCOZ)", overlay=true, max_boxes_count=500)
 
//====================================================
// 1. INPUT SETTINGS
//====================================================
 
groupDetect = "Consolidation Detection"
 
baseLen = input.int(5, "Consolidation candles", minval=3, maxval=12, group=groupDetect)
atrLen = input.int(14, "ATR period", minval=5, group=groupDetect)
compressionATR = input.float(1.20, "Maximum base range (ATR)", minval=0.2, step=0.05, group=groupDetect)
displacementATR = input.float(1.00, "Minimum displacement body (ATR)", minval=0.2, step=0.1, group=groupDetect)
 
groupTF = "Timeframe Settings"
 
primaryTF = input.timeframe("5", "Primary zone timeframe", group=groupTF)
show15 = input.bool(true, "Show 15-minute zones", group=groupTF)
secondaryTF = input.timeframe("15", "Secondary zone timeframe", group=groupTF)
 
groupStyle = "Zone Appearance"
 
freshColor = input.color(color.gray, "Fresh zone outline", group=groupStyle)
testedColor = input.color(color.orange, "First retest outline", group=groupStyle)
freshFill = input.int(100, "Fresh zone transparency", minval=0, maxval=100, group=groupStyle)
testedFill = input.int(90, "Retested zone transparency", minval=0, maxval=100, group=groupStyle)
 
groupManagement = "Zone Management"
 
maxZones = input.int(100, "Maximum historical zones", minval=10, maxval=200, group=groupManagement)
removeInvalid = input.bool(true, "Remove invalidated zones", group=groupManagement)
 
//====================================================
// 2. CONSOLIDATION AND DISPLACEMENT DETECTION
//====================================================
 
f_detectZone() =>
    float atrBase = ta.atr(atrLen)[1]
 
    float baseHigh = ta.highest(high[1], baseLen)
    float baseLow = ta.lowest(low[1], baseLen)
 
    float baseRange = baseHigh - baseLow
    float candleBody = math.abs(close - open)
 
    bool compressed = not na(atrBase) and baseRange <= atrBase * compressionATR
 
    bool bullishExpansion = close > baseHigh and close > open and candleBody >= atrBase * displacementATR
 
    bool bearishExpansion = close < baseLow and close < open and candleBody >= atrBase * displacementATR
 
    bool bullishSignal = barstate.isconfirmed and compressed and bullishExpansion
    bool bearishSignal = barstate.isconfirmed and compressed and bearishExpansion
 
    bool validSignal = bullishSignal or bearishSignal
 
    int direction = bullishSignal ? 1 : bearishSignal ? -1 : 0
 
    int originTime = time[baseLen]
 
    [validSignal, baseHigh, baseLow, originTime, direction]
 
//====================================================
// 3. MULTI-TIMEFRAME DATA
//====================================================
 
[sig5, top5, bottom5, start5, dir5] = request.security(syminfo.tickerid, primaryTF, f_detectZone(), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
 
[sig15, top15, bottom15, start15, dir15] = request.security(syminfo.tickerid, secondaryTF, f_detectZone(), gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off)
 
//====================================================
// 4. ZONE STORAGE
//====================================================
 
var box[] zoneBoxes = array.new_box()
var float[] zoneTops = array.new_float()
var float[] zoneBottoms = array.new_float()
var int[] zoneDirections = array.new_int()
var bool[] zoneTested = array.new_bool()
var int[] zoneBornBars = array.new_int()
 
var int last5Time = na
var int last15Time = na
 
//====================================================
// 5. CREATE ZONES
//====================================================
 
f_addZone(int leftTime, float zoneTop, float zoneBottom, int direction) =>
 
    color border = freshColor
 
    box newBox = box.new(left=leftTime, top=zoneTop, right=time, bottom=zoneBottom, xloc=xloc.bar_time, extend=extend.right, border_color=border, border_width=1, bgcolor=color.new(freshColor, freshFill))
 
    array.push(zoneBoxes, newBox)
    array.push(zoneTops, zoneTop)
    array.push(zoneBottoms, zoneBottom)
    array.push(zoneDirections, direction)
    array.push(zoneTested, false)
    array.push(zoneBornBars, bar_index)
 
    if array.size(zoneBoxes) > maxZones
        box oldBox = array.shift(zoneBoxes)
        box.delete(oldBox)
        array.shift(zoneTops)
        array.shift(zoneBottoms)
        array.shift(zoneDirections)
        array.shift(zoneTested)
        array.shift(zoneBornBars)
 
//====================================================
// 6. REGISTER NEW 5-MINUTE ZONES
//====================================================
 
bool new5 = sig5 and not na(start5) and (na(last5Time) or start5 != last5Time)
 
if new5
    f_addZone(start5, top5, bottom5, dir5)
    last5Time := start5
 
//====================================================
// 7. REGISTER NEW 15-MINUTE ZONES
//====================================================
 
bool new15 = show15 and sig15 and not na(start15) and (na(last15Time) or start15 != last15Time)
 
if new15
    f_addZone(start15, top15, bottom15, dir15)
    last15Time := start15
 
//====================================================
// 8. MONITOR RETESTS AND INVALIDATIONS
//====================================================
 
if array.size(zoneBoxes) > 0
 
    for i = array.size(zoneBoxes) - 1 to 0
 
        box currentBox = array.get(zoneBoxes, i)
 
        float zoneTop = array.get(zoneTops, i)
        float zoneBottom = array.get(zoneBottoms, i)
 
        int direction = array.get(zoneDirections, i)
 
        bool tested = array.get(zoneTested, i)
 
        int bornBar = array.get(zoneBornBars, i)
 
        // Invalidation rules
        bool invalidated = direction == 1 ? close < zoneBottom : close > zoneTop
 
        // Remove invalidated zones
        if removeInvalid and invalidated and bar_index > bornBar
 
            box.delete(currentBox)
 
            array.remove(zoneBoxes, i)
            array.remove(zoneTops, i)
            array.remove(zoneBottoms, i)
            array.remove(zoneDirections, i)
            array.remove(zoneTested, i)
            array.remove(zoneBornBars, i)
 
        else
 
            // First price revisit
            bool touched = high >= zoneBottom and low <= zoneTop
 
            if touched and not tested and bar_index > bornBar
 
                array.set(zoneTested, i, true)
 
                // Change fresh grey zone into orange dashed zone
                box.set_border_color(currentBox, testedColor)
                box.set_border_style(currentBox, line.style_dashed)
                box.set_bgcolor(currentBox, color.new(testedColor, testedFill))
 
//====================================================
// 9. ALERTS
//====================================================
 
alertcondition(new5, "New 5M Consolidation Zone", "New GOLD 5-minute consolidation origin zone detected.")
 
alertcondition(new15, "New 15M Consolidation Zone", "New GOLD 15-minute consolidation origin zone detected.")

← 返回列表