Help Needed - unable to retrieve additional value from custom indicator

 

Hi, I am trying to modify this custom indicator to provide an additional result in the index buffer (trend direction).

I have modified the code and the print function produces the correct value, however I cannot retrieve it from my EA.


Here is the code -

#property indicator_chart_window
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots 1
//--- plot ZigZag
#property indicator_label1  "ZigZag"
#property indicator_type1   DRAW_SECTION
#property indicator_color1  clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2


input int FrBarsLeft = 5; //LBar
input int FrBarsRight = 2; //RBar

double gzz[];
double frUp[];
double frDown[]; 
double trenddirection[];

int direction;
datetime it;
double l_level_up, l_level_down, l_zz_high, l_zz_low;
int rt;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,gzz,INDICATOR_DATA);
   ArraySetAsSeries(gzz,true);

//--- set short name and digits
   string short_name=StringFormat("Valable ZigZag(%d,%d)",FrBarsLeft,FrBarsRight);
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
   PlotIndexSetString(0,PLOT_LABEL,short_name);
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
   SetIndexBuffer(1,frUp,INDICATOR_CALCULATIONS);
   ArraySetAsSeries(frUp,true);
   SetIndexBuffer(2,frDown,INDICATOR_CALCULATIONS);
   ArraySetAsSeries(frDown,true);
   SetIndexBuffer(3,trenddirection,INDICATOR_CALCULATIONS);
//--- set an empty value
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
  }
//+------------------------------------------------------------------+
//| ZigZag calculation                                               |
//+------------------------------------------------------------------+
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[])
  {
   rt = rates_total;
   if(rates_total<FrBarsLeft+FrBarsRight+1) return(0);
      
   int start;
   
   if(prev_calculated<FrBarsLeft+FrBarsRight+1) {
      start=rates_total-FrBarsLeft-1;
      ArrayInitialize(gzz,0);
   } else start=rates_total-prev_calculated+FrBarsRight;
   
   for(int i=start;i>FrBarsRight;i--){
       trenddirection[i] = 0;
       if(Logic(i)==0) trenddirection[i] = 0;
       else if(Logic(i)==1) trenddirection[i] = 1;
       Print(trenddirection[i]);
   }
   return(rates_total);
  }
Documentation on MQL5: Common Functions / Print
Documentation on MQL5: Common Functions / Print
  • www.mql5.com
Print - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
jfox: I have modified the code and the print function produces the correct value, however I cannot retrieve it from my EA.

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked.
     How To Ask Questions The Smart Way. (2004)
          Be precise and informative about your problem

We can't see your broken EA code.

Always post all relevant code (using Code button) or attach the source file.

 

Apologies, I have been coding EAs for a bit now, but am now trying to expand into custom indicators

Here is the full indicator code, which I have edited from a custom indicator I found on this site -

#property indicator_chart_window
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots 1
//--- plot ZigZag
#property indicator_label1  "ZigZag"
#property indicator_type1   DRAW_SECTION
#property indicator_color1  clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2


input int FrBarsLeft = 5; //LBar
input int FrBarsRight = 2; //RBar

double gzz[];
double frUp[];
double frDown[];
double trenddirection[];

int direction;
datetime it;
double l_level_up, l_level_down, l_zz_high, l_zz_low;
int rt;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,gzz,INDICATOR_DATA);
   ArraySetAsSeries(gzz,true);

//--- set short name and digits
   string short_name=StringFormat("Valable ZigZag(%d,%d)",FrBarsLeft,FrBarsRight);
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
   PlotIndexSetString(0,PLOT_LABEL,short_name);
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
   SetIndexBuffer(2,frUp,INDICATOR_CALCULATIONS);
   ArraySetAsSeries(frUp,true);
   SetIndexBuffer(3,frDown,INDICATOR_CALCULATIONS);
   ArraySetAsSeries(frDown,true);
   SetIndexBuffer(1,trenddirection,INDICATOR_DATA);
   ArraySetAsSeries(trenddirection,true);
//--- set an empty value
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
  }
//+------------------------------------------------------------------+
//| ZigZag calculation                                               |
//+------------------------------------------------------------------+
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[])
  {
   rt = rates_total;
   if(rates_total<FrBarsLeft+FrBarsRight+1) return(0);
      
   int start;
   
   if(prev_calculated<FrBarsLeft+FrBarsRight+1) {
      start=rates_total-FrBarsLeft-1;
      ArrayInitialize(gzz,0);
   } else start=rates_total-prev_calculated+FrBarsRight;
   
   for(int i=start;i>FrBarsRight;i--){
      trenddirection[i] = Logic(i);
   Print(trenddirection[i]);
   }
   return(rates_total);
  }

