Incorporating value from a custom indicator into EA

 

I'm looking to incorporate a value generated in this STC indicator into my EA: https://www.mql5.com/en/code/20281

On inspecting the code I discovered there is a data type called "valc" that holds a value (0, 1 or 2) - these numbers indicate whether the trend is going up (1) or down (2) Normally these are used by the indicator to define the line color but I'd like to use them for my own purposes. So I'm looking to pull that number from the indicator and bring it into my EA to use.

I ran a test using iCustom but that just returns a constant value of 10?

double GetValue = iCustom(_Symbol,0,"Schaff trend cycle - standard.ex5");

void OnTick()
  {
   Comment(GetValue);
  }

Any suggestion for how to go about this?

Schaff Trend Cycle
Schaff Trend Cycle
  • www.mql5.com
The Schaff Trend Cycle (STC) indicator detects up and down trends long before the MACD. It does this by using the same Exponential Moving Averages (EMAs), but adds a cycle component to factor currency cycle trends. Since currency cycle trends move based on a certain amount of days, this is factored into the equation of the STC indicator to give more accuracy and reliability than the MACD.
 

You are getting the handle of the indicator.

Read the documentation of how to use iCustom.

 
Keith Watford:

You are getting the handle of the indicator.

Read the documentation of how to use iCustom.

The problem is the indicator only shows the trend cycle in the data window, thats not the value I need.

Would I need to alter the code of the indicator to return valc to the data window?

 
tnil25 :

I'm looking to incorporate a value generated in this STC indicator into my EA: https://www.mql5.com/en/code/20281

On inspecting the code I discovered there is a data type called "valc" that holds a value (0, 1 or 2) - these numbers indicate whether the trend is going up (1) or down (2) Normally these are used by the indicator to define the line color but I'd like to use them for my own purposes. So I'm looking to pull that number from the indicator and bring it into my EA to use.

I ran a test using iCustom but that just returns a constant value of 10?

Any suggestion for how to go about this?

Please read the reference documentation. In MQL5, an indicator handle MUST be created ONLY ONCE - and this is done in OnInit.

Example:

//+------------------------------------------------------------------+
//|                                            iCustom Get Value.mq5 |
//|                              Copyright © 2021, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2021, Vladimir Karputov"
#property version   "1.000"
//+------------------------------------------------------------------+
//| Enub enPrices                                                    |
//+------------------------------------------------------------------+
enum enPrices
  {
   pr_close,      // Close
   pr_open,       // Open
   pr_high,       // High
   pr_low,        // Low
   pr_median,     // Median
   pr_typical,    // Typical
   pr_weighted,   // Weighted
   pr_average,    // Average (high+low+open+close)/4
   pr_medianb,    // Average median body (open+close)/2
   pr_tbiased,    // Trend biased price
   pr_tbiased2,   // Trend biased (extreme) price
   pr_haclose,    // Heiken Ashi close
   pr_haopen,     // Heiken Ashi open
   pr_hahigh,     // Heiken Ashi high
   pr_halow,      // Heiken Ashi low
   pr_hamedian,   // Heiken Ashi median
   pr_hatypical,  // Heiken Ashi typical
   pr_haweighted, // Heiken Ashi weighted
   pr_haaverage,  // Heiken Ashi average
   pr_hamedianb,  // Heiken Ashi median body
   pr_hatbiased,  // Heiken Ashi trend biased price
   pr_hatbiased2  // Heiken Ashi trend biased (extreme) price
  };
//--- input parameters
input int       SchaffPeriod = 32;       // Schaff period
input int       FastEma      = 23;       // Fast EMA period
input int       SlowEma      = 50;       // Slow EMA period
input double    SmoothPeriod = 3;        // Smoothing period
input enPrices  Price        = pr_close; // Price
//---
int      handle_iCustom;                        // variable for storing the handle of the iCustom indicator
bool     m_init_error               = false;    // error on InInit
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- forced initialization of variables
   m_init_error               = false;    // error on InInit
//--- create handle of the indicator iCustom
   handle_iCustom=iCustom(Symbol(),Period(),"Schaff Trend Cycle",
                          SchaffPeriod,
                          FastEma,
                          SlowEma,
                          SmoothPeriod,
                          Price);
//--- if the handle is not created
   if(handle_iCustom==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iCustom indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      m_init_error=true;
      return(INIT_SUCCEEDED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(m_init_error)
      return;
//---
   double custom[];
   ArraySetAsSeries(custom,true);
   int start_pos=0,count=3;
   if(!iGetArray(handle_iCustom,0,start_pos,count,custom))
      return;
//---
   string text="";
   for(int i=0; i<count; i++)
     {
      text=text+
           " bar #"+IntegerToString(i)+": "+
           " value "+DoubleToString(custom[i],Digits()+1)+"\n";
     }
   Comment(text);
  }
//+------------------------------------------------------------------+
//| Get value of buffers                                             |
//+------------------------------------------------------------------+
bool iGetArray(const int handle,const int buffer,const int start_pos,
               const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);
      return(false);
     }
   ArrayFree(arr_buffer);
//--- reset error code
   ResetLastError();
//--- fill a part of the iBands array with values from the indicator buffer
   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
   if(copied!=count)
     {
      //--- if the copying fails, tell the error code
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
                  __FILE__,__FUNCTION__,count,copied,GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(false);
     }
   return(result);
  }
//+------------------------------------------------------------------+


Result:

Files:
 
Vladimir Karputov:

Please read the reference documentation. In MQL5, an indicator handle MUST be created ONLY ONCE - and this is done in OnInit.

Example:


Result:

Vladimir to the rescue once again. This did it. I was able to use the cycle instead of the value I was looking for, totally rethought my approach.

Thank you!

Reason: