+1 888 340 6572

# Averages > D_EMA 2 Lines

Article/Author: Bill Mars

File Includes: Indicator > D_EMA 2 Lines

Category: Indicator > Averages

Description:

The indicator plots two EMA1 and EMA2 lines (Exponential Moving Averages), based on the formulas:

EMA1 = (ALPHA1 * PRICE) + ((1 - ALPHA1) * EMA1);
EMA2 = ((ALPHA2) * PRICE) + ((1 - (ALPHA2)) * EMA2);

where is PRICE = (HIGH[1]+LOW[1])/2. ALPHA is the constant for the EMA filter. Where is the EMA relates to the EMS by ALPHA = 2/(LENGTH + 1). For example, some common values are:

 ALPHA LENGTH .50 3 DAYS .40 4 DAYS .25 7 DAYS .20 9 DAYS .182 10 DAYS .133 14 DAYS .118 16 DAYS .095 20 DAYS .069 28 DAYS

Usage:

The most popular method of interpreting a moving average is to compare the relationship between a moving average of the security's price with the security's price itself (or between two different moving averages). A buy signal is generated when the security's price rises above its moving average and a sell signal is generated when the security's price falls below its moving average.

Inputs:

LENGTH1 - The parameter tells how many bars to the EMA1 average
LENGTH2 - The parameter tells how many bars to the EMA2 average

EasyLanguage Code:
```INPUT: LENGTH1(10), LENGTH2(40); VARS: PRICE(0), EMA1(0), EMA2(0), ALPHA1(.25), ALPHA2(.133),
D_HIGH(0), D_LOW(0), FIRSTDAY(TRUE);

IF DATE <> DATE[1] AND DAYOFWEEK(DATE[1]) <> 0 THEN BEGIN
PRICE = (D_HIGH + D_LOW) /2;
IF FIRSTDAY = TRUE THEN BEGIN
IF LENGTH1 > 2 THEN ALPHA1 = 2 / (LENGTH1 + 1)
ELSE ALPHA1 = .67;
IF LENGTH2 > 2 THEN ALPHA2 = 2 / (LENGTH2 + 1)
ELSE ALPHA2 = .34;
PRICE = (H[1]+L[1])/2;
EMA1 = PRICE ;
EMA2 = PRICE;
FIRSTDAY = FALSE;
END;
EMA1  =  (ALPHA1 * PRICE) + ((1 - ALPHA1) *  EMA1);
EMA2  =  ((ALPHA2) * PRICE) + ((1 - (ALPHA2)) *  EMA2);
PLOT1[1](EMA1,"EMA1");
PLOT2[1](EMA2,"EMA2");
D_LOW = LOW;
D_HIGH = HIGH;
END;

IF HIGH > D_HIGH THEN D_HIGH = HIGH;
IF LOW  < D_LOW THEN D_LOW = LOW;

```