Welcome to the BEST forum for traders

Did you know we're sharing tons of exclusive content here?
Our community is growing quickly. Take a look around and say "Hello".

  • Forums
  • Pinescript
  • Having trouble with an if statement calling up an auto fib retracement.

Having trouble with an if statement calling up an auto fib retracement.

Jun 27, 2020 - 11:39 PM

Viewed 895 times

https://best-trading-indicator.com/community/bti/forums/4180/topics/17596 COPY
  • I'm trying to make make a script for an if ema2<=ma
    then call an auto fib retracement but it keeps giving me syntax errors(i.e. line 66: Syntax error at input '=>'.) and I have no idea what to do about this, I've tried changing it to maybe '<=', but then it just gives me continuation errors, and no amount of indenting will fix it! Thanks in advance.

    study(shorttitle="Crossover Event", title="Test EMA&MA Crossover", overlay=true)
    
    //Inputs
    
    EMA1=input(title="EMA 1", type=input.integer, defval=12, minval=1)
    EMA2= input(title="EMA 2", type=input.integer, defval=21, minval=1)
    EMA3= input(title="EMA 3", type=input.integer, defval=55, minval=1)
    
    MA= input(title="MA", type=input.integer, defval=200, minval=1)
    
    FibRetracement= input(title="AutoFibRetracement", type=input.bool, defval=true)
    
    
    //Get Data
    
    ema1= ema(close, EMA1)
    ema2= ema(close, EMA2)
    ema3= ema(close, EMA3)
    ma= wma(close, MA)
    
    //Plot
    plot(close, color=color.fuchsia, linewidth=1)
    plot(ema1, title="Resistance", color=color.red, linewidth=2, transp=10)
    plot(ema2, title="MainLine", color=color.white, linewidth=1, transp=70)
    plot(ema3, title="Support", color=color.green, linewidth=2, transp=10)
    plot(ma, title="Bull/Bear Indicator", color=color.blue, linewidth=3, transp=75)
    
    
    
    
    
    //AutoFibRetracement
    
    if ema2<=ma
        threshold_multiplier = input(title="Deviation", type=input.float, defval=3, minval=0)
        dev_threshold = atr(10) / close * 100 * threshold_multiplier
    
        depth = input(title="Depth", type=input.integer, defval=10, minval=1)
        var extendLeft = input(false, "Extend Lines Left")
        var extendRight = input(true, "Extend Lines Right")
    
        var extending = extend.none
        if extendLeft and extendRight
            extending := extend.both
        if extendLeft and not extendRight 
            extending := extend.left
        if not extendLeft and extendRight
            extending := extend.right
    
        reverse = input(false, "Reverse")
        prices = input(true, "Prices")
        levels = input(true, "Levels")
        levelsFormat = input("Values", "Levels Format", options = ["Values", "Percent"])
        labelsPosition = input("Left", "Labels Position", options = ["Left", "Right"])
    
        var line lineLast = na
        var int iLast = 0
        var int iPrev = 0
        var float pLast = 0
        var isHighLast = false // otherwise the last pivot is a low pivot
    
        pivots(src, length, isHigh) => 
            l2 = length * 2 
            c = nz(src[length])
            ok = true
            for i = 0 to l2
                if isHigh and src[i] > c
                    ok := false
    
                if not isHigh and src[i] < c
                    ok := false
            if ok
                [bar_index[length], c]
            else
                [int(na), float(na)]
        [iH, pH] = pivots(high, depth / 2, true)
        [iL, pL] = pivots(low, depth / 2, false)
    
        calc_dev(base_price, price) =>
            100 * (price - base_price) / price
    
        pivotFound(dev, isHigh, index, price) => 
            if isHighLast == isHigh and not na(lineLast)
                // same direction
                if isHighLast ? price > pLast : price < pLast
                    line.set_xy2(lineLast, index, price)
                    [lineLast, isHighLast]
                else
                    [line(na), bool(na)]
            else // reverse the direction (or create the very first line)
                if abs(dev) > dev_threshold
                    // price move is significant
                    id = line.new(iLast, pLast, index, price, color=color.gray, width=1, style=line.style_dashed)
                    [id, isHigh]
                else
                    [line(na), bool(na)]
    
        if not na(iH)
            dev = calc_dev(pLast, pH)
            [id, isHigh] = pivotFound(dev, true, iH, pH)
            if not na(id)
                if id != lineLast
                    line.delete(lineLast)
                lineLast := id
                isHighLast := isHigh
                iPrev := iLast
                iLast := iH
                pLast := pH
        else
            if not na(iL)
                dev = calc_dev(pLast, pL)
                [id, isHigh] = pivotFound(dev, false, iL, pL)
                if not na(id)
                    if id != lineLast
                        line.delete(lineLast)
                    lineLast := id
                    isHighLast := isHigh
                    iPrev := iLast
                    iLast := iL
                    pLast := pL
    
        _draw_line(price, col) =>
            var id = line.new(iLast, price, bar_index, price, color=col, width=1, extend=extending)
            if not na(lineLast)
                line.set_xy1(id, line.get_x1(lineLast), price)
                line.set_xy2(id, line.get_x2(lineLast), price)
    
    
        _draw_label(price, txt, txtColor) =>
            x = labelsPosition == "Left" ? line.get_x1(lineLast) : line.get_x2(lineLast)
            var id = label.new(x=x, y=price, text=txt, textcolor=txtColor, style=label.style_none)
            label.set_xy(id, x, price)
            label.set_text(id, txt)
            label.set_textcolor(id, txtColor)
    
        _wrap(txt) =>
            "(" + tostring(txt, "#.##") + ")"
    
        _label_txt(level, price) =>
            l = levelsFormat == "Values" ? tostring(level) : tostring(level * 100) + "%"
            (levels ? l : "") + (prices ? _wrap(price) : "")
    
        sl1 = input(true, "0")
        vall1 = 0.000
    
        sl2 = input(true, "0.236")
        vall2 = 0.236
    
        sl3 = input(true, "0.382")
        vall3 = 0.382
    
        sl4 = input(true, "0.5")
        vall4 = 0.5
    
        sl5 = input(true, "0.618")
        vall5 = 0.618
    
        sl6 = input(true, "0.786")
        vall6 = 0.786
    
        sl7 = input(true, "1")
        vall7 = 1
    
        sl8 = input(true, "1.272")
        vall8 = 1.272
    
        sl9 = input(true, "1.414")
        vall9 = 1.414
    
        sl10 = input(true, "1.618")
        vall10 = 1.618
    
        sl11 = input(false, "2.618")
        vall11 = 2.618
    
        sl12 = input(false, "3.618")
        vall12 = 3.618
    
        sl13 = input(false, "4.236")
        vall13 = 4.236
    
        sl14 = input(false, "-0.236")
        vall14 = -0.236
    
        sl15 = input(false, "-0.382")
        vall15 = -0.382
    
        sl16 = input(false, "-0.618")
        vall16 = -0.618
    
        _draw_retracement(startPrice, endPrice) =>
            iHL = startPrice > endPrice
            diff = (iHL ? -1 : 1) * abs(startPrice - endPrice)
            if sl1
                l1 = startPrice + diff * vall1
                _draw_line(l1, #808080)
                _draw_label(l1, _label_txt(vall1, l1), #808080)
    
            if sl2
                l2 = startPrice + diff * vall2
                _draw_line(l2, #a61c00)
                _draw_label(l2, _label_txt(vall2, l2), #a61c00)
    
            if sl3
                l3 = startPrice + diff * vall3
                _draw_line(l3, #95cc28)
                _draw_label(l3, _label_txt(vall3, l3), #95cc28)
    
            if sl4
                l4 = startPrice + diff * vall4
                _draw_line(l4, #28cc28)
                _draw_label(l4, _label_txt(vall4, l4), #28cc28)
    
            if sl5
                l5 = startPrice + diff * vall5
                _draw_line(l5, #28cc95)
                _draw_label(l5, _label_txt(vall5, l5), #28cc95)
    
            if sl6
                l6 = startPrice + diff * vall6
                _draw_line(l6, #2895cc)
                _draw_label(l6, _label_txt(vall6, l6), #2895cc)
    
            if sl7
                l7 = startPrice + diff * vall7
                _draw_line(l7, #808080)
                _draw_label(l7, _label_txt(vall7, l7), #808080)
    
            if sl8
                l8 = startPrice + diff * vall8
                _draw_line(l8, #82CA89)
                _draw_label(l8, _label_txt(vall8, l8), #82CA89)
    
            if sl9
                l9 = startPrice + diff * vall9
                _draw_line(l9, #F32C42)
                _draw_label(l9, _label_txt(vall9, l9), #F32C42)
    
            if sl10
                l10 = startPrice + diff * vall10
                _draw_line(l10, #2796ED)
                _draw_label(l10, _label_txt(vall10, l10), #2796ED)
    
            if sl11
                l11 = startPrice + diff * vall11
                _draw_line(l11, #a61c00)
                _draw_label(l11, _label_txt(vall11, l11), #a61c00)
    
            if sl12
                l12 = startPrice + diff * vall12
                _draw_line(l12, #9B03AE)
                _draw_label(l12, _label_txt(vall12, l12), #9B03AE)
    
            if sl13
                l13 = startPrice + diff * vall13
                _draw_line(l13, #E80065)
                _draw_label(l13, _label_txt(vall13, l13), #E80065)
    
            if sl14
                l14 = startPrice + diff * vall14
                _draw_line(l14, #a61c00)
                _draw_label(l14, _label_txt(vall14, l14), #a61c00)
    
            if sl15
                l15 = startPrice + diff * vall15
                _draw_line(l15, #95cc28)
                _draw_label(l15, _label_txt(vall15, l15), #95cc28)
    
            if sl16
                l16 = startPrice + diff * vall16
                _draw_line(l16, #28cc95)
                _draw_label(l16, _label_txt(vall16, l16), #28cc95)
    
        p1 = reverse ? line.get_y1(lineLast) : pLast
        p2 = reverse ? pLast : line.get_y1(lineLast)
        _draw_retracement(p1, p2)
    
    0
  • I think you're missing the
    //@version=4
    at the beginning of your code

    Without this line, none of your code above will work anyway

    Also based on current indentation, seems to be you're the declaring _draw_retracement function inside your ema2 > ma if condition
    Functions cannot be declared inside if or else conditions. They have to be declared outside

    0

    Dave - Helping traders becoming the best version of themselves
CONTENTS