iCustom parameters && TEMA problems

 

Hi,

I am trying to make a Bollinger Bands indicator that uses TEMA. I've got the TEMA source code from a webpage, but (1) I cannot really use this indicator with iCustom and (2) also there seem to be some problems with this indicator.

(1)

I use the following parameters with iCustom:

Symbol: NULL

Timeframe:0

Name: "TEMA"

Mode: 3 (although I'm not absolutely sure that this is what I need to use here)

I am not sure if after I should pass the value of the BandsPeriod variable to TEMA after the Name parameter. Also I am unable to interpret the description of the last parameter

"Shift - Index of the value taken from the indicator buffer (shift relative to the current bar the given amount of periods ago)"

(2)

The source code of TEMA says that MetaQuotes is the Copyright holder but still it produces some weird results. Everything is ok until you start to scroll back and after a while you see this:

Could someone help me?

Source code for reference:


//+------------------------------------------------------------------+
//|                                                         TEMA.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.ru/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.ru/"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 DarkBlue
#property  indicator_width1  2
//---- input parameters
extern int       EMA_period=14;
//---- buffers
double TemaBuffer[];
double Ema[];
double EmaOfEma[];
double EmaOfEmaOfEma[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(4);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,TemaBuffer);
   SetIndexBuffer(1,Ema);
   SetIndexBuffer(2,EmaOfEma);
   SetIndexBuffer(3,EmaOfEmaOfEma);

   IndicatorShortName("TEMA("+EMA_period+")");
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i,limit,limit2,limit3,counted_bars=IndicatorCounted();
//----
   if (counted_bars==0)
      {
      limit=Bars-1;
      limit2=limit-EMA_period;
      limit3=limit2-EMA_period;
      }
   if (counted_bars>0)
      {
      limit=Bars-counted_bars-1;
      limit2=limit;
      limit3=limit2;
      }
   for (i=limit;i>=0;i--) Ema[i]=iMA(NULL,0,EMA_period,0,MODE_EMA,PRICE_CLOSE,i);
   for (i=limit2;i>=0;i--) EmaOfEma[i]=iMAOnArray(Ema,0,EMA_period,0,MODE_EMA,i);
   for (i=limit3;i>=0;i--) EmaOfEmaOfEma[i]=iMAOnArray(EmaOfEma,0,EMA_period,0,MODE_EMA,i);
   for (i=limit3;i>=0;i--) TemaBuffer[i]=3*Ema[i]-3*EmaOfEma[i]+EmaOfEmaOfEma[i];
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
  1. Indicator code:
    #property indicator_buffers 1
    extern int       EMA_period=14;
    SetIndexBuffer(0,TemaBuffer);
    So your code:
    #define TEMA "TEMA"         // Exact indicator name without extension
    #define TEMABUFFER 0        // Which buffer(s) it defines 
                                // EMA=1, EMAOFEMA=2 but these are not visible.
    int Tema_EMA_period=14;     // Indicator parameter(s)
    :
    double temaCurrent = iCustom(NULL,0, TIMA,
                                 Tema_EMA_period,   // Indicator parameter(s)
                                                    // in the order defined.
                                 TEMABUFFER,        // Which buffer you want
                                 0);                // Shift 0=current bar 
                                                    // 1=last bar...

  2. As for creating Bollinger Bands from it, you'll have to calculate the RMS from your tema buffer in your indicator. You will not be able to use iStdDev() or iStdDevOnArray()

  3. Everything is ok until you start to scroll back and after a while you see this:
    This is because it doesn't properly calculate and use proper limits. You can't get a moving average(x) when you have less than x bars. It doesn't use SetIndexDrawBegin
    int DrawBegin_EMA,
        DrawBegin_EMAofEMA,
        DrawBegin_EMAofEMAofEMA,
        DrawBegin_Tema,
    int init(){
        :
        #define DRAWBEGIN_PRICE 0
        DrawBegin_EMA           = DRAWBEGIN_PRICE    + EMA_period;
        DrawBegin_EMAofEMA      = DrawBegin_EMA      + EMA_period;
        DrawBegin_EMAofEMAofEMA = DrawBegin_EMAofEMA + EMA_period;
        DrawBegin_Tema          = DrawBegin_EMAofEMAofEMA; // Max of above.
        SetIndexDrawBegin(TEMABUFFER, DrawBegin_Tema);
    }
    int start(){
       int counted_bars=IndicatorCounted();
       if (counted_bars < DrawBegin_EMA)            counted_bars = DrawBegin_EMA;
       int limit1 = Bars - 1 - counted_bars;
       if (counted_bars < DrawBegin_EMAofEMA)       counted_bars = DrawBegin_EMAofEMA;
       int limit2 = Bars - 1 - counted_bars;
       if (counted_bars < DrawBegin_EMAofEMAofEMA)  counted_bars = DrawBegin_EMAofEMAofEMA;
       int limit3 = Bars - 1 - counted_bars;
       int limit4 = limit3; // Min of above.
       for (i=limit1;i>=0;i--) Ema[i]=iMA(NULL,0,EMA_period,0,MODE_EMA,PRICE_CLOSE,i);
       for (i=limit2;i>=0;i--) EmaOfEma[i]=iMAOnArray(Ema,0,EMA_period,0,MODE_EMA,i);
       for (i=limit3;i>=0;i--) EmaOfEmaOfEma[i]=iMAOnArray(EmaOfEma,0,EMA_period,0,MODE_EMA,i);
       for (i=limit4;i>=0;i--) TemaBuffer[i]=3*Ema[i]-3*EmaOfEma[i]+EmaOfEmaOfEma[i];
    }
    //----
 

Thanks for your answer. I still cannot make it working but it just indicates that I should go back and continue reading the MQL book :) For now, this is just an experiment as I fell in love with the TEMA and would like to use it in other indicators. What I did here was copying the BBands code into a new file, commenting out

MovingBuffer[i]=iMA(NULL,0,BandsPeriod,BandsShift,MODE_SMA,PRICE_CLOSE,i);

and inserting

MovingBuffer[i]=iCustom(NULL,0,"TEMA",BandsPeriod,0,0); //using the default BandsPeriod external variable

instead. All I get however is a flat line that is supposed to be the EMA (the surrounding lines work ok though as I did not touch that part of the code). After your reply I included

#define TEMA "TEMA"
#define TEMABUFFER 0

before the declaration of variables and changed the iCustom line to

MovingBuffer[i]=iCustom(NULL,0,"TEMA",BandsPeriod,TEMABUFFER,0);

this might be a mess as I don't know what exactly I've done here, as I'm saying I'm still in the stage of reading the book and this is an early experiment of mine. However, the results are exactly the same. The line that is supposed to be the TEMA is flat, the rest of the code seems to be working.

 
Castor888:
exactly the same. The line that is supposed to be the TEMA is flat, the rest of the code seems to be working.
Did you compile TEMA?
 
WHRoeder:
Did you compile TEMA?

Yes, and it works on its own.
 
Print out GetLastError() and find out WHY
 
WHRoeder:
Print out GetLastError() and find out WHY

Tried it, it returns value 0, doesn't find any errors...
Reason: