# Trix function issue

128

Hello! I'm hoping someone out there can help me... I have taken the Trix indicator made by Raff here: https://www.forex-tsd.com/forum/debates-discussions/3142-trend-deviation-trd#comment_73338

and have tried to convert it to a function for EAs. However, the output result is NEVER the same as the indicator. I have looked over and messed with this code for a week now. The function is below:

double Trix(int period, int signal, int tf, int mode, int shift) {
if (period == 0) { return 0; }
if (signal == 0) { return 0; }
if (tf == 0) { return 0; }

double dataArray[];
//ArraySetAsSeries(dataArray, true);
ArrayResize(dataArray, period);     // Load extra data

// Load data to the array
for (int i = 0; i < period; i++) { dataArray[i] = Close[i]; }

double EMA1[];
double EMA2[];
double EMA3[];

int i = 0;

//ArraySetAsSeries(EMA1, true);
//ArraySetAsSeries(EMA2, true);
//ArraySetAsSeries(EMA3, true);
ArrayResize(EMA1, period);
ArrayResize(EMA2, period);
ArrayResize(EMA3, period);

for (i = 0; i < period; i++) { EMA1[i] = iMAOnArray(dataArray, 0, period, 0, MODE_EMA, i); }
//for(i = 0; i < period; i++) EMA1[i] = iMA(Symbol(), tf, period, 0, MODE_EMA, PRICE_CLOSE, i);
for (i = 0; i < period; i++) { EMA2[i] = iMAOnArray(EMA1, 0, period, 0, MODE_EMA, i); }
for (i = 0; i < period; i++) { EMA3[i] = iMAOnArray(EMA2, 0, period, 0, MODE_EMA, i); }

i = period - 2;

while (i >= 0) {
EMA1[i] = ((EMA3[i] - EMA3[i + 1]) / EMA3[i + 1]) * 10000; // The *10000 is to see the number better
i--;
}

//ArraySetAsSeries(EMA2, true);

for (i = 0; i < period; i++) { EMA2[i] = iMAOnArray(EMA1, 0, signal, 0, MODE_EMA, i); }

//ArraySetAsSeries(EMA2, false);

if (mode == 1) { return EMA1[shift]; }
if (mode == 2) { return EMA2[shift]; }
if (mode == 3) { return EMA1[shift] - EMA2[shift]; }

return 0;
}

5040

EMA1[] and EMA2[] arrays are too small, because the first two indices of EMA3[] are used.
128

pipPod:
EMA1[] and EMA2[] arrays are too small, because the first two indices of EMA3[] are used.

After altering Raffs indicator:

for(i=0; i<Bars; i++) ind_buffer1[i]=iMA(NULL,0,TRIX_Period,0,MODE_EMA,PRICE_CLOSE,i);
for(i=0; i<Bars; i++) ind_buffer2[i]=iMAOnArray(ind_buffer1,Bars,TRIX_Period,0,MODE_EMA,i);
for(i=0; i<Bars; i++) ind_buffer5[i]=iMAOnArray(ind_buffer2,Bars,TRIX_Period,0,MODE_EMA,i);
i=Bars-2;
while(i>=0)
{
ind_buffer1[i]=((ind_buffer5[i]-ind_buffer5[i+1])/ind_buffer5[i+1])*10000;
i--;
}
for(i=0; i<Bars; i++) ind_buffer2[i]=iMAOnArray(ind_buffer1,Bars,Signal_Period,0,MODE_EMA,i);

Comment("B1 ", ArraySize(ind_buffer1), "\nB2 ", ArraySize(ind_buffer2), "\nB5 ", ArraySize(ind_buffer5)); // On operation, this shows that the array sizes are the same!

I then altered this:

ArrayResize(EMA1, period);
ArrayResize(EMA2, period);

ArrayResize(EMA3, period);

to

ArrayResize(EMA1, period * or + a number );
ArrayResize(EMA2, period * or + a number);
ArrayResize(EMA3, period);

and got very strange results. Perhaps I misunderstood something?

5040

Depending on how many bars you want to look back, it should be increased by 1 or 2.

Code isn't tested.

```//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
//---
double Trix[2][2];
if(TriX(_Symbol,0,Period,Signal,Trix)!=4)
return;
if(Trix[0][0]>Trix[0][1] && Trix[1][0]<Trix[1][1])
//---
return;
}
//+------------------------------------------------------------------+
//|  TriX                                                            |
//+------------------------------------------------------------------+
char TriX(const string symbol,
ENUM_TIMEFRAMES timeframe,
const int period,
const int signal,
double &trix[2][2])
{
if(period<2 || signal<2)
return(0);
char count=0;
//---
static double EMA1[],EMA2[],EMA3[],Trix[];
if(ArrayResize(EMA1,period+2)!=period+2 ||
ArrayResize(EMA2,period+2)!=period+2 ||
ArrayResize(EMA3,signal+2)!=signal+2 ||
ArrayResize(Trix,signal+1)!=signal+1)
return(0);
//---
for(int i=0;i<period+2;i++)
EMA1[i]=iMA(symbol,timeframe,period,0,MODE_EMA,PRICE_CLOSE,i);
for(int i=0;i<period+2;i++)
EMA2[i]=iMAOnArray(EMA1,0,period,0,MODE_EMA,i);
for(int i=0;i<signal+2;i++)
EMA3[i]=iMAOnArray(EMA2,0,period,0,MODE_EMA,i);
for(int i=0;i<signal+1;i++)
Trix[i]=(EMA3[i+1]!=0.0)?(EMA3[i]-EMA3[i+1])/EMA3[i+1]:0.0;
//---
trix[0][0]=Trix[0];   count++;
trix[1][0]=Trix[1];   count++;
trix[0][1]=iMAOnArray(Trix,0,signal,0,MODE_SMA,0);   count++;
trix[1][1]=iMAOnArray(Trix,0,signal,0,MODE_SMA,1);   count++;
//---
return(count);
}
//+------------------------------------------------------------------+```