首页 > 解决方案 > 如何为此形态指标添加警报条件?

问题描述

我修改了一个开源脚本来创建一个 ABCD 模式指标。现在我想添加一个仅适用于“ABCD 看跌形态”的警报条件,不适用于 ABCD 看涨形态。

ABCD 看跌和 ABCD 看涨模式均由函数声明运算符定义。我想知道是否有任何方法可以在此链接警报条件,或者如果没有,我如何将它们从函数声明运算符中取出?

有人可以帮我吗?请看下面的代码。

谢谢!

//@version=4
study("Abcd pattern indicator with alerts", shorttitle="Abcd pattern alerts", overlay=true, max_bars_back=1000, max_lines_count=500, max_labels_count=500)

max_pivot_size = input(100, step=10)

showZigZag1 = input(true)
zigzag1Length = input(5, step=5, minval=1)
zigzag1Color = input(color.teal)
zigzag1Width = 1
zigzag1Style = line.style_solid

showZigZag2 = input(true)
zigzag2Length = input(10, step=5, minval=1)
zigzag2Color = input(color.olive)
zigzag2Width = 1
zigzag2Style = line.style_solid

showZigZag3 = input(true)
zigzag3Length = input(15, step=5, minval=1)
zigzag3Color = input(color.lime)
zigzag3Width = 1
zigzag3Style = line.style_solid

showZigZag4 = input(true)
zigzag4Length = input(20, step=5, minval=1)
zigzag4Color = input(color.fuchsia)
zigzag4Width = 1
zigzag4Style = line.style_solid


errorPercent = input(5, minval=5, step=5, maxval=20)
MaxRiskPerReward = input(30, title="Max Risk Per Reward (Double Top/Bottom)", step=10, minval=0)

showStatTable = false
waitForConfirmation = input(false)

bullishColor = input(color.green)
bearishColor = input(color.red)

err_min = (100-errorPercent)/100
err_max = (100+errorPercent)/100

var zigzagpivots1 = array.new_float(0)
var zigzagpivotbars1 = array.new_int(0)
var zigzagpivotdirs1 = array.new_int(0)

var zigzagpivots2 = array.new_float(0)
var zigzagpivotbars2 = array.new_int(0)
var zigzagpivotdirs2 = array.new_int(0)

var zigzagpivots3 = array.new_float(0)
var zigzagpivotbars3 = array.new_int(0)
var zigzagpivotdirs3 = array.new_int(0)

var zigzagpivots4 = array.new_float(0)
var zigzagpivotbars4 = array.new_int(0)
var zigzagpivotdirs4 = array.new_int(0)

var wmlines1 = array.new_line(8)
var wmtype1 = array.new_int(2,1)
var wmLabels1 = array.new_bool(13, false)
var wmLabel1 = array.new_label(1)

var wmlines2 = array.new_line(8)
var wmtype2 = array.new_int(2,1)
var wmLabels2 = array.new_bool(13, false)
var wmLabel2 = array.new_label(1)

var wmlines3 = array.new_line(8)
var wmtype3 = array.new_int(2,1)
var wmLabels3 = array.new_bool(13, false)
var wmLabel3 = array.new_label(1)

var wmlines4 = array.new_line(8)
var wmtype4 = array.new_int(2,1)
var wmLabels4 = array.new_bool(13, false)
var wmLabel4 = array.new_label(1)

pivots(length)=>
    float phigh = highestbars(high, length) == 0 ? high : na
    float plow = lowestbars(low, length) == 0 ? low : na
    dir = 0
    dir := iff(phigh and na(plow), 1, iff(plow and na(phigh), -1, dir[1]))
    [dir, phigh, plow]

zigzag(length, zigzagpivots, zigzagpivotbars, zigzagpivotdirs)=>
    [dir, phigh, plow] = pivots(length)
    dirchanged = change(dir)
    
    if(phigh or plow)
        value = (dir == 1? phigh : plow)
        bar = bar_index
        newDir = dir
        if(not dirchanged and array.size(zigzagpivots) >=1)
            pivot = array.shift(zigzagpivots)
            pivotbar = array.shift(zigzagpivotbars)
            pivotdir = array.shift(zigzagpivotdirs)
            useNewValues = value*pivotdir < pivot*pivotdir
            value:= useNewValues? pivot : value
            bar := useNewValues? pivotbar : bar
        
        if(array.size(zigzagpivots) >=2)
            LastPoint = array.get(zigzagpivots,1)
            newDir := dir*value > dir*LastPoint? dir*2 : dir
        
        array.unshift(zigzagpivots, value = value)
        array.unshift(zigzagpivotbars, bar)
        array.unshift(zigzagpivotdirs, newDir)
        
        if(array.size(zigzagpivots) > max_pivot_size)
            array.pop(zigzagpivots)
            array.pop(zigzagpivotbars)
            array.pop(zigzagpivotdirs)

abcdClassicBEARISH = input(true)
abcdClassicBULLISH = input(true)


get_harmonic_label_abcd_pattern_bearish(wmLabels, dir, price, bar)=>
    isAbcd_bearish = array.get(wmLabels, 9)
    labelText_bearish = "ABCD BEARISH"
    trendColor_bearish = bearishColor

    baseLabel_bearish = label.new(x=bar, y=price, text=labelText_bearish, yloc=yloc.price,
                             color=trendColor_bearish, 
                             style=label.style_label_down,
                             textcolor=color.black, size=size.normal)
    baseLabel_bearish

get_harmonic_label_abcd_pattern_bullish(wmLabels, dir, price, bar)=>
    isAbcd_bullish = array.get(wmLabels, 9)
    labelText_bullish = "ABCD BULLISH"
    trendColor_bullish = bullishColor

    baseLabel_bullish = label.new(x=bar, y=price, text=labelText_bullish, yloc=yloc.price,
                             color=trendColor_bullish, 
                             style=label.style_label_up,
                             textcolor=color.black, size=size.normal)
    baseLabel_bullish



detect_harmonic_pattern(zigzagpivots, zigzagpivotbars, zigzagpivotdirs, wmlines, wmlabel, wmtype, wmLabels, zigzagColor, zigzagWidth, zigzagStyle, showZigZag)=>
    start = waitForConfirmation? 1 : 0
    wm_pattern = false
//    abcd_pattern = false
    abcd_pattern_bearish = false
    abcd_pattern_bullish = false
    double_pattern = false
    if(array.size(zigzagpivots) >= 6+start and showZigZag)
        
        d = array.get(zigzagpivots, start+0)
        dBar = array.get(zigzagpivotbars, start+0)
        dDir = array.get(zigzagpivotdirs, start+0)
        
        c = array.get(zigzagpivots, start+1)
        cBar = array.get(zigzagpivotbars, start+1)
        cDir = array.get(zigzagpivotdirs, start+1)
        
        b = array.get(zigzagpivots, start+2)
        bBar = array.get(zigzagpivotbars, start+2)
        bDir = array.get(zigzagpivotdirs, start+2)
        
        a = array.get(zigzagpivots, start+3)
        aBar = array.get(zigzagpivotbars, start+3)
        aDir = array.get(zigzagpivotdirs, start+3)
        
        x = array.get(zigzagpivots, start+4)
        xBar = array.get(zigzagpivotbars, start+4)
        xDir = array.get(zigzagpivotdirs, start+4)

        y = array.get(zigzagpivots, start+5)
        yBar = array.get(zigzagpivotbars, start+5)
        yDir = array.get(zigzagpivotdirs, start+5)
        
        highPoint = max(x,a,b,c,d)
        lowPoint = min(x,a,b,c,d)
        dir = c > d? 1 : -1

        xabRatio = abs(b-a)/abs(x-a)
        abcRatio = abs(c-b)/abs(a-b)
        bcdRatio = abs(d-c)/abs(b-c)
        xadRatio = abs(d-a)/abs(x-a)
        yxaRatio = abs(a-x)/abs(y-x)
        
        abTime = abs(aBar - bBar)
        cdTime = abs(cBar - dBar)
        abPrice = abs(a-b)
        cdPrice = abs(c-d)
        
        time_ratio = cdTime/abTime
        price_ratio = cdPrice/abPrice
