Download MetaTrader 5

adding alert function to indi

To add comments, please log in or register
MQL5 Cloud Network speeds up calculations. Try it!
sky_lc
52
sky_lc 2014.07.09 15:31 

Hello,

I´ve been studying code for a few weeks and I plan to continue learning until i can say that i´m a expert :)... for now i´m a complete rookie and just trying to add simple functions to indicators that I like.

So, what brings me here is something that has been driving me nuts and my only explanation is that I´m not able to see what´s wrong because I don´t know enough about the subject. 

I´ve been trying to add a price cross alert for the indicator called Xma in two ways. the first is by adding the condition in the original code and the second is by using the icustom function... neither will work. Since these methods work on a regular MA i thought using the same aproach would work. But it seems that Xma isn´t plotting a value or, at least, i can´t find it.

 This is the original code

#property copyright "#Copyright © 2008, XrustSolution.#"
#property link      "#http://www.xrust.ucoz.net#"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Blue


extern int period=12;
extern int porog =3;
extern int metod =1;
extern int metod2=1;
extern int prise =0;
//---- buffers
double Signal[];
//+------------------------------------------------------------------+
void init()
  {
   SetIndexStyle(0,DRAW_LINE);
   SetIndexDrawBegin(0,0);
   SetIndexBuffer(0,Signal);
   IndicatorShortName("Xma"+period+porog);
   return;
  }
//+------------------------------------------------------------------+
int start() 

{
   double tmp1,tmp2;
   int i;

   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   if(counted_bars==0) limit--;

   for(i=limit;i>=0;i--)
     {
      tmp1=iMA(Symbol(),0,period,0,metod,prise,i);
      tmp2=iMA(Symbol(),0,period,1,metod2,prise,i);
      if(MathAbs(tmp1-tmp2)>=porog*Point)
        {
         Signal[i]=tmp2;
           }else{
         Signal[i]=Signal[i+1];
        }
     }
   return(0);
  }
//+------------------------------------------------------------------+   

 

adding a few lines to the original:

 

#property copyright "#Copyright © 2008, XrustSolution.#"
#property link      "#http://www.xrust.ucoz.net#"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Blue


extern int period=12;
extern int porog =3;
extern int metod =1;
extern int metod2=1;
extern int prise =0;
//---- buffers
double Signal[];
//+------------------------------------------------------------------+
void init()
  {
   SetIndexStyle(0,DRAW_LINE);
   SetIndexDrawBegin(0,0);
   SetIndexBuffer(0,Signal);
   IndicatorShortName("Xma"+period+porog);
   return;
  }
//+------------------------------------------------------------------+
int start() 

{
   double tmp1,tmp2;
   int i;

   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
   if(counted_bars==0) limit--;

   for(i=limit;i>=0;i--)
     {
      tmp1=iMA(Symbol(),0,period,0,metod,prise,i);
      tmp2=iMA(Symbol(),0,period,1,metod2,prise,i);
      if(MathAbs(tmp1-tmp2)>=porog*Point)
        {
         Signal[i]=tmp2;
           }else{
         Signal[i]=Signal[i+1];
        }
     }
// added this part

//--- bearish cross
     if(Open[1]>Signal[i] && Close[1]<Signal[i])
     {
               PlaySound("news.wav");
               Print("XMA has been crossed on " + Symbol() + ".");
            }   
//--- bullish cross
   if(Open[1]<Signal[i] && Close[1]>Signal[i])
     {
               PlaySound("news.wav");
               Print("XMA has been crossed on " + Symbol() + ".");
            }   
   return(0);
  }
//+------------------------------------------------------------------+   

 

using iCustom:

 

#property copyright "PipPicker"
#property link      ""

#property indicator_chart_window
#property indicator_buffers 0

extern bool       Active            = true;
extern string     _                 = " ---------- Moving Average Parameters ------";

extern int period=12;
extern int porog =3;
extern int metod =1;
extern int metod2=1;
extern int prise =0; 

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   if(Active && IsItNewBar())
      {
         double MA = iCustom(Symbol(),0,"Xma",period,porog,metod,metod2,prise);
         if((Open[1] >= MA && Close[1] <= MA) || (Open[1] <= MA && Close[1] >= MA))
            {
               PlaySound("Alert.wav");
               Print("MA has been crossed on " + Symbol() + ".");
            }   
      }   

   return(0);
}

bool IsItNewBar()
{
   static datetime lastTime;
   bool IsNewBar = (Time[0] != lastTime);
   lastTime = Time[0];

   return(IsNewBar);
}


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

 

Can anobody shed some light to what´s wrong?

 

thanks 

Rodrigo Malacarne
Moderator
9904
Rodrigo Malacarne 2014.07.09 19:41  
sky_lc:

Hello,

(...)

Can anobody shed some light to what´s wrong?

thanks 

Please post MQL4-related questions on the right forum.
MQL4: automated trading forum
  • www.mql5.com
MQL4: automated trading forum
sky_lc
52
sky_lc 2014.07.09 20:44  
oops... sorry :)
To add comments, please log in or register