Alternate Function for IndicatorCounted()

 

Hi,

I got an indicator (Open-source) giving buying and selling signal.

I am trying to use that "indicator" in an EA.

But in that they are using a custom indicator function counted_bars=IndicatorCounted().

I read that (in Documents, here) this function cannot be used in EAs and scripts.

Can anyone tell me what is the alternate function or method for this function?

Thanks in advance.

With Regards,

Krishna. 

 
krishna_gopal_2:

Hi,

I got an indicator (Open-source) giving buying and selling signal.

I am trying to use that "indicator" in an EA.

But in that they are using a custom indicator function counted_bars=IndicatorCounted().

I read that (in Documents, here) this function cannot be used in EAs and scripts.

Can anyone tell me what is the alternate function or method for this function?

There isn't . . . call the Indicator from your EA using iCustom . . . Detailed explanation of iCustom - MQL4 forum
 

Hi,

     I complied a test code given below. (Just to included an indicator)

I got the errors like,

'init' - function already defined and has a body C:\Program Files (x86)\MetaTrader 4\experts\test.mq4 (2, 5)

'deinit' - function already defined and has a body C:\Program Files (x86)\MetaTrader 4\experts\test.mq4 (3, 5)

'start' - function already defined and has a body C:\Program Files (x86)\MetaTrader 4\experts\test.mq4 (4, 5)
#include <BFS_Stoc.mq4>
int init() {return(0);}
int deinit() {return(0);}
int start() {return(0);}

The indicator works fine. Help me with it.

Thanks in advance. 

 
krishna_gopal_2:

Hi,

     I complied a test code given below. (Just to included an indicator) 

I wrote this,  it's good advice, why don't you take it ?

RaptorUK:
There isn't . . . call the Indicator from your EA using iCustom . . . Detailed explanation of iCustom - MQL4 forum

 
RaptorUK:

I wrote this,  it's good advice, why don't you take it ?


 


int init() {return(0);}
int deinit() {return(0);}
int start()
{
int trade = iCustom(NULL, 0, "BFS_Stoc", 0,0);
Print(trade);
return(0);
}

I tried this code. But it is not returning the value exactly what the Indicator returns. 

///Custom Indicator////
int start() 
  {

   /////blah blah blah//// if buy return 1, else return 2....

   Print("Trade signal = ",trade_signal);
   return(trade_signal);
  }

EA prints the below given number "2147483647"

                           2013.06.04 21:10:45 test EURUSD,H1: 2147483647

Indicator Prints

                           2013.06.04 21:12:00 BFS_Stoc EURUSD,H1: Trade signal = 1

 
krishna_gopal_2:

I tried this code. But it is not returning the value exactly what the Indicator returns. 

So your "Indicator" doesn't use Indicator buffers ?
 
RaptorUK:
So your "Indicator" doesn't use Indicator buffers ?

"#property indicator_buffers 2" is there in the code. If this is wrong, can you please suggest alternative code. I need to get the signal (Buy or Sell) atlast.

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 LawnGreen
#property indicator_color2 Red
#property indicator_width1  1
#property indicator_width2  1
//----
extern bool SoundON=false;
extern bool EmailON=false;
//---- input parameters
extern int KPeriod=5;
extern int DPeriod=3;
extern int Slowing=3;
extern int MA_Method=0; // SMA 0, EMA 1, SMMA 2, LWMA 3
extern int PriceField=0; // Low/High 0, Close/Close 1
extern int OverBoughtLevel  =80;
extern int OverSoldLevel    =20;
extern bool show_KD_cross=false;
extern bool show_K_OBOScross=true;
extern bool show_D_OBOScross=false;
extern string note_Price="PriceField:  Low/High = 0, Close/Close = 1";
extern string _MA_Method="SMA0 EMA1 SMMA2 LWMA3";
double CrossUp[];
double CrossDown[];

double trade_signal = 0;

int flagval1=0;
int flagval2=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0, DRAW_ARROW, EMPTY);
   SetIndexArrow(0, 233);
   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(1, DRAW_ARROW, EMPTY);
   SetIndexArrow(1, 234);
   SetIndexBuffer(1, CrossDown);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
  int start() 
  {
   int limit, i, counter;
   double tmp=0;
   double fastMAnow, slowMAnow, fastMAprevious, slowMAprevious;
   double Range, AvgRange;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit= Bars-counted_bars;
     for(i=1; i<=limit; i++) 
     {
      counter=i;
      Range=0;
      AvgRange=0;
      for(counter=i; counter<=i+9; counter++)
        {
         //AvgRange=AvgRange+MathAbs(iStochastic(NULL, 0, KPeriod, DPeriod, Slowing,MA_Method, PriceField, MODE_MAIN, i)-iStochastic(NULL, 0, 
            KPeriod, DPeriod, Slowing,MA_Method, PriceField, MODE_MAIN, i+1));
         AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
        }
      Range=AvgRange/10;
      fastMAnow=iStochastic(NULL, 0, KPeriod, DPeriod, Slowing,MA_Method, PriceField, MODE_MAIN, i);
      fastMAprevious=iStochastic(NULL, 0, KPeriod, DPeriod, Slowing,MA_Method, PriceField, MODE_MAIN, i+1);
      slowMAnow=iStochastic(NULL, 0, KPeriod, DPeriod, Slowing,MA_Method, PriceField, MODE_SIGNAL, i);
      slowMAprevious=iStochastic(NULL, 0, KPeriod, DPeriod, Slowing,MA_Method, PriceField, MODE_SIGNAL, i+1);
      CrossUp[i]=EMPTY_VALUE;
      CrossDown[i]=EMPTY_VALUE;
      if
         (((show_KD_cross)&&(fastMAnow > slowMAnow) && (fastMAprevious < slowMAprevious))||
            ((show_K_OBOScross)&&(fastMAnow > OverSoldLevel) && (fastMAprevious < OverSoldLevel))||
            ((show_D_OBOScross)&&(slowMAnow > OverSoldLevel) && (slowMAprevious < OverSoldLevel)) )
        {
         if (i==1 && flagval1==0)
           {
            flagval1=1;
            flagval2=0;
            //if (SoundON) Alert("BUY signal at Ask=",Ask,"\n Bid=",Bid,"\n Time=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",
                     TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());
            //if (EmailON) SendMail("BUY signal alert","BUY signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", 
               Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
           }
         CrossUp[i]=Low[i] - Range*0.5;
         
         //Print("Buy Price = ",CrossUp[i]);
            ////////////////////////////////////
            //trade_type = 1; // Buy          //
            trade_signal = 1;         //
            ////////////////////////////////////
            
         //         CrossUp[i] = AvgRange;
         CrossDown[i]=EMPTY_VALUE;
        }
      else if
            (((show_KD_cross)&&(fastMAnow < slowMAnow) && (fastMAprevious > slowMAprevious))||
               ((show_K_OBOScross)&&(fastMAnow < OverBoughtLevel) && (fastMAprevious > OverBoughtLevel))||
               ((show_D_OBOScross)&&(slowMAnow < OverBoughtLevel) && (slowMAprevious > OverBoughtLevel)) )
           {
            if (i==1 && flagval2==0)
              {
               flagval2=1;
               flagval1=0;
               if (SoundON) Alert("SELL signal at Ask=",Ask,"\n Bid=",Bid,"\n Date=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",
                          TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());
               if (EmailON) SendMail("SELL signal alert","SELL signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", 
                       Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
              }
            CrossDown[i]=High[i] + Range*0.5;
            //Print("Buy Price = ",CrossDown[i]);
            //////////////////////////////////////
            //trade_type = 2; // Sell           //
            trade_signal = 2;    //
            //////////////////////////////////////
            
            //        CrossDown[i] = AvgRange;
            CrossUp[i]=EMPTY_VALUE;
           }
     }
//----
   //Print("Trade signal = ",trade_signal);
   return(trade_signal);
  }
//+------------------------------------------------------------------+
 
krishna_gopal_2:

"#property indicator_buffers 2" is there in the code. If this is wrong, can you please suggest alternative code. I need to get the signal (Buy or Sell) atlast.

OK, do yourself a big favour . . . .  read this thread   ( link below)  from start to finish, slowly, repeatedly until you understand it . . .  

RaptorUK:
There isn't . . . call the Indicator from your EA using iCustom . . . Detailed explanation of iCustom - MQL4 forum     <----- link,  click me ! !

. . .  then you will be able to get what you need from this Indicator,  I guarantee it.
 
RaptorUK:

OK, do yourself a big favour . . . .  read this thread   ( link below)  from start to finish, slowly, repeatedly until you understand it . . .  

. . .  then you will be able to get what you need from this Indicator,  I guarantee it.

 


I think I followed your advice and made some corrections in both EA and Indicator as given below.

Please check it out and tell me whats wrong with it.

Because its not working correctly. But output changed from 2147483647 to 1.

 In indicator,

#property indicator_buffers 3   //previous value was 2 here

double trade_signal[];

 In int()   I have added 

 SetIndexBuffer(2, trade_signal);

 In start() I have added

 trade_signal[0] = 1;         //For Buy Signal

 trade_signal[0] = 2;         //For Sell Signal

And EA code is

int init() {return(0);}
int deinit() {return(0);}
int start()
{
int trade = iCustom(NULL,0,"BFS_Stoc",2,0);
Print(trade);
return(0);
}

 Out Put is

2013.06.06 17:56:48     test EURUSD,H1: 1

Output should be '2'.

Please tell me whats the problem with it. I wrote this from what I have understood from the Topic RaptorUK: gave.

 
krishna_gopal_2:

I think I followed your advice and made some corrections in both EA and Indicator as given below.

Please check it out and tell me whats wrong with it.

Why are you changing the Indicator code ?  where was this mentioned as being needed ?  use the buffers already coded in the Indicator.
 
RaptorUK:
Why are you changing the Indicator code ?  where was this mentioned as being needed ?  use the buffers already coded in the Indicator.


Actually in the indicator they are simply drawing arrows. So I edited it to give number 1 for buying 2 for selling. That's all.

For that variable I increased the buffer. That's all. Please tell me whether the EA code is right or wrong. If wrong means why or what should I do.

Is my iCustom() is correct or not?

Reason: