TRIX indicator

 

I just recently started playing around with MT5 (I live in the U.S. so my options are greatly limited as to brokers who use MT5).

I found an indicator in there called TRIX.  And I really like it.  So I took upon myself to try to convert the MT5 indicator to MT4.  I've made modifications to make it compile in MQL4.  But the indicator comes up in the terminal like it's "shifted".  I'm not sure why.  Any ideas?

TRIX pic


//+------------------------------------------------------------------+
//|                                                         TRIX.mq5 |
//|                   Copyright 2009-2017, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright   "2009-2017, MetaQuotes Software Corp."
#property link        "http://www.mql5.com"
#property description "Triple Exponential Average"
#include <MovingAverages.mqh>
//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  Red
#property indicator_width1  1
#property indicator_label1  "TRIX"
//#property indicator_applied_price PRICE_CLOSE
//--- input parameters
input int InpPeriodEMA=14;               // EMA period
//--- indicator buffers
double    TRIX_Buffer[];
double    EMA[];
double    SecondEMA[];
double    ThirdEMA[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,TRIX_Buffer,INDICATOR_DATA);
   SetIndexBuffer(1,EMA,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,SecondEMA,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,ThirdEMA,INDICATOR_CALCULATIONS);
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,3*InpPeriodEMA-3);
//--- name for index label
   string short_name=StringFormat("TRIX(%d)",InpPeriodEMA);
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
   PlotIndexSetString(0,PLOT_LABEL,short_name);
//--- indicator digits
   IndicatorSetInteger(INDICATOR_DIGITS,8);
   SetIndexLabel(1,NULL);
   SetIndexLabel(2,NULL);
   SetIndexLabel(3,NULL);
   SetIndexStyle(1,DRAW_NONE);
   SetIndexStyle(2,DRAW_NONE);
   SetIndexStyle(3,DRAW_NONE);

  }
//+------------------------------------------------------------------+
//| Triple Exponential Average                                       |
//+------------------------------------------------------------------+
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<3*InpPeriodEMA-3)
      return(0);
//---
   int start;
   if(prev_calculated==0)
     {
      start=3*(InpPeriodEMA-1);
      for(int i=0; i<start; i++)
         TRIX_Buffer[i]=EMPTY_VALUE;
     }
   else
      start=prev_calculated-1;
//--- calculate EMA
   ExponentialMAOnBuffer(rates_total,prev_calculated,0,InpPeriodEMA,close,EMA);
//--- calculate EMA on EMA array
   ExponentialMAOnBuffer(rates_total,prev_calculated,InpPeriodEMA-1,InpPeriodEMA,EMA,SecondEMA);
//--- calculate EMA on EMA array on EMA array
   ExponentialMAOnBuffer(rates_total,prev_calculated,2*InpPeriodEMA-2,InpPeriodEMA,SecondEMA,ThirdEMA);
//--- calculate TRIX
   for(int j=start; j<rates_total && !IsStopped(); j++)
     {
      if(ThirdEMA[j-1]!=0.0)
         TRIX_Buffer[j]=(ThirdEMA[j]-ThirdEMA[j-1])/ThirdEMA[j-1];
      else
         TRIX_Buffer[j]=0.0;
     }
//--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Michael: I just recently started playing around with MT5 (I live in the U.S. so my options are greatly limited as to brokers who use MT5).

I found an indicator in there called TRIX.  And I really like it.  So I took upon myself to try to convert the MT5 indicator to MT4.  I've made modifications to make it compile in MQL4.  But the indicator comes up in the terminal like it's "shifted".  I'm not sure why.  Any ideas?

By the way, there are many TRIX implementations for MQL5 in the Codebase: https://www.mql5.com/en/search#!keyword=TRIX&module=mql5_module_codebase

On a personal note, code by @Mladen Rakic is usually good and work well, so look into TRIX implementations by him: https://www.mql5.com/en/search#!keyword=TRIX&module=mql5_module_codebase&author=mladen

 
Fernando Carreiro:
By the way, there are many TRIX implementations for MQL5 in the Codebase: https://www.mql5.com/en/search#!keyword=TRIX&module=mql5_module_codebase

On a personal note, code by @Mladen Rakic is usually good and work well, so look into TRIX implementations by him: https://www.mql5.com/en/search#!keyword=TRIX&module=mql5_module_codebase&author=mladen

Thanks but I'm not looking for an MQL5 implementation.  I'm trying get/make one for MQL4.

 
Michael: Thanks but I'm not looking for an MQL5 implementation.  I'm trying get/make one for MQL4.
My apologies for misunderstanding, but in that case my answer remains valid as there are several MQL4 implementations in the CodeBase as well!
 
Fernando Carreiro:
My apologies for misunderstanding, but in that case my answer remains valid as there are several MQL4 implementations in the CodeBase as well!

Well, went through the codebase; not what I'm looking for.

 
Trix
Trix
  • www.mql5.com
Indicator Trix is used in determining of the overbought/oversold market conditions. You can also use it as an impulse indicator.
Reason: