Open Trade After Alert From Indicator

 

Hey all,

I was very happy with the help I received last time I posted in this forum, so I'm back with another n00b question.

How can I make my EA open a trade when an indicator running on a chart (say the EURUSD) gives an alert?

Specifically, I have an indicator that will put a little blue circle under a new candle when it says to open a BUY, and puts a little pink circle above a new candle when it says to SELL. It has proven to be a very effective indicator, however I am not always able to be at my computer when I receive these alerts/see the circles, so I want to integrate the indicator into an EA that will open the trades for me. I can provide the code for the indicator if the answer isn't blantantly obvious to someone out there. I was hoping it would be. Thanks again for the support! 

 
mattmoneywilson:

Hey all,

I was very happy with the help I received last time I posted in this forum, so I'm back with another n00b question.

How can I make my EA open a trade when an indicator running on a chart (say the EURUSD) gives an alert?

Specifically, I have an indicator that will put a little blue circle under a new candle when it says to open a BUY, and puts a little pink circle above a new candle when it says to SELL. It has proven to be a very effective indicator, however I am not always able to be at my computer when I receive these alerts/see the circles, so I want to integrate the indicator into an EA that will open the trades for me. I can provide the code for the indicator if the answer isn't blantantly obvious to someone out there. I was hoping it would be. Thanks again for the support! 

Attach the code, without the code, you and us are just guessing. 

In the indicator find the code that generate the alert, then use iCustom (https://docs.mql4.com/indicators/iCustom), replicate that code in EA.

Got that ?

 
hi there can u share the EA with us if yes up it here or send it to shabnamsshaikh@rediffmail.com 
 

Here is the code to the indicator. Near the bottom I have the two Alert and Notifications lines where it will pop up an alert on the computer and notify me by phone. I basically want to make an EA that will open the corresponding trades too. I have a basic EA layout that I can sub in the different buy and sell conditions easily, just need to know what to put. IE, the iCustom line I can utilize somehow? Thank you for the quick responses.

For some reason I couldn't get the "code" button to work so I apologize for the formatting. Thanks all.  

#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link      "https://www.metaquotes.net/"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua
#property indicator_color2 Magenta

extern int RISK=4;
extern int AllBars=250;
int up=0,dn=0;
double val1buffer[];
double val2buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
      SetIndexStyle(0,DRAW_ARROW,EMPTY);
      SetIndexArrow(0,108);
      SetIndexBuffer(0, val1buffer);

      SetIndexStyle(1,DRAW_ARROW,EMPTY);
      SetIndexArrow(1,108);
      SetIndexBuffer(1, val2buffer);
      return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
   {
     return(0);
   }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
// int    counted_bars=IndicatorCounted();

int start()
{  
  
   double value2;
   double value3;
   double value10=10;
   double value11;
   double x1=70;
   double x2=30;
   int TrueCount;
   int counter;
   int MRO1;
   int MRO2;
   int i1;
   double Range;
   double AvgRange;
   double val1;
   double val2;
   double Table_value2[500][2];
   int counted_bars=IndicatorCounted();
  
   value10=3+RISK*2;
   x1=67+RISK;
   x2=33-RISK;
   value11=value10;
  //----------------------------
   
   if(counted_bars<0) return (-1);
   if(counted_bars>0) counted_bars--;       //last bar recounted
   int i;
   int shift = Bars-counted_bars-1;
   if (shift > AllBars) shift = AllBars;
        
  for(i=shift; i>0; i--)
   {
  


  
                
      counter=i;
      Range=0;
      AvgRange=0;
      for (counter=i ;counter<=i+9;counter++)
      {
         AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
      }
      Range=AvgRange/10;
     
     
      counter=i;
      TrueCount=0;
      while (counter<i+9 && TrueCount<1)
      {
         if (MathAbs(Open[counter]-Close[counter+1])>=Range*2.0 )
               TrueCount++;
         counter++;
      }

      if (TrueCount>=1)
            MRO1=counter ;
      else
            MRO1=-1;
           
      counter=i;
      TrueCount=0;
      while (counter<i+6 && TrueCount<1)
      {
           if(MathAbs(Close[counter+3]-Close[counter])>=Range*4.6)
            {TrueCount++;}
           counter++;
      }
     
      if(TrueCount>=1)
            MRO2=counter;
      else
            MRO2=-1;
           
      if (MRO1>-1)
            value11=3;
      else
            value11=value10;
           
      if (MRO2>-1)
            value11=4;
      else
           value11=value10;
         
           
      value2=100-MathAbs(iWPR(NULL,0,value11,i));
      Table_value2[i][0]=i;
      Table_value2[i][1]=value2;
      val1=0;
      val2=0;
      value3=0;
      //-------------------     val1 
      if (value2<x2 )  //  x2 = 30
      {
         i1=1;
         while (Table_value2[i+i1][1]>=x2 && Table_value2[i+i1][1]<=x1)
         {i1++;}

         if (Table_value2[i+i1][1]>x1)
         {
            value3=High[i]+Range*0.5;
            val1=value3;
         }
      }
     
      //-------------------     val2 
      if ( value2>x1) // x1 = 70
      { 
            i1=1;
            while (Table_value2[i+i1][1]>=x2 && Table_value2[i+i1][1]<=x1)
            {i1++;}
           
            if (Table_value2[i+i1][1]< x2)
            {
               value3=Low[i]-Range*0.5;
               val2=value3;
            }
      }
     
     
      
    
      if (val2!=0 && up==0 )
      {    
           val1buffer[i]= val2-1*Point;
           up=1;
           dn=0;
           if(shift<=2)
           {
            Alert (Symbol()," ",Period(),"M  Asctrend BUY ");
            SendNotification("Check Buy");
            }
      } 
      if (val1 !=0 && dn==0)
      {
     
            val2buffer[i]= val1+1*Point;
            dn=1;
            up=0;
            if(shift<=2)
            {
            Alert (Symbol()," ",Period(),"M   Asctrend SELL ");
            SendNotification("Check Sell");
            }
       }
  
   }
return(0);

}

 
mattmoneywilson:

Here is the code to the indicator. Near the bottom I have the two Alert and Notifications lines where it will pop up an alert on the computer and notify me by phone. I basically want to make an EA that will open the corresponding trades too. I have a basic EA layout that I can sub in the different buy and sell conditions easily, just need to know what to put. IE, the iCustom line I can utilize somehow? Thank you for the quick responses.

For some reason I couldn't get the "code" button to work so I apologize for the formatting. Thanks all.  

I recently wrote about iCuatom https://www.mql5.com/en/forum/142102/page2#717112, and please read again documentation about iCustom (https://docs.mql4.com/indicators/iCustom).

Let me write partial of the code :

double buffer_1, buffer_2;

buffer_1 = iCustom (... , 0, 1);
buffer_2 = iCustom (... , 1, 1);

if (buffer_1 != EMPTY_VALUE) 
   {
   //--- buy
   }

if (buffer_2 != EMPTY_VALUE) 
   {
   //--- sell
   }

One thing to remember about your CI, you should open immediately right after the alert. I see from your other topic that you are using EA Builder, well, I don't know much about it but you have to make it open position immediately after it gets the signal.

 

Thanks for the response. I have reviewed both links you sent me. I am still unclear about what you put above. Why is the mode of buffer_1 set to 0 and the mode of buffer_2 set to 1? I am still new to mql4 programming so these concepts are still alien to me and I would love a full explanation as the references above don't shed much detail. Thanks for the help!

 
mattmoneywilson:

Thanks for the response. I have reviewed both links you sent me. I am still unclear about what you put above. Why is the mode of buffer_1 set to 0 and the mode of buffer_2 set to 1? I am still new to mql4 programming so these concepts are still alien to me and I would love a full explanation as the references above don't shed much detail. Thanks for the help!

If you don't understand the concept of Indicator buffers then you need to read up on them and gain an understanding.


This might help:  https://book.mql4.com/samples/icustom
 
mattmoneywilson:

Thanks for the response. I have reviewed both links you sent me. I am still unclear about what you put above. Why is the mode of buffer_1 set to 0 and the mode of buffer_2 set to 1? I am still new to mql4 programming so these concepts are still alien to me and I would love a full explanation as the references above don't shed much detail. Thanks for the help!

In mql4 CI capable of holding 8 buffers, and they are counted started from 0 to 7 - not from 1 to 8 - like this : 0, 1, 2, 3, 4, 5, 6, 7. You can name the buffer with any name you like. In my example I named buffer 0 as buffer_1, and buffer 1 as buffer_2. Of course I can named them anything else, like buffer 0 as Whatever_Line and buffer 1 as Line_Another, or buffer 0 as The_Fast and buffer 1 as The_Furious. Sorry if my name convention confuse you.
 

Thank you for the reference material. I have read them, but I still have no idea what I'm looking at. I understand now that a buffer is essentially a numeric "name" for a certain aspect of an indicator.

Now, when creating my EA, I have entered this after my int start():

  double buffer_1, buffer_2;

      buffer_1 = iCustom(NULL, 0, "PerkyAsctrend1.mq4",0,0,0);
      buffer_2 = iCustom(NULL, 0, "PerkyAsctrend1.mq4",0,1,0);

Am I correct so far? Recall from the indicator file (entitled PerkyAsctrend1.mq4):

//---- indicators
      SetIndexStyle(0,DRAW_ARROW,EMPTY);
      SetIndexArrow(0,108);
      SetIndexBuffer(0, val1buffer);

      SetIndexStyle(1,DRAW_ARROW,EMPTY);
      SetIndexArrow(1,108);
      SetIndexBuffer(1, val2buffer);
      return(0);

So I have the buffers lined up. Basically, the 0 buffer is the sell alert, and the 1 buffer is the buy alert. Do I need to do anything with the "..." parameters section of the iCustom line? If not, can I just do what phi.nuts put above and just have my trade conditions start when there is not an empty value in those buffers? Sorry to be a pain, I just hate not understanding. Thanks! 

 
mattmoneywilson:

Thank you for the reference material. I have read them, but I still have no idea what I'm looking at. I understand now that a buffer is essentially a numeric "name" for a certain aspect of an indicator.

Now, when creating my EA, I have entered this after my int start():

  double buffer_1, buffer_2;

      buffer_1 = iCustom(NULL, 0, "PerkyAsctrend1.mq4",0,0,0);
      buffer_2 = iCustom(NULL, 0, "PerkyAsctrend1.mq4",0,1,0);

Am I correct so far? Recall from the indicator file (entitled PerkyAsctrend1.mq4):

//---- indicators
      SetIndexStyle(0,DRAW_ARROW,EMPTY);
      SetIndexArrow(0,108);
      SetIndexBuffer(0, val1buffer);

      SetIndexStyle(1,DRAW_ARROW,EMPTY);
      SetIndexArrow(1,108);
      SetIndexBuffer(1, val2buffer);
      return(0);

So I have the buffers lined up. Basically, the 0 buffer is the sell alert, and the 1 buffer is the buy alert. Do I need to do anything with the "..." parameters section of the iCustom line? If not, can I just do what phi.nuts put above and just have my trade conditions start when there is not an empty value in those buffers? Sorry to be a pain, I just hate not understanding. Thanks! 

Yes, you need to do the "..." with the indicator extern input.
Reason: