#twitter-follow { border: 0;position: fixed; top: 240px; left:0;} #live-support { border: 0;position: fixed; top: 370px; left:0;} #knowledge-base { border: 0;position: fixed; top: 170px; left:0;}

## Moving Average: Rule-based

by The trader, 5366 days ago

Probably the most popular trading indicator is the moving average. It is widely used in technical analysis and several other indicators use it in their calculation. For each bar, the moving average indicator calculates the average price of a time-series over the preceding N-bars.
The function name is 'rbsma', which stands for 'rule-based simple moving average'. This type of moving average does not calculate an average value of the last N-bars, but for N-bars that satisfy a specific rule.

Here is an example:
Let us say we want to calculate the average close price of for the last five Monday bars. We then should take the last five bars (bars that occurred on Monday) and calculate the average. Then repeat the process for all the available trading bars.

To do this with the 'rbsma' function, you will need to type:
rbsma(close, DayOfWeek() == 1, 5, 25);
The first parameter is the close price series.
The second parameter is the filter rule. It instructs the application to take into consideration only bars whose 'DayOfWeek' value is equal to one, which is Monday.
The third parameter is the number of occurrences to use in the moving average calculation.
The last parameter value defines the maximum number of bars to look for. A low value could result in the 'rbsma' function not finding the five occurrences where the filter rule passed. This could result in the calculation of a moving average with less than five values.
This doesn't mean that that you should put a very high value in this parameter to avoid this kind of problem. A high value generally needs more computation time and more time to calculate the function.

Here are some other examples:
rbsma(close, rsi(14) > 50, 20, 50);
This moving average rule returns the average close price for bars where the security price relative strength index indicator is above 50.

rbsma(volume, close > ref(close, 1), 20, 50);
This moving average rule returns the average volume for bars where the stock or security price closed higher than yesterday's value.

rbsma(volume, close > ref(close, 1), 20, 50) > rbsma(volume, close < ref(close, 1), 20, 50)
This is a moving average rule that returns one on bars where the average volume for the last 20 occurrences where the security price closed higher is greater than the average volume for the last 20 occurrences where the security price closed lower.

To use the 'rbsma' function as a classic moving average, you just need to set the rule formula to '1'.
rbsma(close, 1, 14, 14) is equal to sma(close, 14)

This function can also be modified to return another rule-based indicator; that is, an indicator that uses bars that pass a specific rule.

What is this?

 Type: Trading Indicator Object ID: 324 Country: All Market: All Style: Technical Analysis

 Related objects Empty
Number of reviews
Average rate
Click to rate this item
Number of rates the current object received
Report an object
if you can't run it for example or if it contains errors
Click to report this object

 Random Blog Posts Stock split & dividend Survivorship bias Transaction Costs How scripts communicate with each other How to simulate options strategies Organizing Trading Objects Creating a download item: Initial Jobless Claims How to search for a download item Show All

Number of reviews
Average rate
Click to rate this item
Number of rates the current object received
Report an object
if you can't run it for example or if it contains errors
Click to report this object