Calculating and Tracking Maximum and Minimum Spreads Between Moving Averages

05 Jul 2024
0 comment
0 attachment

This code snippet is designed to calculate the percentage spread between the highest and lowest moving averages on a chart and track the historical maximum and minimum values of this spread. It uses three different types of moving averages: two exponential and one simple moving average.


a = exponentialaverage[6]
b = average[10]
c = exponentialaverage[20]
upper = max(max(a,b),c)
lower = min(min(a,b),c)
d = ((upper - lower) / close) *100

if c > 0 then
    if minapart = 0 then
        minapart = d
    endif
    maxapart = max(d,maxapart)
    minapart = min(d,minapart)
endif

return d as "% MA Zone", maxapart as "max MA spread", minapart as "min MA spread"

Explanation of the Code:

  • Initialization of Moving Averages: The variables a, b, and c are assigned the values of an exponential moving average over 6 periods, a simple moving average over 10 periods, and another exponential moving average over 20 periods, respectively.
  • Calculating Upper and Lower Bounds: The upper variable finds the maximum value among the three averages, and the lower variable finds the minimum value among them.
  • Spread Calculation: The variable d calculates the percentage difference between the upper and lower moving averages relative to the current closing price.
  • Tracking Maximum and Minimum Spreads: The code checks if the third moving average c is greater than zero to ensure valid data. It initializes minapart with the value of d if it’s the first calculation (i.e., minapart is zero). It then updates maxapart and minapart to store the maximum and minimum values of d observed historically.
  • Output: The function returns the current spread percentage as “% MA Zone”, and the historical maximum and minimum spreads as “max MA spread” and “min MA spread”, respectively.

This snippet is useful for analyzing the variability and convergence/divergence of different moving averages over time, providing insights into market trends and volatility.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/ma-scan/#post-111276

Visit Link
What is a Snippet? A snippet is a small, reusable chunk of code designed to solve specific tasks quickly. Think of it as a shortcut that helps you achieve your coding goals without reinventing the wheel. How to Use: Simply copy the snippet and paste it into your project where needed. Don't forget to tweak it to fit your context. Snippets are not just time-savers; they're also learning tools to help you become a more efficient coder.
Vonasi Master
V-oyaging ON A S-mall I-ncome
Author’s Profile

Comments

Search Snippets

Showing some results...
Sorry, no result found!

Snippets Categories

global
35
indicator
134
strategy
171

Recent Snippets

How to Track Up/Down Cumulative Volume Inside Each Candle (Tick Volume Delta)
indicator
This snippet separates the real-time, intra-candle volume into up-volume and down-volume based on whether the last [...]
How to Create a Simple MTF Trend Dashboard with EMA and SMA
indicator
This indicator builds a compact multi-timeframe (MTF) dashboard that shows whether price is trading above or below a [...]
How to Display Per-Bar Volume Accumulation in Real Time (Intrabar Updates)
global
This snippet tracks and displays the current bar’s accumulated volume while the bar is still forming, instead of only [...]
Ticks Counter: Count Tick Updates Per Bar on Tick or Time Charts
global
This snippet counts how many tick updates have occurred for the current bar by incrementing a per-bar counter on each [...]
How to Build a Step-Based Trailing Stop That Moves to Break-Even First
strategy
This snippet implements a step trailing stop that advances in fixed increments once price reaches predefined profit [...]
Logo Logo
Loading...