Help on using custom indicator for expert advisor

 

Hi everyone. I have been trying to use several values from the custom indicator, called "Trix", in my expert advisor. I was wondering if anyone can help me with what am I doing wrong and how I should get the values that I need.

Brief explanation: I have been trying to get the 'double' value for ind_buffer3[0]ind_buffer2[0] , ind_buffer4[0] and use them in my expert advisor (in order to get these values from the indicator, i used iCustom command). I have tried to check if I am receiving the same values, both from the indicator and expert advisor (using Alert() function), however the values for ind_buffer3[0] and ind_buffer4[0] appear to be different when Trix is telling me that there is a signal to Buy or Sell. Basically the values that I get from my expert advisor (using Alert() function) stay the same and never change, but the values that i get from the indicator do change (I understand that this happens during the "if" statements in the code).


The question: How can i get the values from the indicator in my expert advisor so that they also change during the Buy or Sell signal?

Code for TRIX indicator:
#property  indicator_separate_window
#property  indicator_buffers 6
//----
#property  indicator_color1 Crimson
#property  indicator_color2 Green
#property  indicator_color3 Red
#property  indicator_color4 Aqua
#property  indicator_color5 DarkGray
#property  indicator_color6 DarkGray
#property  indicator_width1 1
#property  indicator_width2 1
#property  indicator_width3 0
#property  indicator_width4 0
#property  indicator_width5 1
#property  indicator_width6 1
#property  indicator_style1 STYLE_SOLID
#property  indicator_style2 STYLE_DOT
#property  indicator_style3 STYLE_SOLID
#property  indicator_style4 STYLE_SOLID
#property  indicator_style5 STYLE_SOLID
#property  indicator_style6 STYLE_SOLID
#property  indicator_level1 0
//---- indicator parameters
extern int  TRIX_Period    =13;
extern int  Signal_Period  =8;
extern bool Signals        =true;
extern int  CountBars      =1500;
//---- indicator buffers
double      ind_buffer1[];
double      ind_buffer2[];
double      ind_buffer3[];
double      ind_buffer4[];
double      ind_buffer5[];
double      ind_buffer6[];
double      ind_buffer7[];
//---- variables
static bool TurnedUp=false;
static bool TurnedDown=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   IndicatorBuffers(7);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,233);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,234);
   SetIndexStyle(4,DRAW_LINE);
   SetIndexStyle(5,DRAW_HISTOGRAM);
//---- indicator buffers mapping
   SetIndexBuffer(0,ind_buffer1);
   SetIndexBuffer(1,ind_buffer2);
   SetIndexBuffer(2,ind_buffer3);
   SetIndexBuffer(3,ind_buffer4);
   SetIndexBuffer(4,ind_buffer5);
   SetIndexBuffer(5,ind_buffer6);
   SetIndexBuffer(6,ind_buffer7);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("TRIX index | Period: "+TRIX_Period+", Signal Period: "+Signal_Period+" | ");
   SetIndexLabel(0,"TRIX");
   SetIndexLabel(1,"Signal");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| TRIX                                                             |
//+------------------------------------------------------------------+
int start()
  {
   if (TRIX_Period==Signal_Period) return(0);
   int i, limit=CountBars;
   if (limit>Bars) limit=Bars-1;
//---- trix 
   for(i=0; i<limit; i++) ind_buffer1[i]=iMA(NULL,0,TRIX_Period,0,MODE_SMMA,PRICE_CLOSE,i);
   for(i=0; i<limit; i++) ind_buffer2[i]=iMAOnArray(ind_buffer1,0,TRIX_Period,0,MODE_SMMA,i);
   for(i=0; i<limit; i++) ind_buffer7[i]=iMAOnArray(ind_buffer2,0,TRIX_Period,0,MODE_EMA,i);
   //
   for(i=0; i<limit-1; i++) ind_buffer1[i]=(ind_buffer7[i]-ind_buffer7[i+1])/ind_buffer7[i+1];
   for(i=0; i<limit-1; i++) ind_buffer2[i]=iMAOnArray(ind_buffer1,0,Signal_Period,0,MODE_EMA,i);
   for(i=0; i<limit-1; i++) {ind_buffer5[i]=ind_buffer1[i]-ind_buffer2[i]; ind_buffer6[i]=ind_buffer5[i];}
//---- signals
   i=limit-1;
   while(i>=0)
     {
      if (Signals==true)
        {
         if (ind_buffer2[i]<ind_buffer1[i] && ind_buffer2[i+1]>=ind_buffer1[i+1]) ind_buffer3[i]=ind_buffer2[i]-0.0001;
         if (ind_buffer2[i]>ind_buffer1[i] && ind_buffer2[i+1]<=ind_buffer1[i+1]) ind_buffer4[i]=ind_buffer2[i]+0.0001;
         if (ind_buffer3[0]==ind_buffer2[0]-0.0001 && TurnedUp==false)
           {
            Alert("Trix Buy:  ",Symbol()," - ",Period(),"  at  ", Close[0],"  -  ", TimeToStr(CurTime(),TIME_SECONDS));
            TurnedDown=false;
            TurnedUp=true;
           }
         if (ind_buffer4[0]==ind_buffer2[0]+0.0001 && TurnedDown==false)
           {
            Alert("Trix SELL:  ",Symbol()," - ",Period(),"  at  ", Close[0],"  -  ", TimeToStr(CurTime(),TIME_SECONDS));
            TurnedUp=false;
            TurnedDown=true;
           }
        }
      i--;
     }
     Alert("buffer 3(up): "+ind_buffer3[0]+"     buffer 4(down): "+ind_buffer4[0]+"    buffer 2(mid): "+ind_buffer2[0]);
//---- done
   return(0);
  }

My expert advisor code to get the values from the indicator:

extern int TRIX_Period = 34;
extern int Signal_Period = 13;
extern bool Signals = true;
extern int CountBars = 5000;

static bool turnUp = false;
static bool turnDown = false;

int OnInit()
  {

   return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
  {
   
  }

void OnTick()
  {
    double up1  = iCustom(NULL,0,"Trix",TRIX_Period,Signal_Period,Signals,CountBars,2,1);

    double down1 = iCustom(NULL,0,"Trix",TRIX_Period,Signal_Period,Signals,CountBars,3,1);
    double midval = iCustom(NULL,0,"Trix",TRIX_Period,Signal_Period,Signals,CountBars,1,1);
  //  Alert("up "+up1+"             down "+down1+"             mid "+midval);
    if(up1==midval-0.0001 && turnUp==false){
         turnUp=true;
         turnDown=false;
  //       Alert("up val is true");
      }
    if(down1==midval+0.0001 && turnDown==false){
         turnDown=true;
         turnDown=false;
  //      Alert("down val is true");
      }
   
  }


I apologize if it's a noob question. I have been trying to look up several topics, but didn't find anything that helped me. Thank you.

 

If you want the current values, you have to indicate bar index = 0 :

double up1  = iCustom(NULL,0,"Trix",TRIX_Period,Signal_Period,Signals,CountBars,2,0);

Also, please note that:

SetIndexBuffer(1,ind_buffer2);   //  ind_buffer2's  index = 1.  Therefore ind_buffer2[0] = iCustom(NULL,0,"Trix",TRIX_Period,Signal_Period,Signals,CountBars,1,0);
SetIndexBuffer(2,ind_buffer3);   //  ind_buffer3's  index = 2.  Therefore ind_buffer3[0] = iCustom(NULL,0,"Trix",TRIX_Period,Signal_Period,Signals,CountBars,2,0);
SetIndexBuffer(3,ind_buffer4);   //  ind_buffer4's  index = 3.  Therefore ind_buffer4[0] = iCustom(NULL,0,"Trix",TRIX_Period,Signal_Period,Signals,CountBars,3,0);

//  In general:  iCustom(NULL,0,"Trix",TRIX_Period,Signal_Period,Signals,CountBars, buffer index, bar index);


Regards.

 
Jose Francisco Casado Fernandez:

If you want the current values, you have to indicate bar index = 0 :

Also, please note that:


Regards.

I see. When I have been looking in the tutorial videos it always showed "1" there. I will try that right now. If this will work, I will respond again to make sure my question was answered. Thank you!

 
Budzzza:

I see. When I have been looking in the tutorial videos it always showed "1" there. I will try that right now. If this will work, I will respond again to make sure my question was answered. Thank you!

You are welcome.  Regards.
 
Jose Francisco Casado Fernandez:
You are welcome.  Regards.

Everything worked. Thank you so much again.



  SOLVED

 
Budzzza:

Everything worked. Thank you so much again.



  SOLVED

I'm glad everything was fine. Regards.