Self Referencing Scripts
Overview
The SELF() function allows a script to reference itself to get its previous value for use in the current calculation. Alternativley you can reference the function with [] as you would with an offset value.
E.g., a simple script that increments 1 on each bar process
A1 = 1 + Self();
A1
Or
A1 = 1 + A1[1];
A1
In this script example you can see A1 is equal to a standard moving average MA() plus 0.2, then multiplies by the previous scripts value.
A1 = MA() + 0.2 * Self();
A1;
or this script would output the same result…
A1 = MA() + 0.2 * A1[1];
A1;
In this final example this script returns the accumulated turnover divided by the accumulated volume…
t1 = TO();
v1 = VOLUME();
t2 = t1 + t2[1];
v2 = v1 + v2[1];
t2/v2