dusktrader
Pine Script Scholar
Pine Script Scholar
Posts: 32
Joined: February 24th, 2021
Contact: TradingView Profile

indicator for measuring the average bar size?

Hello, is there an indicator that can tell me the current average bar size? Say for example the last 20 bars on my chart in the timeframe I'm on?

I'm working on a strategy that currently involves a bit of a dynamic stop. The stop is relative to the size of the prior bar on entry, which means in fast moving markets the stop will be wider.

I am going to research some similar indicators like Parabolic SAR, but I think it would be great if I had a way to get the average bar size for last X bars.

Ideally, as a manual-trading strategy, what I want is an indicator that will calculate the correct stop and position size. This will be constantly changing based on the volatility of the market, so I need i calculated in realtime. I'm sure I can build the indicator but currently I'm not sure how to get the average bar size.

Thanks!

kmarryat
Pine Script Rookie
Pine Script Rookie
Posts: 20
Joined: December 21st, 2020
Contact: TradingView Profile

Re: indicator for measuring the average bar size?

Hi dusktrader,
A common indicator for dynamic stops is ATR (Average True Range), check it out.
Generally a multiple of ATR is used. IE. 2*ATR for Target, 1.5 * ATR for Stop.

Here's a short code snippet to compare a simple average of bar ranges with ATR.

Code: Select all

//@version=4
study("Range",overlay=false)

Range=abs(high-low)
AvgRange=avg(Range[1],Range[2],Range[3],Range[4],Range[5])
plot(AvgRange,color=color.blue)

ATR=atr(5)
plot(ATR,color=color.red)
 
Hope it helps. Good Luck!

dusktrader
Pine Script Scholar
Pine Script Scholar
Posts: 32
Joined: February 24th, 2021
Contact: TradingView Profile

Re: indicator for measuring the average bar size?

Hey thanks! I will give that a try. I am currently using Parabolic SAR and it seems to be working good manually (I am still learning how to code it).

But my strategy requires the SAR from a lower timeframe. I use this for dynamic stop. Currently trying to determine how to gain access to a different timeframe in my pine script. I see the sar reference book entry, but it doesn't mention how to access it on a different timeframe.

dusktrader
Pine Script Scholar
Pine Script Scholar
Posts: 32
Joined: February 24th, 2021
Contact: TradingView Profile

Re: indicator for measuring the average bar size?

Btw I think I found the answer to my own question here - this explains how to access an indicator value from a different timeframe:
https://www.tradingview.com/blog/en/new ... ion-18479/

Return to “General Trading Discussions”