Download MetaTrader 5

How to attach a custom indicator in EA code

To add comments, please log in or register
Do you have Linux? Find out how to install MetaTrader on it!
Nelson Reis
1686
Nelson Reis 2015.08.07 11:45 

Hi, guys

I have a custom indicator and need put it in EA code. Can someone help me?


The indicator code

=====================================================================================

//------------------------------------------------------------------------------------------------------------------------------|
// OBVPower.mq4                                                                                                                 |
// The code should be used for illustrating whether current OBV is above, below or equal to an averaged OBV of Period_MA;       |
//------------------------------------------------------------------------------------------------------------------------------|
#property indicator_separate_window                                  // Drawing in a separate window

//---- NOTE TO SELF - learn how non drawing buffers are distinguished from drawing                                          
                                               
#property indicator_buffers  3                   // Number of DRAWING buffers is 3
                                                
                                                

#property indicator_maximum  1                                       // Max separate window scale
#property indicator_minimum  0                                       // Min separate window scale                            
                                                
                                                
                                                                                              
#property indicator_color1 Green                                     // Color of the 1st indicator and buffer 0
#property indicator_color2 Red                                       // Color of the 2nd indicator and buffer 1
#property indicator_color3 Yellow                                    // Color of the 3rd indicator and buffer 2

extern int    Period_MA      =34;                                    // Averaging Period for Moving Average
extern string MA_Method_Help ="0-SMA, 1-EMA, 2-SMMA, 3-LWMA";
extern int    MA_Method      =1;                                     // Moving Average Emuneration Method
extern double GapMin         =0.5;                                   // Minimum Allowable Gap between OBV and AveOBV

double UpBuffer[];                                                   // Declaring an indicator array
double DownBuffer[];                                                 // Declaring an indicator array
double ZeroBuffer[];                                                 // Declaring an indicator array
double OBV[];                                                        // OnBalanceVolume array
double AveOBV[];                                                     // Average of OnBalanceVolume array
double Gap[];                                                        // Difference between OBV and AveOBV array values                   

//------------------------------------------------------------------------------------------------------------------------------| 
   int init()                                    // Special function init()
  {
   IndicatorBuffers(6);                          // TOTAL amount of buffers including non drawing buffers
//----
   SetIndexBuffer(0,UpBuffer);                                       // Assigning an array to a buffer
   SetIndexBuffer(1,DownBuffer);                                     // Assigning an array to a buffer
   SetIndexBuffer(2,ZeroBuffer);                                     // Assigning an array to a buffer
   SetIndexBuffer(3,OBV);                                            // Assigning an array to a buffer
   SetIndexBuffer(4,AveOBV);                                         // Assigning an array to a buffer
   SetIndexBuffer(5,Gap);                                            // Assigning an array to a buffer
//------------------------------------------------------------------------------------------------------------------------------|
   SetIndexStyle (0,DRAW_HISTOGRAM,STYLE_SOLID,4);                   // line style
   SetIndexStyle (1,DRAW_HISTOGRAM,STYLE_SOLID,4);                   // line style
   SetIndexStyle (2,DRAW_HISTOGRAM,STYLE_SOLID,4);                   // line style
//------------------------------------------------------------------------------------------------------------------------------|
   SetIndexEmptyValue (0,0.0);                      
   SetIndexEmptyValue (1,0.0);                      
   SetIndexEmptyValue (2,0.0);
   SetIndexEmptyValue (3,0.0);                      
   SetIndexEmptyValue (4,0.0);                      
   SetIndexEmptyValue (5,0.0);
//------------------------------------------------------------------------------------------------------------------------------|  
   SetIndexLabel (0,"UpBuffer");            
   SetIndexLabel (1,"DownBuffer");
   SetIndexLabel (2,"ZeroBuffer");
   SetIndexLabel (3,"OBV");              
   SetIndexLabel (4,"AveOBV");              
   SetIndexLabel (5,"Gap");               
//----           
   return;                                       // Exit the special funct. init()
  }
//------------------------------------------------------------------------------------------------------------------------------|
int start()                                      // Special function start()
  {
//------------------------------------------------------------------------------------------------------------------------------|
   int  limit,i;
   int  Counted_bars = IndicatorCounted();                           
   if  (Counted_bars<0) return(-1);                                  // Check for possible errors
   if  (Counted_bars>0) Counted_bars--;                              // The last counted bar will be recounted
        limit=Bars-Counted_bars;                                     // Number of counted bars                          
        i=Bars-Counted_bars-1;                                       // Index of the first uncounted
   double Diff;
  
//------------------------------------------------------------------------------------------------------------------------------|  
//---- the main loop      
//------------------------------------------------------------------------------------------------------------------------------|  

//-------------calculate the array values for On Balance Volume                                        
   i=Bars-Counted_bars-1;           // Index of the first uncounted
   while(i>=0)                                                       // Loop for uncounted bars
     {
     OBV[i]=iOBV(NULL,0,PRICE_CLOSE,i);                              // Value of 0 buffer on i bar
     i--;                                                            // Calculating index of the next bar
     }

   i=Bars-Counted_bars-1;                                            // Index of the first uncounted
   while(i>=0)                                                       // Loop for uncounted bars
     {
     AveOBV[i]=iMAOnArray(OBV,0,Period_MA,0,MA_Method,i);            // Value of 0 buffer on i bar
     i--;                                                            // Calculating index of the next bar
     }

     
  i=Bars-Counted_bars-1;                                            // Index of the first uncounted
  while(i>=0)                                                       // Loop for uncounted bars
     {
     Gap[i]= (OBV[i]-AveOBV[i]);                                    // Value of 0 buffer on i bar
     i--;                                                           // Calculating index of the next bar
     }
  i=Bars-Counted_bars-1;                                            // Index of the first uncounted
  while(i>=0)
  {
     Diff= Gap[i];                                                  // Value of 0 buffer on i bar
    
      
  if      (Diff>GapMin)
                 {
                 UpBuffer[i]  =1;
                 }
  else if (Diff<-GapMin)
                 {
                 DownBuffer[i]=1;
                 }
  else           {
                 ZeroBuffer[i]=1;
                 }
     i--;                                                           // Calculating index of the next bar           
      }
    
//--------------------------------------------------------------------
   return;                          // Exit the special funct. start()
  }
//--------------------------------------------------------------------


=================================================================

Thanks

Nelson

Keith Watford
Moderator
9648
Keith Watford 2015.08.07 11:58  
whroeder1
14543
whroeder1 2015.08.07 13:00  
nelreis: I have a custom indicator and need put it in EA code. Can someone help me?
.Don't do that. Just get the value of the indicator. Detailed explanation of iCustom - MQL4 forum
To add comments, please log in or register