βœ–

Home Forums Trading view How can I create support and resistance levels automatically in Pine Script?

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

  • Author
    Posts
    • 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?

    • 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.

Viewing 1 reply thread
×

Start a Discussion

Get help from the AlgoDelta community.

×

Welcome Back!

Enter your email to sign in or create an account. No passwords needed.

⚠️

Delete This?

Are you sure you want to delete this? This action is permanent and cannot be undone.

Scroll to Top