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
33
indicator
132
strategy
170

Recent Snippets

Utilizing Arrays to Track and Compare Indicator Values Within the Same Bar in ProBuilder
indicator
This ProBuilder code snippet demonstrates how to use arrays to compare the values of an indicator (RSI in this case) [...]
Calculating Break-Even Points in ProBuilder
strategy
This ProBuilder code snippet demonstrates how to calculate the break-even point for a trading position. The break-even [...]
Implementing Time and Day Restrictions in Trading Algorithms
strategy
This code snippet demonstrates how to set restrictions on trading activities based on specific days of the week and [...]
Implementing Partial Position Closure Based on Price Retracement in ProBuilder
strategy
This code snippet demonstrates how to partially close a trading position when the price retraces to a certain level in [...]
Implementing Single Trade Per Timeframe Logic in ProBuilder
strategy
This code snippet demonstrates how to ensure that only one trade is executed per timeframe in a trading strategy using [...]
Logo Logo
Loading...