Author: 2009-2020, MetaQuotes Software Corp.
0 Views
0 Downloads
0 Favorites
TEMA_v1
ÿþ//+------------------------------------------------------------------+

//|                                                         TEMA.mq4 |

//|                   Copyright 2009-2017, MetaQuotes Software Corp. |

//|                                              http://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright   "2009-2020, MetaQuotes Software Corp."

#property link        "http://www.mql5.com"

#property description "Triple Exponential Moving Average"

#property strict



#include <MovingAveragesTS.mqh>



#property indicator_chart_window

#property indicator_buffers  1

#property indicator_color1   clrAquamarine

//----

enum  tema_price        {

                          tema_CLS  = 0,  // CLOSE

                          tema_OPN  = 1,  // OPEN

                          tema_HGH  = 2,  // HIGH

                          tema_LOW  = 3,  // LOW

                          tema_MED  = 4,  // MEDIAN

                          tema_TYP  = 5,  // TYPICAL

                          tema_WGT  = 6   // WEIGHTED

                        };

//---- input parameters

input int                  InpPeriodEMA = 14;            // EMA period

input tema_price           InpPrice     = tema_CLS;      // Indicator's price

input int                  InpShift     = 0;             // Indicator's shift

//---- indicator buffers

double  TemaBuffer[];

double  PriceBuffer[];

double  Ema[];

double  EmaOfEma[];

double  EmaOfEmaOfEma[];

//----

int     nDraw_begin1=0;

//+------------------------------------------------------------------+   

//| Custom indicator initialization function                         | 

//+------------------------------------------------------------------+

int OnInit()

{

   string sShort_name;

   //----

   IndicatorDigits(_Digits);

   //---- 4 additional buffers are used for counting

   IndicatorBuffers(5);

   //----

   SetIndexBuffer(1, PriceBuffer);

   SetIndexBuffer(2, Ema);

   SetIndexBuffer(3, EmaOfEma);

   SetIndexBuffer(4, EmaOfEmaOfEma);

   //---- indicator lines

   SetIndexBuffer(0,TemaBuffer);

   SetIndexStyle (0,DRAW_LINE);

   SetIndexLabel (0,"TEMA ("+(string)InpPeriodEMA+","+StringSubstr(EnumToString(InpPrice),5)+")");

   SetIndexShift (0,InpShift);

   //---- name for indicator

   sShort_name="TEMA ("+(string)InpPeriodEMA+","+StringSubstr(EnumToString(InpPrice),5)+")";

   IndicatorShortName(sShort_name);

   //----

   nDraw_begin1=3*InpPeriodEMA-3+InpShift;

   SetIndexDrawBegin(0,nDraw_begin1);

//----

   return(INIT_SUCCEEDED);

}

//+------------------------------------------------------------------+ 

//| Custom indicator iteration function                              | 

//+------------------------------------------------------------------+ 

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

{

//----

   if(rates_total<=nDraw_begin1) return(0);

   //----

   int limit=rates_total-prev_calculated;

   //----

   if(limit>1)

     {

      limit=rates_total-nDraw_begin1-1;

      //----

      ArrayInitialize(TemaBuffer,    0.0);

      ArrayInitialize(PriceBuffer,   0.0);

      ArrayInitialize(Ema,           0.0);

      ArrayInitialize(EmaOfEma,      0.0);

      ArrayInitialize(EmaOfEmaOfEma, 0.0);

     }

   //---- price box

   for(int i=limit; i>=0; i--) 

     {

           if(InpPrice==0)  PriceBuffer[i] = close[i];

      else if(InpPrice==1)  PriceBuffer[i] = open[i];

      else if(InpPrice==2)  PriceBuffer[i] = high[i];

      else if(InpPrice==3)  PriceBuffer[i] = low[i];

      else if(InpPrice==4)  PriceBuffer[i] = (high[i]+low[i])/2;

      else if(InpPrice==5)  PriceBuffer[i] = (high[i]+low[i]+close[i])/3;

      else                  PriceBuffer[i] = (high[i]+low[i]+close[i]+close[i])/4; // 6

     }

   //---- calculate EMA

   EmaOnBuffer(rates_total,prev_calculated,0,InpPeriodEMA,PriceBuffer,Ema);

   //---- calculate EMA on EMA array

   EmaOnBuffer(rates_total,prev_calculated,InpPeriodEMA-1,InpPeriodEMA,Ema,EmaOfEma);

   //---- calculate EMA on EMA array on EMA array

   EmaOnBuffer(rates_total,prev_calculated,2*InpPeriodEMA-2,InpPeriodEMA,EmaOfEma,EmaOfEmaOfEma);

   //---- calculate TEMA

   for(int i=limit; i>=0; i--) 

      TemaBuffer[i]=3*Ema[i]-3*EmaOfEma[i]+EmaOfEmaOfEma[i];

//----

   return(rates_total);

}

//+------------------------------------------------------------------+

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 ---