bool IsFractal(int _i, bool _type) {
   if (_type) {
      double low = iLow(_Symbol,PERIOD_CURRENT,_i);
      for(int j=1;j<=FrBarsLeft;j++) if (iLow(_Symbol,PERIOD_CURRENT,_i+j) < low) return false;
      for(int j=1;j<=FrBarsRight;j++) if (iLow(_Symbol,PERIOD_CURRENT,_i-j) < low) return false;
      return true;
   } else {
      double high = iHigh(_Symbol,PERIOD_CURRENT,_i);
      for(int j=1;j<=FrBarsLeft;j++) if (iHigh(_Symbol,PERIOD_CURRENT,_i+j) > high) return false;
      for(int j=1;j<=FrBarsRight;j++) if (iHigh(_Symbol,PERIOD_CURRENT,_i-j) > high) return false;
      return true;
   }
   return false;
}

int Logic(int i) { 
   if (IsFractal(i,false)) {
      frUp[i] = iHigh(_Symbol,PERIOD_CURRENT,i);
   }
   if (IsFractal(i,true)) {
      frDown[i] = iLow(_Symbol,PERIOD_CURRENT,i);
   }  
   
   if (direction == 0) {
      if (l_level_down > 0 && iClose(_Symbol,PERIOD_CURRENT,i) < l_level_down) {
         gzz[i] = iLow(_Symbol,PERIOD_CURRENT,i);
         l_zz_low = gzz[i];
         direction = 1;
      }
      if (frUp[i] == iHigh(_Symbol,PERIOD_CURRENT,i) && l_zz_high < frUp[i]) {
         gzz[i] = iHigh(_Symbol,PERIOD_CURRENT,i);
         l_zz_high = gzz[i];
         ClearTheExtraValue(i,direction);
      }
   } else {
      if (l_level_up > 0 && iClose(_Symbol,PERIOD_CURRENT,i) > l_level_up) {
         gzz[i] = iHigh(_Symbol,PERIOD_CURRENT,i);
         l_zz_high = gzz[i];
         direction = 0;
      }
      if (frDown[i] == iLow(_Symbol,PERIOD_CURRENT,i) && l_zz_low > frDown[i]) {
         gzz[i] = iLow(_Symbol,PERIOD_CURRENT,i);
         l_zz_low = gzz[i];
         ClearTheExtraValue(i,direction);
      }
   }
   
   if (frUp[i] == iHigh(_Symbol,PERIOD_CURRENT,i)) {
      l_level_up = frUp[i];  
   }
   if (frDown[i] == iLow(_Symbol,PERIOD_CURRENT,i)) {
      l_level_down = frDown[i];
   }
   return(direction);
}

void ClearTheExtraValue(int _start, int _type) {
   for (int i=_start+1;i<rt;i++) {
      if (_type == 0) {
         if (iLow(_Symbol,PERIOD_CURRENT,i) == gzz[i]) return;
         gzz[i] = 0;
      } else {
         if (iHigh(_Symbol,PERIOD_CURRENT,i) == gzz[i]) return;
         gzz[i] = 0;
      }
   }
}
 

I have then created the following EA to test that I can pull the trenddirection through.

However, my EA does not print the same result as the indicator, therefore my assumption is that the indicator is not passing the correct value to the buffer trenddirection[].

As I have said, I am trying to expand my knowledge with custom indicators but am stalling at this stage. 

Here is my EA, which I have written simply to test that I can pull the value through - 


input ENUM_TIMEFRAMES      TF1   =  PERIOD_M15;
input ENUM_TIMEFRAMES      TF2   =  PERIOD_H1;
input ENUM_TIMEFRAMES      TF3   =  PERIOD_H4;
int HandleZZTF1;

//Global variables
string indicatorMetrics = "";                //initiate string for indicatorMetrics Variable. This will reset variable each time Ontick functions runs
int TicksReceivedCount = 0;                  //Counts number of ticks from oninit function
int TicksProcessedCount = 0;                 //Counts number of ticks processed based off candle opens only
static datetime TimeLastTickProcessed;       //stores the last time a tick was processed based off candle opens only

input int FrBarsLeft = 5; //LBar
input int FrBarsRight = 2; //RBar

int OnInit()
  {
   HandleZZTF1 = iCustom(Symbol(), TF1, "Valable_ZigZag_Base", FrBarsLeft, FrBarsRight);
   return(INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason)
  {
  }
void OnTick()
  {
   TicksReceivedCount++;          

   bool IsNewCandle   =     false;
   if(TimeLastTickProcessed   !=  iTime(Symbol(), Period(), 0))
   {
   IsNewCandle        =     true;
   TimeLastTickProcessed      =   iTime(Symbol(), Period(), 0);
   }
   if(IsNewCandle == true)
   {
   Print("NEW CANDLE");
   GetTrendDirection();
   }
  }
//+------------------------------------------------------------------+

void GetTrendDirection(){
   double      Bufferdir[];                  
   double filldir = CopyBuffer(HandleZZTF1, 3, 0, 2, Bufferdir);
   Print(Bufferdir[0]);
}