Help please / indicator convert to EA

 

Hi,

Firstly, I Have never  write new topic this forum, the format may be incorrect, sorry.


I would like to develop my first EA.

I tried to convert Custom Indicator to EA, but failed.


This codes was correctly compiled as Indicators and succeed sending mailalert.

Therefore, I thought that the convert processes have any mistakes.


#property copyright "hosoway"

//

extern int                FractalPeriod   = 25;

extern double             Lots            = 0.1;

extern int                TakeProfit      = 200;

extern int                StopLoss        = 200;

extern ENUM_APPLIED_PRICE PriceHigh       = PRICE_HIGH;

extern ENUM_APPLIED_PRICE PriceLow        = PRICE_LOW;


//---- input parameters

extern bool alert=true;

extern bool email=true;


double UpperBuffer[];

double LowerBuffer[];

double trend[];


//+------------------------------------------------------------------+


int init()

{

return(0);

}


int deinit() {  return(0); }


//+------------------------------------------------------------------+


//

///////////////////////////////////////////////////////////////////////////////

// Alert once

bool AlertOnce(string alert_msg, int ref)

   static datetime LastAlert[20];


   if(ref<1 || ref>20){

      Alert(ref+" ref error");

      return(false);

   }

   if(LastAlert[ref-1] != Time[0]){

      Alert(alert_msg,Symbol()," at ",Ask);

      LastAlert[ref-1] = Time[0];

      return(true);

   }

   return(false);

}

/////////////////////////////////////////////////////////////////////////////

//

// Email once

bool EmailOnce(string alert_msg, int ref)

   static datetime LastAlert[20];


   if(ref<1 || ref>20){

      Alert(ref+" ref error");

      return(false);

   }

   if(LastAlert[ref-1] != Time[0]){

      SendMail(alert_msg,Symbol()+Period()+"at"+DoubleToStr(Ask,Digits)+"¥n"+

      "ServerTime : "+TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS)+"¥n"+

      "LocalTime : "+TimeToStr(TimeLocal(),TIME_DATE|TIME_SECONDS)); 

      LastAlert[ref-1] = Time[0];

      return(true);

   }

   return(false);

}

    

//

int start()

{


//

   int cnt, CurrentPosition;

   int Ticket;

   int i,limit,counted_bars=IndicatorCounted();

   if(counted_bars<0) return(-1);

   if(counted_bars>0) counted_bars--;

           limit=MathMin(MathMax(Bars-counted_bars,FractalPeriod),Bars-1);

   //

   //check order


   CurrentPosition=-1;

   for(cnt=0;cnt<OrdersTotal();cnt++){

   OrderSelect(cnt,SELECT_BY_POS);

   if(OrderSymbol()==Symbol()) CurrentPosition=cnt;

   }

   //

   //

     int half = FractalPeriod/2;

     for(i=limit; i>=0; i--)

     {

         bool   found     = true;

         double compareTo = iMA(NULL,0,1,0,MODE_SMA,PriceHigh,i);

         for (int k=1;k<=half;k++)

            {

               if ((i+k)<Bars && iMA(NULL,0,1,0,MODE_SMA,PriceHigh,i+k)> compareTo) { found=false; break; }

               if ((i-k)>=0   && iMA(NULL,0,1,0,MODE_SMA,PriceHigh,i-k)>=compareTo) { found=false; break; }

            }

            

         if (found) UpperBuffer[i]=iMA(NULL,0,1,0,MODE_SMA,PriceHigh,i);

         else       UpperBuffer[i]=UpperBuffer[i+1];


         //

         //

         found     = true;

         compareTo = iMA(NULL,0,1,0,MODE_SMA,PriceLow,i);

         for (k=1;k<=half;k++)

            {

               if ((i+k)<Bars && iMA(NULL,0,1,0,MODE_SMA,PriceLow,i+k)< compareTo) { found=false; break; }

               if ((i-k)>=0   && iMA(NULL,0,1,0,MODE_SMA,PriceLow,i-k)<=compareTo) { found=false; break; }

            }

            

         if (found) LowerBuffer[i]=iMA(NULL,0,1,0,MODE_SMA,PriceLow,i);  

         else       LowerBuffer[i]=LowerBuffer[i+1];

         

         //

         

         trend[i] = trend[i+1];;

         if (Close[i]>UpperBuffer[i])                          trend[i] = 1;

         if (Close[i]<LowerBuffer[i])                          trend[i] =-1;

         if (Close[i]<UpperBuffer[i]&&Close[i]>LowerBuffer[i]) trend[i] = 0;

         }

         

            

   if (trend[1] == 1 && trend[2] == 0) 

           { 

           //Buy alert

           if(alert==true) AlertOnce("BlueSignal",1); 

           

           //Buy mail

           if(email==true) EmailOnce("BlueSignal Buy Ordered",1); 

           

           if(CurrentPosition==-1)   //none position

            {

            Ticket=OrderSend(Symbol(), OP_BUY, Lots, Ask, 5, Ask-(StopLoss*Point), Ask+(TakeProfit*Point), "Buy", 0, 0, Blue);             

            }

           }

           

         

   if (trend[1] == -1 && trend[2] == 0) 

           { 

           //sell alert

           if(alert==true) AlertOnce("RedSignal",1); 

           

           //sell mail

           if(email==true) EmailOnce("RedSignal Sell Ordered",1); 

           

           if(CurrentPosition==-1)   //none position

            {

             Ticket=OrderSend(Symbol(), OP_SELL, Lots, Bid, 5, Bid+(StopLoss*Point), Bid-(TakeProfit*Point), "Sell", 0, 0, Red); 

            }

           }

return(0);

        }   

