ta.tsi in MQL5: True Strength Index with double EMAs

Short answer

MetaTrader 5 has no iTSI. Write four EMAs: EMA(EMA(change, long), short) divided by EMA(EMA(|change|, long), short). Pine's ta.tsi returns the ratio between -1 and 1; TradingView's indicator multiplies it by 100.

ta.tsi(source, short_length, long_length) is William Blau's True Strength Index: the one-bar change of the source, smoothed twice with exponential averages, divided by the absolute change smoothed the same way. Pine's built-in returns the ratio itself, between -1 and 1; TradingView's "True Strength Index" indicator multiplies it by 100 for display. MetaTrader 5 has no iTSI, so the MQL5 equivalent is four EMAs written by hand.

What ta.tsi computes

With pc = ta.change(source):

tsi = ta.ema(ta.ema(pc, long_length), short_length) / ta.ema(ta.ema(math.abs(pc), long_length), short_length)

The long smoothing is applied first, the short one second, even though the arguments are listed short first; swapping them gives a plausible-looking but different line. Each ta.ema uses alpha = 2 / (length + 1) and starts from the first non-na value of its input. pc is na on bar 0, so every EMA starts on bar 1, and the TSI has a value from bar 1 on (with the first value being ±1, since numerator and denominator start equal in magnitude). The early bars are dominated by that start and converge over roughly five times long_length bars. A window with no movement makes the denominator 0 and the result na. The common setting is 13/25, often plotted with a signal line ta.ema(tsi, 13).

The MQL5 equivalent

//@version=6
indicator("TSI ref")
t = ta.tsi(close, 13, 25)
plot(t, "TSI")

The indicator below keeps each of the four EMAs in its own calculation buffer, so the realtime bar is recomputed from the previous bar's values on every tick instead of compounding. EmaStep reproduces ta.ema's start: the first input becomes the average. The output is left on Pine's -1..1 scale; multiply by 100 if you are matching TradingView's TSI indicator rather than the built-in function.

//+------------------------------------------------------------------+
//| True Strength Index matching ta.tsi(source, short, long)          |
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 5
#property indicator_plots   1
#property indicator_label1  "TSI"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDodgerBlue
#property indicator_level1  0
#property indicator_minimum -1
#property indicator_maximum 1

input int InpShort = 13;   // Short length (second smoothing)
input int InpLong  = 25;   // Long length (first smoothing)

double TsiBuf[], Num1[], Den1[], Num2[], Den2[];

int OnInit()
{
   SetIndexBuffer(0, TsiBuf, INDICATOR_DATA);
   SetIndexBuffer(1, Num1, INDICATOR_CALCULATIONS);
   SetIndexBuffer(2, Den1, INDICATOR_CALCULATIONS);
   SetIndexBuffer(3, Num2, INDICATOR_CALCULATIONS);
   SetIndexBuffer(4, Den2, INDICATOR_CALCULATIONS);
   PlotIndexSetDouble(0, PLOT_EMPTY_VALUE, EMPTY_VALUE);
   return INIT_SUCCEEDED;
}

// ta.ema step: the first value seeds the average, then alpha = 2 / (len + 1)
double EmaStep(const double v, const double &state[], const int i, const int len)
{
   if(i <= 1 || state[i - 1] == EMPTY_VALUE) return v;
   const double a = 2.0 / (len + 1.0);
   return a * v + (1.0 - a) * state[i - 1];
}

int OnCalculate(const int rates_total, const int prev_calculated,
                const int begin, const double &price[])
{
   int start = prev_calculated > 1 ? prev_calculated - 1 : 0;
   for(int i = start; i < rates_total; i++)
   {
      if(i == 0)   // no change on the first bar: everything is na
      {
         Num1[0] = EMPTY_VALUE; Den1[0] = EMPTY_VALUE;
         Num2[0] = EMPTY_VALUE; Den2[0] = EMPTY_VALUE;
         TsiBuf[0] = EMPTY_VALUE;
         continue;
      }
      const double mom = price[i] - price[i - 1];
      Num1[i] = EmaStep(mom,          Num1, i, InpLong);    // long smoothing first
      Den1[i] = EmaStep(MathAbs(mom), Den1, i, InpLong);
      Num2[i] = EmaStep(Num1[i],      Num2, i, InpShort);   // then short
      Den2[i] = EmaStep(Den1[i],      Den2, i, InpShort);
      // ta.tsi returns -1..1; TradingView's TSI indicator multiplies by 100 for display
      TsiBuf[i] = Den2[i] == 0.0 ? EMPTY_VALUE : Num2[i] / Den2[i];
   }
   return rates_total;
}

Where the two differ

AspectPine ta.tsiMQL5
Built-inYesNo iTSI function
Scale-1..1Whatever you return; TradingView's indicator shows -100..100
Smoothing orderLong EMA first, then shortMust be written in that order
EMA startFirst value of the inputMust be written; iMA MODE_EMA cannot run on a buffer through a handle
Warm-upValues from bar 1; converges over ~5 x long lengthSame if you seed the same way
Flat marketnaGuard the zero denominator yourself

The two-stage EMA makes the TSI one of the slowest indicators to forget its starting point. When comparing with TradingView, both charts must hold several hundred bars before the line under study, or the seed will show up as a constant-looking offset that slowly fades.

What the Stratlate converter emits

The converter maps ta.tsi to its runtime helper pine_tsi(src, num1, den1, num2, den2, bar, short, long), which keeps the four EMAs in four per-call-site arrays, applies the long length first and starts each EMA from its first value, as above. One difference to know about: the current helper multiplies the ratio by 100, so the converted value is on TradingView's indicator scale (-100..100) rather than the built-in's -1..1. A script that plots ta.tsi directly therefore shows values 100 times larger in MetaTrader, and a script that compares it with a fixed level such as 0.25 or rescales it with * 100 needs that factor checked by hand. The test script above converts with no notes in the report, so this is not flagged automatically. Review the scale, compile the output and backtest it.

Pitfalls

  • Smoothing with the short length first. The line is noisier and leads TradingView's by a few bars; the swap is easy because the arguments are listed short first.
  • Seeding the EMAs with an SMA of the first length values (the ta.rma convention). The early part of the chart differs, and with two stacked EMAs the difference lasts for hundreds of bars.
  • Assuming the -100..100 scale for ta.tsi. Thresholds such as tsi > 25 copied from TradingView's indicator never trigger on the built-in's output, and tsi > 0.25 triggers constantly on a ×100 output.
  • Keeping the EMAs in static double variables. Each tick on the forming bar applies the update again and the TSI drifts within the bar.
  • Integer alphas: 2 / (len + 1) with an int length is 0 and every EMA stays at its first value.

FAQ

Does MetaTrader 5 have a True Strength Index function?

No. There is no iTSI among the built-in indicator functions. Compute the four EMAs yourself as on this page, or load a custom TSI indicator with iCustom and check its formula and scale first.

Why are my TSI values 100 times larger or smaller than TradingView's?

The built-in ta.tsi returns a ratio between -1 and 1, while TradingView's True Strength Index indicator multiplies it by 100. Check which of the two your Pine script uses and scale the MQL5 output the same way.

Which length is applied first in ta.tsi?

The long length. ta.tsi(source, 13, 25) smooths the one-bar change with a 25-period EMA first and then with a 13-period EMA, and does the same to the absolute change.

Convert the whole script

Paste a Pine Script indicator or strategy and get MQL5 with this and every other call mapped, checked against the original on the same bars, and every construct that could not be carried over reported.

Open the converter

Related