You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于Array和UDT创建警报并实现Zig Zag折线断后重建

基于数组与UDT创建警报及折线更新解决方案

问题描述

我不清楚如何基于Array(数组)和UDT(用户定义类型)的数据创建警报。已创建整数变量IsCrossingAlert,期望其值为1时触发警报,值为0时不触发警报。同时,还想了解如何删除原有折线,并从起始点到折线断裂的位置重新创建新折线。

原代码

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tgh
//@version=5
indicator('funkcja', 'funk', overlay=true, precision=2, max_bars_back = 1000)

showHHLL = input(defval=true, title="showHHLL?")
showHHw       = input(defval=true, title="showHHw?") // ukrywa i pokazuje linie i opis HH LL 

prd1 = input.int(10, title="ZigZag Period 1", minval = 2, maxval = 20)  // było 8ale mi nie pasowało
phA1 = ta.highestbars(high, prd1) // 0 oznacza najwyzszy pkt, -1 do prd => liczba bar do najwyzszy pkt

showzz = input.string("Show Zig Zag 1", title = "Show Zig Zags", options = ["Show Zig Zag 1", "Show Zig Zag 2", "Show Both", "Show None"])
showhhll = input.string("Show HHLL 1", title = "Show HHLL", options = ["Show HHLL 1", "Show HHLL 2", "Show Both", "Show None"])
upcol1 = input(defval = color.lime, title = "Zig Zag 1 Up Color")
dncol1 = input(defval = color.red, title = "Zig Zag 1 Down Color")
txtcol = input(defval = color.black, title = "Text Color")
zz1style = input.string("Dashed", title = "Zig Zag 1 Line Style", options = ["Dashed", "Dotted"])
zz1width = input.int(2, title = "Zig zag 1 Line Width", minval = 1, maxval = 4)

float ph1 = ta.highestbars(high, prd1) == 0 ? high : na
float pl1 = ta.lowestbars(low, prd1) == 0 ? low : na
var dir1 = 0
dir1 := (ph1 and na(pl1)) ? 1 : (pl1 and na(ph1)) ? -1 : dir1

//
var max_array_size = 22 // [5, 2] matrix 10
var zigzag1 = array.new_float(0)
var zigzag2 = array.new_float(0)
oldzigzag1 = array.copy(zigzag1)
oldzigzag2 = array.copy(zigzag2)

add_to_zigzag(pointer, value, bindex)=>
    array.unshift(pointer, bindex)
    array.unshift(pointer, value)
    if array.size(pointer) > max_array_size
        array.pop(pointer)
        array.pop(pointer)

update_zigzag(pointer, value, bindex, dir)=>
    if array.size(pointer) == 0
        add_to_zigzag(pointer, value, bindex)
    else
        if (dir == 1 and value > array.get(pointer, 0)) or (dir == -1 and value < array.get(pointer, 0))
            array.set(pointer, 0, value)
            array.set(pointer, 1, bindex)
        0.

dir1changed = ta.change(dir1)
if ph1 or pl1
    if dir1changed 
        add_to_zigzag(zigzag1, dir1 == 1 ? ph1 : pl1, bar_index)
    else
        update_zigzag(zigzag1, dir1 == 1 ? ph1 : pl1, bar_index, dir1)

var MacierzNr = array.new_int() // , var MacierzLow = array.new_float()
// HH HL  LH LL   
// 22 21  12 11 
// zigzag1 
//  1  0  3  2  5  4  7  6   9 8  
//  x1 y1 x2 y2 x3 y3 x4 y4 x5 y5  

