adding price cross alert to indi

 

Hello,

I´ve been studying code for a few weeks and I plan to continue learning until i can say that i´ve mastered it :)... 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 outputting a value like a regular MA or, at least, i can´t find it. What I don´t understand is that it´s built on two regular MA´s, so it should work the same way, shouldn´t 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. tried with Signal[i] but don´t know what else to use :

 

#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 another MA cross alert indi just, just replaced iMA with 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);              //just replaced iMA(Symbol and so on) with iCustom
         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 anybody shed some light to what´s wrong?

 

thanks in advance

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

Wow! thank you so much for your fast reply and for your help! 

It WORKS!!!! ...with an unexpected problem... but it really WORKS!! :)

 Well, the unexpected problem is that until the next candle closes, every tick shoots an alert... i think i´ll have to isolate the first tick...

 

regarding the icustom, is it possible to call the indicator the way I described it? My only interest in it is the possibility of building an ea... 

 

I haven't read the code in the second indicator

because it seems to me senseless to make an indicator to call another indicator, especially, when you have an open source

but of course it's possible to call iCustom

 
qjol:

I haven't read the code in the second indicator

because it seems to me senseless to make an indicator to call another indicator, especially, when you have an open source

but of course it's possible to call iCustom


Well, you made a really good point. There´s no need to call an outside indicator if our indicator can do the job. But in the case of an EA, if i understood correctly, one should call iCustom, right? at least in this case so the EA can access the buffer. 

 So in this case (below), where this is the code for the standard moving average EA that comes in mt4, i just replaced iMA with iCustom and the apropriate extern parameters.

#define MAGICMA  20131111

extern string   Xma     ;                       //= "------------ XMA settings";
extern int period=12;
extern int porog =3;
extern int metod =1;
extern int metod2=1;
extern int prise =0;  
//--- Inputs
input double Lots          =0.1;
input double MaximumRisk   =0.02;
input double DecreaseFactor=3;
//input int    MovingPeriod  =12;
//input int    MovingShift   =6;
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//---
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//--- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//--- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//--- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
           {
            Print("Error in history!");
            break;
           }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL)
            continue;
         //---
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1)
         lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//--- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double xma;
   int    res;
//--- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//--- get Moving Average 
   xma=iCustom(Symbol(),0,"Xma",period,porog,metod,metod2,prise);
    Print("Xma =",xma,"  error =",GetLastError());
//--- sell conditions
   if(Open[1]>xma && Close[1]<xma)
     {
      res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
      return;
     }
//--- buy conditions
   if(Open[1]<xma && Close[1]>xma)
     {
      res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
      return;
     }
//---
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
   double xma;
//--- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//--- get Moving Average 
   xma=iCustom(Symbol(),0,"Xma",period,porog,metod,metod2,prise);
//---
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
      //--- check order type 
      if(OrderType()==OP_BUY)
        {
         if(Open[1]>xma && Close[1]<xma)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
               Print("OrderClose error ",GetLastError());
           }
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if(Open[1]<xma && Close[1]>xma)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
               Print("OrderClose error ",GetLastError());
           }
         break;
        }
     }
//---
  }
//+------------------------------------------------------------------+
//| OnTick function                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false)
      return;
//--- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
//---
  }
//+------------------------------------------------------------------+

 

  although it calls the indicator, doesn´t open any order. notice that also added :

Print("Xma =",xma,"  error =",GetLastError());

and all i get is Xma =0 ; error =0 

Maybe the indicator isn´t outputing a value that can be read. Any ideas?

 

is this all nonsense? should i build the EA without calling an outsideindicator? 

 

you missed 2 parameters for the iCustom call

double  iCustom(
   string       symbol,           // symbol
   int          timeframe,        // timeframe
   string       name,             // path/name of the custom indicator compiled program
   ...                            // custom indicator input parameters (if necessary)
   int          mode,             // line index      <-----
   int          shift             // shift           <-----
   );
 

i can´t believe i missed that :/

 

it WORKS now!!! qjol, i can´t thank you enough. you absolutely nailed everything.

 

thank you so much for your time, knowledge and patience. 

 
qjol:

you missed 2 parameters for the iCustom call


There is a problem with the tooltip for iCustom() it only shows the first three parameters. I told the service desk about that a while back but it has still not been fixed.
 
SDC:

There is a problem with the tooltip for iCustom() it only shows the first three parameters. I told the service desk about that a while back but it has still not been fixed.


ah, maybe that´s why I missed them. Never seen them really. But makes sense, couldn´t call the buffers any other way...

 

thank you 

Reason: