Implementing Exponential Moving Average Filters with Custom Parameters in ProBuilder

08 Feb 2016
0 comment
0 attachment

This code snippet demonstrates how to implement a moving average filter in ProBuilder, allowing the user to select the type of average, the data point (custom close), and the maximum period for the average calculation. This can be particularly useful in creating flexible trading strategies or indicators.


type = 1
maxaverage = 32
j = 1
while j < maxaverage
    j = j * 2
    ave1 = average[j,type](customclose)
    ave2 = average[j*2,type](customclose)
    if ave1 > ave2 then
        upflag = 1
    else
        upflag = 0
        break
    endif
wend
j = 1
while j < maxaverage
    j = j * 2
    ave1 = average[j,type](customclose)
    ave2 = average[j*2,type](customclose)
    if ave1 < ave2 then
        downflag = -1
    else
        downflag = 0
        break
    endif
wend
return upflag coloured(0,128,0) style(histogram,2), downflag coloured(128,0,0) style(histogram,2)

This code snippet is structured to calculate and compare moving averages of increasing periods, doubling each time, up to a specified maximum period. It sets flags based on the comparison of each pair of averages.

  • Initialization: The variables type and maxaverage are set to define the type of moving average and the maximum period to calculate.
  • First Loop: This loop calculates moving averages for periods that double each iteration (2, 4, 8, etc.). It compares each moving average with the next longer period's moving average. If the shorter period average is greater, it sets upflag to 1; otherwise, it breaks out of the loop setting upflag to 0.
  • Second Loop: Similar to the first, but checks if the shorter period average is less than the longer period average to set downflag to -1. If not, it sets downflag to 0 and breaks the loop.
  • Output: The function returns the upflag and downflag with specific colors and styles for histogram representation. The upflag is colored green and the downflag is colored red.

This example is useful for understanding how to manipulate and compare data series dynamically in ProBuilder, which can be applied to various algorithmic trading strategies.

Related Post

Check out this related content for more information:

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

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
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...