# EMAcalc.efs

ICE Data Services -

EMAcalc.efs

File Name: EMAcalc.efs

Description:
Demonstrates how to create an exponential moving average calculation in EFS.

Formula Parameters:

• nLength: Default is 10

Notes:
Default settings draw a 10 period exponential moving average of the Close.

EMAcalc.efs

EFS Code:

```/*****************************************************************
Provided By : eSignal. (c) Copyright 2003
*****************************************************************/

function preMain() {
setPriceStudy(true);
setStudyTitle("EMA calculation");
setCursorLabelName("EMA of Close", 0);
setDefaultBarFgColor(Color.lime, 0);
}

var vEMA = null;
var vEMA1 = null;

var dPercent = 0.0;
var bPrimed = false;

function EMA(nLength, nArray) {
var nBarState = getBarState();
var dSum = 0.0;
var dRef;

if(nBarState == BARSTATE_ALLBARS || bPrimed == false) {
dPercent = (2.0 / (nLength + 1.0));
bPrimed = false;
}

if (nBarState == BARSTATE_NEWBAR) {
vEMA1 = vEMA;
}

if(bPrimed == false) {
for(i = 0; i < nLength; i++) {
dSum += nArray[i];
}
bPrimed = true;
return (dSum / nLength);
} else {
return (((close() - vEMA1) * dPercent) + vEMA1);
}
}

function main(nLength) {
if(nLength == null || nLength <= 0) nLength = 10;

var nArray = getValue("Close", 0, -nLength);
if(nArray == null) return;

vEMA = EMA(nLength, nArray);

return vEMA;
}```