//        abcdDirection = (a < b and a < c and c < b and c < d and a < d and b < d)? 1 : (a > b and a > c and c > b and c > d and a > d and b > d) ? -1 : 0
        abcdDirection_bearish = (a < b and a < c and c < b and c < d and a < d and b < d)
        abcdDirection_bullish = (a > b and a > c and c > b and c > d and a > d and b > d)
        
        risk = abs(b-d)
        reward = abs(c-d)
        riskPerReward = risk*100/(risk+reward)
        
        if(b < highPoint and b > lowPoint)
            //abcdClassicBEARISH
            if(abcdClassicBEARISH and abcRatio >= 0.618*err_min and abcRatio <= 0.786*err_max and bcdRatio >= 1.272*err_min and bcdRatio <= 1.618*err_max and abcdDirection_bearish != 0)
                abcd_pattern_bearish := true
                array.set(wmLabels, 9, true)
            else
                array.set(wmLabels, 9, false)
            //abcdClassicBULLISH
            if(abcdClassicBULLISH and abcRatio >= 0.618*err_min and abcRatio <= 0.786*err_max and bcdRatio >= 1.272*err_min and bcdRatio <= 1.618*err_max and abcdDirection_bullish != 0)
                abcd_pattern_bullish := true
                array.set(wmLabels, 9, true)
            else
                array.set(wmLabels, 9, false)
        
        cancelW = false
        cancelA = false
        cancelD = false
        if(wm_pattern[1] and x==x[1] and a==a[1] and b==b[1] and c==c[1])
            line.delete(array.get(wmlines,0))
            line.delete(array.get(wmlines,1))
            line.delete(array.get(wmlines,2))
            line.delete(array.get(wmlines,3))
            line.delete(array.get(wmlines,4))
            line.delete(array.get(wmlines,5))
            line.delete(array.get(wmlines,6))
            line.delete(array.get(wmlines,7))
            label.delete(array.get(wmlabel,0))
            cancelW := true
        
        if(abcd_pattern_bearish[1] and a==a[1] and b==b[1] and c==c[1])
            line.delete(array.get(wmlines,1))
            line.delete(array.get(wmlines,2))
            line.delete(array.get(wmlines,3))
            label.delete(array.get(wmlabel,0))
            cancelA := true
            
        if(abcd_pattern_bullish[1] and a==a[1] and b==b[1] and c==c[1])
            line.delete(array.get(wmlines,1))
            line.delete(array.get(wmlines,2))
            line.delete(array.get(wmlines,3))
            label.delete(array.get(wmlabel,0))
            cancelA := true
        
        if(double_pattern[1] and a==a[1] and b==b[1] and c==c[1])
            line.delete(array.get(wmlines,5))
            label.delete(array.get(wmlabel,0))
            cancelD := true
        
        if(wm_pattern)
            xa = line.new(y1=x, y2=a, x1=xBar, x2=aBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
            ab = line.new(y1=a, y2=b, x1=aBar, x2=bBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
            bc = line.new(y1=b, y2=c, x1=bBar, x2=cBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
            cd = line.new(y1=c, y2=d, x1=cBar, x2=dBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
            xb = line.new(y1=x, y2=b, x1=xBar, x2=bBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
            bd = line.new(y1=b, y2=d, x1=bBar, x2=dBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
            xd = line.new(y1=x, y2=d, x1=xBar, x2=dBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
            ac = line.new(y1=a, y2=c, x1=aBar, x2=cBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
            array.set(wmlines, 0, xa)
            array.set(wmlines, 1, ab)
            array.set(wmlines, 2, bc)
            array.set(wmlines, 3, cd)
            array.set(wmlines, 4, xb)
            array.set(wmlines, 5, bd)
            array.set(wmlines, 6, xd)
            array.set(wmlines, 7, ac)
            array.set(wmtype, 0, dir)
        if(abcd_pattern_bearish and not wm_pattern)
            ab = line.new(y1=a, y2=b, x1=aBar, x2=bBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
            bc = line.new(y1=b, y2=c, x1=bBar, x2=cBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
            cd = line.new(y1=c, y2=d, x1=cBar, x2=dBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
            array.set(wmlines, 1, ab)
            array.set(wmlines, 2, bc)
            array.set(wmlines, 3, cd)
            array.set(wmtype, 0, dir)
        if(abcd_pattern_bullish and not wm_pattern)
            ab = line.new(y1=a, y2=b, x1=aBar, x2=bBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
            bc = line.new(y1=b, y2=c, x1=bBar, x2=cBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
            cd = line.new(y1=c, y2=d, x1=cBar, x2=dBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
            array.set(wmlines, 1, ab)
            array.set(wmlines, 2, bc)
            array.set(wmlines, 3, cd)
            array.set(wmtype, 0, dir)
        if(double_pattern and not wm_pattern)
            bd = line.new(y1=b, y2=d, x1=bBar, x2=dBar, color=zigzagColor, width=zigzagWidth, style=zigzagStyle)
            array.set(wmlines, 5, bd)
            array.set(wmtype, 0, dir)
            
        if(wm_pattern or abcd_pattern_bearish or double_pattern)
            array.set(wmlabel, 0, get_harmonic_label_abcd_pattern_bearish(wmLabels, dir, d, dBar))
        if(wm_pattern or abcd_pattern_bullish or double_pattern)
            array.set(wmlabel, 0, get_harmonic_label_abcd_pattern_bullish(wmLabels, dir, d, dBar))

    pattern = (wm_pattern and not wm_pattern[1]) or (abcd_pattern_bearish and not abcd_pattern_bearish[1]) or (abcd_pattern_bullish and not abcd_pattern_bullish[1]) or (double_pattern and not double_pattern[1])
    pattern

zigzag(zigzag1Length, zigzagpivots1, zigzagpivotbars1, zigzagpivotdirs1)
zigzag(zigzag2Length, zigzagpivots2, zigzagpivotbars2, zigzagpivotdirs2)
zigzag(zigzag3Length, zigzagpivots3, zigzagpivotbars3, zigzagpivotdirs3)
zigzag(zigzag4Length, zigzagpivots4, zigzagpivotbars4, zigzagpivotdirs4)

wm_pattern1 = detect_harmonic_pattern(zigzagpivots1, zigzagpivotbars1, zigzagpivotdirs1, wmlines1, wmLabel1, wmtype1, wmLabels1, zigzag1Color, zigzag1Width, zigzag1Style, showZigZag1)
wm_pattern2 = detect_harmonic_pattern(zigzagpivots2, zigzagpivotbars2, zigzagpivotdirs2, wmlines2, wmLabel2, wmtype2, wmLabels2, zigzag2Color, zigzag2Width, zigzag2Style, showZigZag2)
wm_pattern3 = detect_harmonic_pattern(zigzagpivots3, zigzagpivotbars3, zigzagpivotdirs3, wmlines3, wmLabel3, wmtype3, wmLabels3, zigzag3Color, zigzag3Width, zigzag3Style, showZigZag3)
wm_pattern4 = detect_harmonic_pattern(zigzagpivots4, zigzagpivotbars4, zigzagpivotdirs4, wmlines4, wmLabel4, wmtype4, wmLabels4, zigzag4Color, zigzag4Width, zigzag4Style, showZigZag4)

// alert condition for both abcd bearish and abcd bullish
alertcondition(wm_pattern1 or wm_pattern2 or wm_pattern3 or wm_pattern4, title='New harmonic pattern alert', message='New harmonic pattern detected on {{ticker}}')

// Question : How can I create an alert condition for just '' Abcd bearish '', and not for '' Abcd bullish ''?
// Thank you!

标签: pine-script

解决方案


推荐阅读