How can I create support and resistance levels automatically in Pine Script?

Asked by Radhesh Joshi · SOLVED
1 reply 473 views Jump to solution Reply
Radhesh Joshi Original Post
Strategy Builder

I’m creating a TradingView indicator and currently I have to manually identify and draw support and resistance levels on the chart.

I want my Pine Script to detect important price levels automatically and plot them on the chart. What is a simple way to identify swing highs and swing lows and use them as resistance and support?

Posted Aug 8
Reply

    1 Reply

    Tech Support AlgoDelta
    Tech Support AlgoDelta Automation Expert ·

    Yes sir, you can create automatic support and resistance levels using pivot highs and pivot lows.

    Pine Script provides ta.pivothigh() and ta.pivotlow() to identify these points.

    A simple example:

    //@version=6
    indicator("Auto Support Resistance", overlay=true)
    
    leftBars = 5
    rightBars = 5
    
    pivotHigh = ta.pivothigh(high, leftBars, rightBars)
    pivotLow = ta.pivotlow(low, leftBars, rightBars)
    
    plot(pivotHigh, "Resistance", color=color.red, style=plot.style_linebr)
    plot(pivotLow, "Support", color=color.green, style=plot.style_linebr)

    Here:

    –ta.pivothigh() identifies a swing high that can be treated as a resistance level.

    –ta.pivotlow() identifies a swing low that can be treated as a support level.

    -The leftBars and rightBars values determine how many candles are used to confirm the pivot.

    You can also use line.new() if you want to draw and extend the detected support and resistance levels dynamically instead of simply plotting them.

    One important point is that pivot levels are confirmed only after the required number of candles on the right side has formed. So the level will not be identified immediately at the exact candle where the high or low occurs.

    => This approach can be extended further to keep multiple historical support and resistance levels, remove old levels, or create zones instead of single price lines.

    So, by combining pivot detection with drawing functions such as line.new() or box.new(), you can build a fully automatic support and resistance indicator.

    Verified Solution
      Markdown supported · ```json for code blocks · paste or drop screenshots

      Be specific and kind. Never share API keys, passwords or personal account details.

      Welcome to the community

      Log in or create your account in under a minute.

      1. 1Mobile
      2. 2E-mail
      3. 3Password
      +91

      Have a password?

      By continuing you agree to the community guidelines. We’ll send a one-time code by SMS.