Implementing a Multi-Level Moving Average Filter

14 Dec 2023
0 comment
0 attachment

This code snippet demonstrates how to implement a multi-level moving average filter to determine the trend direction in a series of data points, typically used in time series analysis. The filter uses four moving averages with increasing periods to assess the trend.


p1 = average[t1](close)
p2 = average[t1*2](close)
p3 = average[t1*4](close)
p4 = average[t1*8](close)
a1 = p1 > p2 and p2 > p3 and p3 > p4
a2 = p1 < p2 and p2 < p3 and p3 < p4
if a1 then
    b=1
elsif not a1 and not a2 then
    b=0
elsif a2 then
    b=-1
endif
return b

Explanation of the Code:

  • Initialization of Variables: The code starts by calculating four moving averages of the 'close' price over different periods. The periods are defined as t1, t1*2, t1*4, and t1*8. By default, t1 is set to 2.
  • Comparison of Moving Averages: Two boolean variables, a1 and a2, are defined to check the order of these moving averages. a1 checks if each moving average is greater than the next (indicating an upward trend), while a2 checks if each is less than the next (indicating a downward trend).
  • Decision Logic: The code then uses conditional statements to assign a value to b. If a1 is true (b=1), it suggests a strong upward trend. If a2 is true (b=-1), it indicates a strong downward trend. If neither condition is met, b is set to 0, suggesting a lack of clear trend.
  • Output: Finally, the value of b is returned, which can be used to indicate the trend direction in further analysis or trading strategies.

This approach is useful for identifying the strength and direction of trends based on the relative positions of multiple moving averages.

Related Post

Check out this related content for more information:

https://www.prorealcode.com/topic/very-simple-ma-filter/#post-97302

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.
jebus89 Master
This author is like an anonymous function, present but not directly identifiable. More details on this code architect as soon as they exit 'incognito' mode.
Author’s Profile

Comments

Search Snippets

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

Snippets Categories

global
35
indicator
133
strategy
171

Recent Snippets

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 [...]
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) [...]
Logo Logo
Loading...