Author: Copyright 2015, Daisuke
0 Views
0 Downloads
0 Favorites
temabb
ÿþ//+------------------------------------------------------------------+

//| TEMA STD BAND                                                    |

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

#property copyright "Copyright 2015,  Daisuke"

#property link      "http://mt4program.blogspot.jp/"

#property version   "1.00"

#property strict

#property indicator_chart_window



//buffer properties

#property indicator_buffers 8



#property indicator_label1  "TEMA"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrIndianRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2



#property indicator_label2  "EMA"

#property indicator_type2   DRAW_NONE

#property indicator_color2  clrNONE



#property indicator_label3  "EMAEMA"

#property indicator_type3   DRAW_NONE

#property indicator_color3  clrNONE



#property indicator_label4  "EMAEMAEMA"

#property indicator_type4   DRAW_NONE

#property indicator_color4  clrNONE



#property indicator_label5  "HIGH1"

#property indicator_type5   DRAW_LINE

#property indicator_color5  clrOrange

#property indicator_style5  STYLE_SOLID

#property indicator_width5  1



#property indicator_label6  "HIGH2"

#property indicator_type6   DRAW_LINE

#property indicator_color6  clrOrange

#property indicator_style6  STYLE_SOLID

#property indicator_width6  1



#property indicator_label7  "LOW1"

#property indicator_type7   DRAW_LINE

#property indicator_color7  clrOrange

#property indicator_style7  STYLE_SOLID

#property indicator_width7  1



#property indicator_label8  "LOW2"

#property indicator_type8   DRAW_LINE

#property indicator_color8  clrOrange

#property indicator_style8  STYLE_SOLID

#property indicator_width8  1



extern int Shift = 1;      //Shift

extern double Alfa = 0.1;  //EMA Alfa

extern ENUM_APPLIED_PRICE MaPrice=PRICE_CLOSE; // PRICE

extern int StdPeriod=13; // STD Period

extern double Sigma1 = 1;  // Band1 alfa

extern double Sigma2 = 2;  // Band2 alfa



double tema[];

double ema1[];

double ema2[];

double ema3[];

double high1[];

double high2[];

double low1[];

double low2[];

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

//| Initialize                                                       |

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

int OnInit()

  {

   if( Alfa <= 0 || Alfa >= 1 ) return INIT_PARAMETERS_INCORRECT;



// create buffer

   SetIndexBuffer(0,tema);

   SetIndexBuffer(1,ema1);

   SetIndexBuffer(2,ema2);

   SetIndexBuffer(3,ema3);

   SetIndexBuffer(4,high1);

   SetIndexBuffer(5,high2);

   SetIndexBuffer(6,low1);

   SetIndexBuffer(7,low2);

   SetIndexDrawBegin(0,StdPeriod);

   SetIndexDrawBegin(1,StdPeriod);

   SetIndexDrawBegin(2,StdPeriod);

   SetIndexDrawBegin(3,StdPeriod);

   SetIndexDrawBegin(4,StdPeriod);

   SetIndexDrawBegin(5,StdPeriod);

   SetIndexDrawBegin(6,StdPeriod);

   SetIndexDrawBegin(7,StdPeriod);



   string short_name="TEMA BB("+DoubleToStr(Alfa)+")";

   IndicatorShortName(short_name);



   return INIT_SUCCEEDED;

  }

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

//| Calculate Event                                                  |

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

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[])

  {

   for(int i=(rates_total-prev_calculated-1); i>=0 && !IsStopped(); i--)

     {

      if(i>=(rates_total-1-Shift))

        {

         double price=GetPrice(open[i],close[i],high[i],low[i],MaPrice);

         ema1[i] = price;

         ema2[i] = price;

         ema3[i] = price;

         tema[i] = price;

         continue;

        }



      double price=GetPrice(open[i+Shift],close[i+Shift],high[i+Shift],low[i+Shift],MaPrice);

      ema1[i] = Alfa * price + ( 1 - Alfa ) * ema1[i + 1];

      ema2[i] = Alfa * ema1[i] + ( 1 - Alfa ) * ema2[i + 1];

      ema3[i] = Alfa * ema2[i] + ( 1 - Alfa ) * ema3[i + 1];

      tema[i] = ema1[i] * 3 - ema2[i] * 3 + ema3[i];



      if(i>=(rates_total-1-StdPeriod-Shift)) continue;



      // Calculate "Standard Deviation". Average is shifted TEMA Center. 

      double mu = 0;

      for(int j = 0; j < StdPeriod; j++ )

        {

         mu+=MathPow(close[i+j/* + Shift*/]-tema[i+j],2);

        }



      double std=MathSqrt(mu/(StdPeriod-1));



      high1[i] = tema[i] + std * Sigma1;

      high2[i] = tema[i] + std * Sigma2;



      low1[i] = tema[i] - std * Sigma1;

      low2[i] = tema[i] - std * Sigma2;

     }

   return(rates_total - 1);

  }

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

//| Calculate Price                                                  |

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

double GetPrice(double open,

                double close,

                double high,

                double low,

                ENUM_APPLIED_PRICE maPrice)

  {

   double price=0;



   switch(maPrice)

     {

      case PRICE_CLOSE:

         price=close;

         break;

      case PRICE_OPEN:

         price=open;

         break;

      case PRICE_HIGH:

         price=high;

         break;

      case PRICE_LOW:

         price=low;

         break;

      case PRICE_MEDIAN:

         price=(high+low)/2;

         break;

      case PRICE_TYPICAL:

         price=(high+low+close)/3;

         break;

      case PRICE_WEIGHTED:

         price=(high+low+close+close)/4;

         break;

     }

   return price;

  }

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

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