if array.size(zigzag1) >= 6 and showHHLL      
    var line zzline1 = na
    var label zzlabel1 = na
    if array.get(zigzag1, 0) != array.get(oldzigzag1, 0) or array.get(zigzag1, 1) != array.get(oldzigzag1, 1)
        if array.get(zigzag1, 2) == array.get(oldzigzag1, 2) and array.get(zigzag1, 3) == array.get(oldzigzag1, 3)
            line.delete(zzline1)
            label.delete(zzlabel1)
            array.remove(MacierzNr,0) //,   array.remove(MacierzLow,0)                        
        if (showzz == "Show Zig Zag 1" or showzz == "Show Both") and showHHw
            zzline1 := line.new( x1 = math.round(array.get(zigzag1, 1)), y1 = array.get(zigzag1, 0), x2 = math.round(array.get(zigzag1, 3)), y2 = array.get(zigzag1, 2), 
                                 color = dir1 == 1 ? upcol1 : dncol1, 
                                 width = zz1width, 
                                 style = zz1style == "Dashed" ? line.style_dashed : line.style_dotted)
        if (showhhll == "Show HHLL 1" or showhhll == "Show Both")
            hhlltxt1 = dir1 == 1 ? array.get(zigzag1, 4) < array.get(zigzag1, 0) ? "HH" : "LH" : array.get(zigzag1, 4) > array.get(zigzag1, 0) ? "LL" : "HL"            
            labelcol = dir1 == 1 ? array.get(zigzag1, 0) > array.get(zigzag1, 4) ? upcol1 : dncol1 : array.get(zigzag1, 0) < array.get(zigzag1, 4) ? dncol1 : upcol1
            //zzlabel1 := label.new(x = math.round(array.get(zigzag1, 1)), y = array.get(zigzag1, 0), text = hhlltxt1, color = labelcol, textcolor = txtcol, style = dir1 == 1 ? label.style_label_down : label.style_label_up, size= size.small)
            hhllNr1 = dir1 == 1 ? array.get(zigzag1, 4) < array.get(zigzag1, 0) ? 22 : 12 : array.get(zigzag1, 4) > array.get(zigzag1, 0) ? 11 : 21
            array.unshift(id=MacierzNr,value=hhllNr1)
            //hhllLow = dir1 == 1 ? array.get(zigzag1, 4) < array.get(zigzag1, 0) ? low : low : array.get(zigzag1, 4) > array.get(zigzag1, 0) ? low : low
            //array.unshift(id=MacierzLow,value=hhllLow)                        
            zzlabel1 := showHHw ? label.new(x = math.round(array.get(zigzag1, 1)), y = array.get(zigzag1, 0), text = hhlltxt1, color = labelcol, textcolor = txtcol, style = dir1 == 1 ? label.style_label_down : label.style_label_up, size= size.small) : na          
//

type h_line
    bool is_high = false
    bool is_broken = false
    float price = na
    line horizontal_line = na    
//
var my_hline_arr = array.new<h_line>()

int IsCrossingAlert = na // 0 => no alert, != 0 alert 

if array.size(zigzag1) >= 6 and array.size(MacierzNr) >= 6   
    war00a_3 = array.get(MacierzNr,2) == 11 or array.get(MacierzNr,2) == 21 // LL HL

    if (war00a_3)
        new_obj = h_line.new(is_high=true, price=array.get(zigzag1,4), horizontal_line=line.new( x1 = math.round(array.get(zigzag1, 5))
         ,y1 = array.get(zigzag1, 4), x2 = math.round(array.get(zigzag1, 3))+200, y2 = array.get(zigzag1, 4), //extend = extend.right, 
         color = color.new(color.red,10), width = zz1width, style = line.style_solid )    
         )
        array.push(my_hline_arr, new_obj)
    //

    len = array.size(my_hline_arr)

    if (len > 0)
        for i = 0 to len - 1
            obj = array.get(my_hline_arr, i)

            if (not obj.is_broken)  // Only process unbroken lines
                line.set_x2(obj.horizontal_line, bar_index)  // Extend the line

                if (obj.is_high)
                    if (low < obj.price)  // Check if line is broken
                        obj.is_broken := true  // It is broken: Alert, remove the line etc.
                        // I added code below
                        //IsCrossingAlert := 68000
                        //break
                    //else
                        //IsCrossingAlert := 0
//

//plot(  IsCrossingAlert , "IsCrossingAlert", color=color.new(color.red,10))

// IsAlert = if obj.is_broken == true
//     IsAlert := 68000
// else
//     IsAlert := 0
// //
//alertcondition(IsAlert != 0, title="crossing", message='crossing')
// alertcondition(IsCrossingAlert != 0, title="IsCrossingAlert", message='IsCrossingAlert')

解决方案

1. 基于数组与UDT实现警报触发

将IsCrossingAlert初始化为0,在检测到水平线被跌破时设为1,未跌破时保持0,再通过alertcondition监听该变量的状态变化触发警报。

核心修改代码:

// 初始化警报变量为0
int IsCrossingAlert = 0

if array.size(my_hline_arr) > 0
    for i = 0 to array.size(my_hline_arr) - 1
        obj = array.get(my_hline_arr, i)
        if not obj.is_broken
            line.set_x2(obj.horizontal_line, bar_index)
            if obj.is_high
                // 检测价格是否跌破水平线
                if low < obj.price
                    obj.is_broken := true
                    IsCrossingAlert := 1 // 触发警报
                else
                    IsCrossingAlert := 0 // 无警报

// 定义警报条件:当IsCrossingAlert为1时触发
alertcondition(IsCrossingAlert == 1, title="突破警报", message="水平线被价格跌破!")

2. 删除旧折线并重建至断裂点

当检测到折线被突破时,先删除原有水平线,再以原起始点和当前突破的K线位置为端点重新创建折线,并更新UDT中的折线对象。