Can someone please tell me the reason why not operate it?   Thank you.

 
hosoway: I tried to convert Custom Indicator to EA, but failed.


double UpperBuffer[];

double LowerBuffer[];

double trend[];

int start(){  

   int i,limit,counted_bars=IndicatorCounted();

         trend[i] = trend[i+1];;

         if (found) UpperBuffer[i]=iMA(NULL,0,1,0,MODE_SMA,PriceHigh,i);

         else       UpperBuffer[i]=UpperBuffer[i+1];

Can someone please tell me the reason why not operate it?   Thank you.

  1. There are no buffers in EAs. Your arrays have no size.
  2. There is no IndicatorCounted in EAs. Call fails.
  3. Don't try do that. Just get the value of the indicator into the EA and do what you want with it. You should write a self documenting function instead of calling iCustom directly, see Detailed explanation of iCustom - MQL4 forum
 

>whroeder1

Thank you for your Answer! :)

I think I want to use iCustom.

 
Yes you have to iCustom. But for each indicator uses different modes. For example in Tipu CCI code page They say: 
//---signal
int iSignal = iCustom(_Symbol,_Period,"Tipu CCI","",14,PRICE_TYPICAL,0,0,"",1,5,clrNONE,clrNONE,"",false,false,1,false,false,false,4,0);
//---trend
int iTrend = iCustom(_Symbol,_Period,"Tipu CCI","",14,PRICE_TYPICAL,0,0,"",1,5,clrNONE,clrNONE,"",false,false,1,false,false,false,5,0);

//---method#1
if(iSignal == OP_BUY) //buy signal, for sell signal use OP_SELL
if(iTrend == OP_BUY)  //buy trend, for sell trend use OP_SELL

//---method#2
bool bBuy = iCustom(_Symbol,_Period,"Tipu CCI","",14,PRICE_TYPICAL,0,0,"",1,5,clrNONE,clrNONE,"",false,false,1,false,false,false,6,0);
bool bSell = iCustom(_Symbol,_Period,"Tipu CCI","",14,PRICE_TYPICAL,0,0,"",1,5,clrNONE,clrNONE,"",false,false,1,false,false,false,7,0);

but if they had not said anything about it. I would use Signal-COunt-with array.mq4 for beginning to detect which modes correspond with signals. In the video below they try different indicator with this ex4:



 
I have just converted XMaster Formula indicator (that is sell buy signal indicator) to EA. and it works very well... Did you try it:
 
Hi I want to convert an indicator to an EA. This is ... You can use the iCustom function in order to get the indicator array value for each new tick.
Reason: