Download MetaTrader 5

Detailed explanation of iCustom

To add comments, please log in or register
mclegg
181
mclegg 2012.03.19 08:36 

Hello.

I am new to coding, and am learning a lot, however I have struck something that I cannot get my head around at the moment. I am trying to call the result of a test in one of my indicators, to an ea, so it can add it to its own tests before placing a trade. The ea is a simple moving average cross. The indicator is a trending or ranging indicator. I have attached the indicator code.

I would like help, and if possible explained simply so I can get my head around how it should be setup inside the indicator, for the ea to recognise it. Once I have the indicator sorted, I will move onto the ea.

The indicator has two tests, in which I would like to add the iCustom function. At this stage, I am only wanting to add it in one test, it doesn't matter which, as I am trying to understand it. Anyone willing to help and explain it simply, would be greatly beneficial - I cannot seem to find anything on-line that explains it so I understand it. The result of the test could just simply be "true".

Thanks in advance for you time.
Mike

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 2
#property  indicator_color1  Yellow
#property  indicator_color2  Red
#property  indicator_width1  1

//---- indicator parameters
extern string PairName = "";   // Leave blank for the pair of the chart, enter other pair name to compare correlated pairs

extern int StdDev.MA.Period=12;  // D1=20
extern int StdDev.MA.Shift=0;    //
extern int StdDev.MA.Method = 0; // 0=SMA 1=EMA 2=Smoothed 3=Linear Weighted
extern int StdDev.MA.Price = 0;  // 0 Close price, 1 Open price, 2 High price, 3 Low price, 4 Median price, (high+low)/2, 5 Typical price, (high+low+close)/3, 6 Weighted close price, (high+low+close+close)/4

extern int MA.Fast.Period = 3;
extern int MA.Fast.Method = 2;   //  0=SMA 1=EMA 2=Smoothed 3=Linear Weighted
extern int MA.Fast.Shift = 0;

extern bool CheckOncePerBar = true;

int i, limit, counted_bars;
static string Pair1;

datetime CurrentTimeStamp;

//---- indicator buffers
double     STDBuffer[];
double     stddevma[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  
   IndicatorDigits(Digits+1);
     
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE); // 
   SetIndexStyle(1,DRAW_LINE);

      
//---- indicator buffers mapping
   SetIndexBuffer(0, STDBuffer);
   SetIndexBuffer(1, stddevma);

   
   if (PairName == "") Pair1 = Symbol();
   else Pair1 = PairName;

//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("SFX TOR: "+Pair1+"("+StdDev.MA.Period+")");
   SetIndexLabel(0,"StdDev");
   SetIndexLabel(1,"StdDev MA");

//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {

   counted_bars=IndicatorCounted();
   
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   
//---- macd counted in the 1-st buffer
  for(int i=limit-1; i>=0; i--){
     STDBuffer[i]=iStdDev(Pair1,0,StdDev.MA.Period, StdDev.MA.Shift, StdDev.MA.Method, StdDev.MA.Price, i);
     }
  for(i=limit-1; i>=0; i--){
     stddevma[i] = iMAOnArray(STDBuffer, 0, MA.Fast.Period, MA.Fast.Shift, MA.Fast.Method, i);
     } 
    
//Execute on bar Open
    if( CheckOncePerBar == true ){
      if( CurrentTimeStamp == Time[0] )
         return( 0 );
   }
   
   CurrentTimeStamp = Time [0];
   
//Tests
        
      if( STDBuffer[1] > stddevma[1] ){
      if( STDBuffer[2] <= stddevma[2]){
         PlaySound("alert.wav");
         Alert(Symbol()," M",Period()," 30MCrossing");
         SendMail("30MCrossing","30MCrossing");
      }   
   }
   else{ // if it's not greater then it is either lesser than or equal
      if( STDBuffer[2] >= stddevma[2]){
         PlaySound("alert.wav");
         Alert(Symbol()," M",Period()," 30MCrossing");
         SendMail("30MCrossing","30MCrossing");  
      }
   }
          
//---- done
   return(0);
  }
Simon Gniadkowski
Moderator
18002
Simon Gniadkowski 2012.03.19 09:08  

iCustom allows you to access your Indicators buffers . . . it's that simple.

For example, you are writing an EA that has a strategy based on 2 Indicators, you could build the Indicator code into the EA, that is possible but iss a little involved as Indicator buffers don't work on EAs, you would have to use arrays and handle them in an "as series" fashion . . . the alternative is to have the Indicators running and access their buffers from the EA . . . this is what iCustom facilitates. No changes are needed to the Indicators . . . . the EA simply accesses the buffers it needs at the shift values it needs. In the iCustom call it can also pass any extern variables that are needed to setup the Indicator as applicable

mclegg
181
mclegg 2012.03.19 18:28  
RaptorUK:

iCustom allows you to access your Indicators buffers . . . it's that simple.

For example, you are writing an EA that has a strategy based on 2 Indicators, you could build the Indicator code into the EA, that is possible but iss a little involved as Indicator buffers don't work on EAs, you would have to use arrays and handle them in an "as series" fashion . . . the alternative is to have the Indicators running and access their buffers from the EA . . . this is what iCustom facilitates. No changes are needed to the Indicators . . . . the EA simply accesses the buffers it needs at the shift values it needs. In the iCustom call it can also pass any extern variables that are needed to setup the Indicator as applicable


Thanks for that RaptorUK, however it is the actual coding that I am having difficulties with. I am very new to MQL4, and have only been doing it a couple of months, and am still learning - so I am needing a simple explanation of the coding and how to set it up in the indicator.

Thanks

Mike

dabbler
1103
dabbler 2012.03.19 18:52  

RaptorUK:

In the iCustom call it can also pass any extern variables that are needed to setup the Indicator as applicable

Correction ...

In the iCustom call it

MUST

also pass any extern variables that are needed to set up the Indicator as applicable.
dabbler
1103
dabbler 2012.03.19 18:55  
NewCoder47:


Thanks for that RaptorUK, however it is the actual coding that I am having difficulties with. I am very new to MQL4, and have only been doing it a couple of months, and am still learning - so I am needing a simple explanation of the coding and how to set it up in the indicator.

Thanks

Mike

What you are evidently missing here is that you do not use iCustom in the indicator. The indicator is a stand alone piece of code. Your EA can now sneak a peek at what is happening in the indicator by using the iCustom command.

https://docs.mql4.com/indicators/iCustom

https://forum.mql4.com/42002

Ubzen
5394
Ubzen 2012.03.19 19:03  
dabbler:

Correction ...

In the iCustom call it

MUST

also pass any extern variables that are needed to set up the Indicator as applicable.
One can leave the extern values blank. The iCustom() indicator would then use it's default values.
Simon Gniadkowski
Moderator
18002
Simon Gniadkowski 2012.03.19 19:05  
dabbler:

Correction ...

In the iCustom call it

MUST

also pass any extern variables that are needed to set up the Indicator as applicable.

I have read different statements regarding this . . . I have never used iCustom so I can't speak from personal experience . . . but does your statement hold true even when the default extern variables are what are needed ?
dabbler
1103
dabbler 2012.03.19 19:09  
RaptorUK:
I have read different statements regarding this . . . I have never used iCustom so I can't speak from personal experience . . . but does your statement hold true even when the default extern variables are what are needed ?
Yes. The problem is that the last two parameters of the iCustom are crucial so you cannot omit anything.
Simon Gniadkowski
Moderator
18002
Simon Gniadkowski 2012.03.19 19:14  
dabbler:
Yes. The problem is that the last two parameters of the iCustom are crucial so you cannot omit anything.
Of course . . . thanks.
dabbler
1103
dabbler 2012.03.19 19:15  
ubzen:
One can leave the extern values blank. The iCustom() indicator would then use it's default values.
No you can't. I just tried it. It won't compile if you leave out one of the parameters by just leaving an empty place between the commas.
Ubzen
5394
Ubzen 2012.03.19 19:17  
NewCoder47:


.... I am very new to MQL4, and have only been doing it a couple of months, and am still learning - so I am needing a simple explanation of the coding and how to set it up in the indicator....

I recognize the indicator as BarrowBoy's Trend_Or_Range indicator. The key to understanding Indicators is understanding Arrays. I had a healthy fear of Arrays until I realized I've been using them since day-1 when using mql4. OrderSelect() and Ma_CrossOver Logics are some of the first things one have to understand in order to make any EA.

As Raptor and Dabber have pointed out. They're just a Series of Numbers counted Backwards or Forward. Indicators have the added value of utilizing allot more visual tools then Experts. Since this Custom Indicator employs allot of standard Indicators like Standard Deviation and Moving Averages, I recommend doing these calculations within the EA.

1234
To add comments, please log in or register