# Simple Moving Average

Hi all, I'm an S&P daytrader who has done a fair bit of programming with ESPL, especially the wonderful Ensign Windows ESPL which is both fast and powerful.  In fact I'm beginning to do things in our new ESPL which I couldn't do or fit in TradeStation. (Kudos to Howard!!!).  Anyway, thought I'd contribute something to our new Ensign list.

Traders who have worked with other trading platforms may find the prospect of lower level ESPL intimidating when compared the use of simple higher level functions in other platforms.  Now that Ensign Windows has a compiler, that should change.  As I've been building trading tools with the new compiled ESPL in Ensign Windows, I've been constructing and testing general purpose ESPL functions to perform common tasks.  Here's one which I've fully tested and am using:  a general purpose 32 bit function which calculates a simple average on the specified value for specified length.

To calculate a 20 period Simple Moving Average of TrueRange as of the rightmost bar on the chart, one would:

a) include this function in a group of general purpose functions at the top of the script
b) write in Var section   rAverage: Real; {I indicate typing with first char of all variable names}
c)  in logic section   rAverage := ufGetAverageSimple(BarEnd, 20, eTrueRange);

Function ufGetAverageSimple(iLastBar:Integer; iLength:Integer; iValueConstant:Integer):Real;
{ Author : Earl Adamy }
{ History : 10/03/98 Create function }
{ Purpose : Calc Simple average of specified bar value for iBarBeg to iBarEnd}
{ iLastBar is last bar on which Average is to be calculated }
{ 0 specifies that BarEnd is to be used }
{ iLength is number of bars over which Avg is to be calculated }
{ iValueConstant is one of following: }
{ eOpen, eHigh, eLow, eClose, eLast (Close) values on bar }
{ eRange (High - Low) }
{ eTrueRange (> High or yesterday Low - < Low or yesterday High }
{ eMidpoint (H+L/2), eMid3 (H+L+C/3, eMid4 (O+H+L+C/4) }
{ eNet (Close - Yesterday Close) }

var
iBarX: Integer;
iBarBeg: Integer;
iBarEnd: Integer;
rSumX: real;
rValue: real;
Begin
If iLastBar = 0 Then iBarEnd := BarEnd Else iBarEnd := iLastBar;
iBarBeg := (iLastBar - iLength) + 1;
If (iBarBeg > 0) and (iBarEnd <= BarEnd) Then
Begin
{Clear the working variables}
rSumX :=0;
{Iterate the bars accumulating values until complete}
for iBarX := iBarBeg to iBarEnd do
Begin
rValue := Bar(iValueConstant, iBarX);
rSumX := rSumX + rValue;
End;
Result := (rSumX / iLength);
End
Else Result := 0;
End; {ufGetAverageSimple}