DEMA_v1
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
DEMA_v1
//+------------------------------------------------------------------+
//|                                                         DEMA.mq4 |
//| DEMA = 2 * EMA - EMA of EMA													|
//+------------------------------------------------------------------+
#property link "http://www.forexfactory.com/showthread.php?t=29419"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1  Red
#property indicator_width1  1

//---- input parameters
extern int PERIOD	= 12;

//---- indicator buffer
double Buffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
	IndicatorShortName("DEMA("+PERIOD+")");
	SetIndexBuffer(0,Buffer);
	SetIndexStyle(0,DRAW_LINE);
}

//+------------------------------------------------------------------+
int start()
{
	int limit = Bars-1-IndicatorCounted();

	static double lastEMA, lastEMA_of_EMA;
	double weight = 2.0 / (1.0+PERIOD);

	if(IndicatorCounted()==0)
	{
		Buffer[limit]	= Close[limit];
		lastEMA			= Close[limit];
		lastEMA_of_EMA	= Close[limit];
		limit--;
	}

//----
//	Calculate old bars (not the latest), if necessary
	for(int i = limit; i > 0; i--)
	{
		lastEMA			= weight*Close[i]	+ (1.0-weight)*lastEMA;
		lastEMA_of_EMA	= weight*lastEMA	+ (1.0-weight)*lastEMA_of_EMA;

		Buffer[i] = 2.0*lastEMA - lastEMA_of_EMA;
	}

//----
//	(Re)calculate current bar
	double EMA			= weight*Close[0]	+ (1.0-weight)*lastEMA,
			 EMA_of_EMA	= weight*EMA		+ (1.0-weight)*lastEMA_of_EMA;

	Buffer[0] = 2.0*EMA - EMA_of_EMA;

//----
	return(0);
}

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---