核心修改代码:

if obj.is_high
    if low < obj.price
        obj.is_broken := true
        IsCrossingAlert := 1
        
        // 删除原有折线
        line.delete(obj.horizontal_line)
        
        // 从原起始点重建折线到当前断裂的K线位置
        new_line = line.new(
            x1 = math.round(array.get(zigzag1, 5)), // 原折线起始x坐标
            y1 = obj.price,
            x2 = bar_index, // 当前断裂的K线索引
            y2 = obj.price,
            color = color.new(color.red,10),
            width = zz1width,
            style = line.style_solid
        )
        
        // 更新UDT中的折线引用为新创建的折线
        obj.horizontal_line := new_line

完整修改后的代码

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tgh
//@version=5
indicator('funkcja', 'funk', overlay=true, precision=2, max_bars_back = 1000)

showHHLL = input(defval=true, title="showHHLL?")
showHHw       = input(defval=true, title="showHHw?") // ukrywa i pokazuje linie i opis HH LL 

prd1 = input.int(10, title="ZigZag Period 1", minval = 2, maxval = 20)  // było 8ale mi nie pasowało
phA1 = ta.highestbars(high, prd1) // 0 oznacza najwyzszy pkt, -1 do prd => liczba bar do najwyzszy pkt

showzz = input.string("Show Zig Zag 1", title = "Show Zig Zags", options = ["Show Zig Zag 1", "Show Zig Zag 2", "Show Both", "Show None"])
showhhll = input.string("Show HHLL 1", title = "Show HHLL", options = ["Show HHLL 1", "Show HHLL 2", "Show Both", "Show None"])
upcol1 = input(defval = color.lime, title = "Zig Zag 1 Up Color")
dncol1 = input(defval = color.red, title = "Zig Zag 1 Down Color")
txtcol = input(defval = color.black, title = "Text Color")
zz1style = input.string("Dashed", title = "Zig Zag 1 Line Style", options = ["Dashed", "Dotted"])
zz1width = input.int(2, title = "Zig zag 1 Line Width", minval = 1, maxval = 4)

float ph1 = ta.highestbars(high, prd1) == 0 ? high : na
float pl1 = ta.lowestbars(low, prd1) == 0 ? low : na
var dir1 = 0
dir1 := (ph1 and na(pl1)) ? 1 : (pl1 and na(ph1)) ? -1 : dir1

//
var max_array_size = 22 // [5, 2] matrix 10
var zigzag1 = array.new_float(0)
var zigzag2 = array.new_float(0)
oldzigzag1 = array.copy(zigzag1)
oldzigzag2 = array.copy(zigzag2)

add_to_zigzag(pointer, value, bindex)=>
    array.unshift(pointer, bindex)
    array.unshift(pointer, value)
    if array.size(pointer) > max_array_size
        array.pop(pointer)
        array.pop(pointer)

update_zigzag(pointer, value, bindex, dir)=>
    if array.size(pointer) == 0
        add_to_zigzag(pointer, value, bindex)
    else
        if (dir == 1 and value > array.get(pointer, 0)) or (dir == -1 and value < array.get(pointer, 0))
            array.set(pointer, 0, value)
            array.set(pointer, 1, bindex)
        0.

dir1changed = ta.change(dir1)
if ph1 or pl1
    if dir1changed 
        add_to_zigzag(zigzag1, dir1 == 1 ? ph1 : pl1, bar_index)
    else
        update_zigzag(zigzag1, dir1 == 1 ? ph1 : pl1, bar_index, dir1)

var MacierzNr = array.new_int() // , var MacierzLow = array.new_float()
// HH HL  LH LL   
// 22 21  12 11 
// zigzag1 
//  1  0  3  2  5  4  7  6   9 8  
//  x1 y1 x2 y2 x3 y3 x4 y4 x5 y5  

if array.size(zigzag1) >= 6 and showHHLL      
    var line zzline1 = na
    var label zzlabel1 = na
    if array.get(zigzag1, 0) != array.get(oldzigzag1, 0) or array.get(zigzag1, 1) != array.get(oldzigzag1, 1)
        if array.get(zigzag1, 2) == array.get(oldzigzag1, 2) and array.get(zigzag1, 3) == array.get(oldzigzag1, 3)
            line.delete(zzline1)
            label.delete(zzlabel1)
            array.remove(MacierzNr,0) //,   array.remove(MacierzLow,0)                        
        if (showzz == "Show Zig Zag 1" or showzz == "Show Both") and showHHw
            zzline1 := line.new( x1 = math.round(array.get(zigzag1, 1)), y1 = array.get(zigzag1, 0), x
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.11 10:07:15