OPEN-SOURCE SCRIPT

Groupped SMA with Custom Lenght by Mustafa KAPUZ

This script is a custom implementation of grouped Simple Moving Averages (SMA) with added Bollinger Bands

Key Features:

Customizable Length (n): Allows users to set the length for the SMA calculation. The length is the number of bars used to calculate the average. This is input by the user and can be adjusted to analyze different time periods.
Standard Deviation Multiplier (StdDev): This input allows users to customize the width of the Bollinger Bands. A higher multiplier results in wider bands, and a lower multiplier results in narrower bands.
Grouped Calculation: Instead of calculating the SMA on a rolling basis, this script groups the data in sets of n (as per the user-defined length) and calculates the SMA for each group. After each group, the calculation resets.
Bollinger Bands: Based on the SMA calculated for each group, the script calculates the standard deviation of prices within the group. Using the standard deviation and the standard deviation multiplier (StdDev), it computes the upper and lower Bollinger Bands.
Dynamic Visualization: For each completed group of n bars, the script draws lines on the chart representing the upper band, lower band, and the SMA itself. These lines help visualize the volatility and the average price level for each group.

How It Works:

Data Grouping: For every candle/bar on the chart, the script sums up the closing prices and counts the number of bars until it reaches the user-defined length (n). It stores closing prices in an array for further calculations.
Average Calculation: Once the count reaches n, it calculates the average closing price for the group and resets the sum and count for the next group.
Standard Deviation and Bollinger Bands: With the average calculated, it then computes the standard deviation of the closing prices within the group. This standard deviation, multiplied by the user-defined StdDev multiplier, determines the distance of the Bollinger Bands from the average.
Drawing Lines: Finally, the script visually represents these calculations on the chart by drawing lines for the upper band, lower band, and the average itself for each group.

Purpose and Use:

This script is useful for traders and analysts who prefer to examine price movements and volatility in fixed intervals or groups of bars, rather than the continuous rolling averages provided by traditional SMA and Bollinger Band indicators. By adjusting the length and standard deviation multiplier, users can tailor the indicator to fit various trading strategies, time frames, and market conditions. This grouped approach can provide unique insights into market trends, potential reversals, and volatility patterns that might not be as evident with standard indicators.
Bands and Channels

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer