TRIX in MetaTrader 5 — MQL5 Source Code

A custom triple-exponential TRIX oscillator for MT5

TRIX in MetaTrader 5
TRIX in MetaTrader 5

TRIX in MQL5 is a nice exercise in chaining smoothing buffers — an EMA of an EMA of an EMA, then a rate of change. See the explainer for the concept.

What you’ll need

  • MetaTrader 5 with MetaEditor
  • Save as AlgoGen.TRIX.mq5 under MQL5/Indicators/

The source

//+------------------------------------------------------------------+
//|                                                 AlgoGen.TRIX.mq5 |
//|                                          Copyright 2026, AlgoGen |
//|                                           https://www.algogen.io |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, AlgoGen"
#property link      "https://www.algogen.io"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_plots   2

#property indicator_label1  "TRIX"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDodgerBlue
#property indicator_width1  2
#property indicator_label2  "Signal"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrOrangeRed
#property indicator_level1  0.0

input int InpPeriod = 15;
input int InpSignal = 9;

double TrixBuffer[], SigBuffer[], E1[], E2[], E3[];

int OnInit()
  {
   SetIndexBuffer(0, TrixBuffer, INDICATOR_DATA);
   SetIndexBuffer(1, SigBuffer,  INDICATOR_DATA);
   SetIndexBuffer(2, E1, INDICATOR_CALCULATIONS);
   SetIndexBuffer(3, E2, INDICATOR_CALCULATIONS);
   SetIndexBuffer(4, E3, INDICATOR_CALCULATIONS);
   IndicatorSetString(INDICATOR_SHORTNAME,
      StringFormat("AlgoGen TRIX(%d,%d)", InpPeriod, InpSignal));
   return(INIT_SUCCEEDED);
  }

// One recursive EMA step: dst[i] from src[i] and dst[i-1].
void EmaStep(const double &src[], double &dst[], int i, int period)
  {
   double k = 2.0 / (period + 1.0);
   dst[i] = (i == 0) ? src[i] : src[i] * k + dst[i - 1] * (1.0 - k);
  }

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 < 2) return(0);
   int start = (prev_calculated > 1) ? prev_calculated - 1 : 0;

   for(int i = start; i < rates_total; i++)
     {
      EmaStep(close, E1, i, InpPeriod);
      EmaStep(E1,    E2, i, InpPeriod);
      EmaStep(E2,    E3, i, InpPeriod);
      TrixBuffer[i] = (i > 0 && E3[i - 1] != 0.0)
                      ? 100.0 * (E3[i] - E3[i - 1]) / E3[i - 1] : 0.0;
      double k = 2.0 / (InpSignal + 1.0);
      SigBuffer[i] = (i == 0) ? TrixBuffer[0]
                     : TrixBuffer[i] * k + SigBuffer[i - 1] * (1.0 - k);
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+

Install & compile

  1. Drop it in MQL5/Indicators/, press F7.
  2. Drag onto a chart — a separate window shows the TRIX and signal lines around zero, matching the output chart.

Gotchas

  • Chained calculation buffers. E1, E2, E3 are INDICATOR_CALCULATIONS buffers holding the successive smoothings — the clean way to build an EMA of an EMA of an EMA in MQL5.
  • Order of operations. Smooth three times, then take the rate of change of the third EMA. Doing the ROC earlier gives a different indicator.
  • MQL4. Same math; swap the OnCalculate signature and buffer setup. There’s no standard built-in TRIX, so the custom version is the way.

Same indicator elsewhere: Python, Pine Script, EasyLanguage, NinjaScript. Then test a TRIX rule in AlgoGen.


This post is educational, not financial advice. Indicators describe the past; they don’t predict the future. Backtest anything before you risk real money on it.

Sources and further reading

  1. Technical indicator functions (MetaQuotes)

Historical research from the Algogen archive. Not investment advice.

Blog

Have a strategy idea?

Describe it in plain English, preview where your rules fire, then decide whether the evidence is worth a backtest.

Keep me postedHow it works