Download MetaTrader 5

Help please / indicator convert to EA

To add comments, please log in or register
hosoway
32
hosoway  

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.

whroeder1
15900
whroeder1  
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
hosoway
32
hosoway  

>whroeder1

Thank you for your Answer! :)

I think I want to use iCustom.

To add comments, please log in or register