Search code examples
pine-scriptalgorithmic-tradingtradingpine-script-v4

How to use crossunder() method to detect where price is interacting with Fibonacci levels in pinescript?


I'm fairly new at coding pinescript and am trying to code a custom alert where if price crosses under a fibonacci golden level (0.382, 0.50, or 0.618) and bar state is confirmed, it will return true and trigger an alert.

FYI, I'm analyzing and building upon TradingView's built-in Auto Fib Retracement Indicator script to use as an alert. I'm having difficulty trying to retrieve the fibonacci values to compare. I notice when using auto fib retracement tool, it plots the fibonacci level (in decimal format) along with the price and I was wondering how do I retrieve it to compare where price has hit the levels within it if that's the correct way to go about it?

Code Snippet (my added alert code from the bult-in indicator):

crossFibGoldenLvls = low < value_0_382 and barstate.isconfirmed // if the 0.382 level price is greater than the current bar low AND bar state is confirmed; thus returns the bar’s final price
// if price cross down thru any of these golden levels (0.382, 0.50, 0.618) AND then 2 green consecutive candles close higher than previous bar close, return true
if crossFibGoldenLvls
    alert("Price has crossed a fib golden level.", alert.freq_once_per_bar_close)
    //alert("Price has crossed a fib golden level:" + tostring(processLevel(show_1, value_1, color_1)), alert.freq_once_per_bar_close)
// else if price cross down thru any of these levels (0.786, 1), return false
// …

//plot(series=processLevel(show_0_382, value_0_382, color_0_382), title="fib level 0.382")
//plot(series=processLevel(value_0_382), title="fib level 0.382")
plotchar(show_0_382, title="show_0_382")
plotchar(value_0_382, title="value_0_382")
//plotchar(value, title="value")
//plotchar(m, title="m")
//plotchar(r, title="r")
plotchar(diff, title="diff")
plotchar(startPrice, title="startPrice")
plotchar(endPrice, title="endPrice")
//plotchar(price, title="price")
//plotchar(crossFibGoldenLvls, title="crossFibGoldenLvls")

Tradingview’s auto fib retracement tool can be found here along with its full source code. The code snippet above is what I added to it so far.


Solution

  • value_0_382 holds the fibonacci level actually (0.382). What you are looking for is calculated within the processLevel() function and named r.

    You can easily check that modifying the code such that processLevel() returns the value of r and plot the returned value.

    processLevel(show, value, colorL) =>
        float m = value
        r = startPrice + diff * m
        if show
            _draw_line(r, colorL)
            _draw_label(r, _label_txt(m, r), colorL)
            if _crossing_level(close, r)
                alert("Autofib: " + syminfo.ticker + " crossing level " + tostring(value))
        r
    
    pl = processLevel(show_0_382, value_0_382, color_0_382)
    plot(pl)
    

    enter image description here

    Then use this return value in your condition:

    crossFibGoldenLvls = low < pl and barstate.isconfirmed