PureBytes Links
Trading Reference Links
|
You are making this much harder than it is. The code is:
If CurrentBar <= 1 Then Begin
Factor = 2 / (Length + 1);
XAverage = Price;
End Else
XAverage = Factor * Price + (1 - Factor) * XAverage[1];
So on the first bar of data (CurrentBar = 1) it calculates "Factor" and sets XAverage to the "Price" input to initialize it.
Then on the next bar it calculates a new value for XAverage:
XAverage = Factor * Price + (1 - Factor) * XAverage[1];
using the value of XAverage from the previous bar ("XAverage[1]")
Then on the next bar it calculates a new value for XAverage:
XAverage = Factor * Price + (1 - Factor) * XAverage[1];
using the value of XAverage from the previous bar ("XAverage[1]")
etc., until it finished the calculation for the last bar on the chart.
Each value depends upon the previous value and only the previous value. But that value includes a component from all past values, obviously.
This is one of a class of digital filters called "Infinite Impulse Response" (IIR) filters where the current value depends upon all previous values back in time.
Do a Google search of "IIR Digital Filter" (14,000 hits as of today) to learn more that you ever wanted to know on these kinds of filters.
Bob Fulks
|