Download MetaTrader 5

Help with trouble-shoot - crossover and alarm

To add comments, please log in or register
PetziK
20
PetziK  

Hi,

could someone help me please with that code. All I wanted is to get a spoken alarm when the two lines of the ADX Indicator (D+ and D-) cross over.

 

The code is repeating the sound file onevery tick - but I want it just to play one time.

Please, any ways to improve my code is welcome, thank you

Peter


#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Red
//----
input int ADXPeriod=14;
input bool VoiceYN = true;
input int ADXMainLevel=20;
bool AlarmFlagA=true;
bool AlarmFlagB=true;
bool LimitFlag = false;


//----
double ExtBuffer1[];
double ExtBuffer2[];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void XComment(string Text,bool ResetText=false)
  {
   static string ExistingComment = "";
   if(ResetText) ExistingComment = "";
   ExistingComment=ExistingComment+Text;
   Comment(ExistingComment);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexBuffer(0,ExtBuffer1);
   SetIndexStyle(0,DRAW_HISTOGRAM,0,2);
//----
   SetIndexBuffer(1,ExtBuffer2);
   SetIndexStyle(1,DRAW_HISTOGRAM,0,2);
//----
   IndicatorShortName("Advanced_ADX ("+ADXPeriod+")");
   return(0);

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()

/*int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  */
  {

   int i,limit;
   double ADX0,ADX1,ADX2,ADXMain,ADX1_1,ADX2_1,ADX1_2,ADX2_2;
   int counted_bars=IndicatorCounted();
   if(counted_bars<0)
      return(-1);
   if(counted_bars>0)
      counted_bars--;
   limit=Bars-counted_bars;
//----   
   for(i=0; i<limit; i++)
     {
      ADX0 = iADX(NULL, 0, ADXPeriod, PRICE_CLOSE, MODE_MAIN, i);
      ADX1 = iADX(NULL, 0, ADXPeriod, PRICE_CLOSE, MODE_PLUSDI, i);
      ADX2 = iADX(NULL, 0, ADXPeriod, PRICE_CLOSE, MODE_MINUSDI, i);

      ADXMain= iADX(NULL,0,ADXPeriod,PRICE_CLOSE,MODE_MAIN,0);
      ADX1_1 = iADX(NULL, 0, ADXPeriod, PRICE_CLOSE, MODE_PLUSDI, 0);
      ADX2_1 = iADX(NULL, 0, ADXPeriod, PRICE_CLOSE, MODE_MINUSDI, 0);
      ADX1_2 = iADX(NULL,0,ADXPeriod,PRICE_CLOSE, MODE_PLUSDI,1);
      ADX2_2 = iADX(NULL,0,ADXPeriod,PRICE_CLOSE, MODE_MINUSDI,1);

      //----
      if(ADX1>=ADX2)
        {
         ExtBuffer1[i] = ADX0;
         ExtBuffer2[i] = 0;
        }
      else
        {
         ExtBuffer1[i] = 0;
         ExtBuffer2[i] = ADX0;
        }

      if(ADXMain<ADXMainLevel) {LimitFlag=true;}
      else {LimitFlag=false; XComment("",true);}
      if(VoiceYN && ADXMain>=ADXMainLevel)
        {
         if(ADX1_1>ADX2_1 && ADX1_2<ADX2_2)
           {

            if(AlarmFlagA) PlaySound("60sekcallup.wav");
            AlarmFlagA= !AlarmFlagA;
            AlarmFlagB=true;

           }
         else if(ADX1_1<ADX2_1 && ADX1_2>ADX2_2)
           {

            if(AlarmFlagB) PlaySound("60sekputdown.wav");
            AlarmFlagB=!AlarmFlagB;
            AlarmFlagA=true;

           }

        }

      if(!AlarmFlagA) XComment("\nUP HOCH! ",true);
      if(!AlarmFlagB) XComment("\nDOWN TIEF! ",true);
      if(LimitFlag) XComment("\nADX_Main BELOW LIMIT: "+ADXMainLevel+" !!!!!",true);
      else XComment("\nADX_Main Limit: "+ADXMainLevel,true);
      //XComment("\nADXMain: "+ADXMain,false);
      //XComment("\nADX1_1: "+ADX1_1,false);
      //XComment("\nADX2_1: "+ADX2_1,false);

     }
   return(0);

  }
//+------------------------------------------------------------------+
PetziK
20
PetziK  

I think I found a way.

Please, use code and test if you are interested

 

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Red
//----
#define SIGNAL_NONE 0
#define SIGNAL_UP   1
#define SIGNAL_DOWN  2

input int ADXPeriod=14;
input bool VoiceYN = true;
input int ADXMainLevel=20;
input string xx="Wait until next sound:... [Seconds]";
extern int PauseTimer=60;
bool LimitFlag=false;
bool AlarmFlagA,AlarmFlagB=false;
int UDSignal=0;

//----
double ExtBuffer1[];
double ExtBuffer2[];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void XComment(string Text,bool ResetText=false)
  {
   static string ExistingComment = "";
   if(ResetText) ExistingComment = "";
   ExistingComment=ExistingComment+Text;
   Comment(ExistingComment);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexBuffer(0,ExtBuffer1);
   SetIndexStyle(0,DRAW_HISTOGRAM,0,2);
//----
   SetIndexBuffer(1,ExtBuffer2);
   SetIndexStyle(1,DRAW_HISTOGRAM,0,2);
//----
   IndicatorShortName("Advanced_ADX ("+ADXPeriod+")");
   PauseTimer=PauseTimer*1000;
   return(0);

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool NewBar()
{
   static datetime dt = 0;
   if (dt != Time[0])
   {
      dt = Time[0]; Sleep(100); // wait for tick
      return(true);
   }
   return(false);
}

int start()

/*int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  */
  {

   int i,limit,zz;
   double ADX0,ADX1,ADX2,ADXMain,ADX1_1,ADX2_1,ADX1_2,ADX2_2;
   int counted_bars=IndicatorCounted();
   if(counted_bars<0)
      return(-1);
   if(counted_bars>0)
      counted_bars--;
   limit=Bars-counted_bars;
//----   
   for(i=0; i<limit; i++)
     {
      ADX0 = iADX(NULL, 0, ADXPeriod, PRICE_CLOSE, MODE_MAIN, i);
      ADX1 = iADX(NULL, 0, ADXPeriod, PRICE_CLOSE, MODE_PLUSDI, i);
      ADX2 = iADX(NULL, 0, ADXPeriod, PRICE_CLOSE, MODE_MINUSDI, i);

      ADXMain= iADX(NULL,0,ADXPeriod,PRICE_CLOSE,MODE_MAIN,0);
      ADX1_1 = iADX(NULL, 0, ADXPeriod, PRICE_CLOSE, MODE_PLUSDI, 0);
      ADX2_1 = iADX(NULL, 0, ADXPeriod, PRICE_CLOSE, MODE_MINUSDI, 0);
      ADX1_2 = iADX(NULL,0,ADXPeriod,PRICE_CLOSE, MODE_PLUSDI,1);
      ADX2_2 = iADX(NULL,0,ADXPeriod,PRICE_CLOSE, MODE_MINUSDI,1);

      //----
      if(ADX1>=ADX2)
        {
         ExtBuffer1[i] = ADX0;
         ExtBuffer2[i] = 0;
        }
      else
        {
         ExtBuffer1[i] = 0;
         ExtBuffer2[i] = ADX0;
        }

      zz=0;
      if(ADX1_1>ADX2_1 && ADX1_2<ADX2_2)
        {
         UDSignal=SIGNAL_UP; zz++;
        }
      if(ADX1_1<ADX2_1 && ADX1_2>ADX2_2)
        {
         UDSignal=SIGNAL_DOWN; zz++;
        }

      if(ADXMain<ADXMainLevel) {LimitFlag=true;}
      else {LimitFlag=false; XComment("",true);}
      if(VoiceYN && !LimitFlag)
        {
         if(UDSignal==SIGNAL_UP)
           {

            if(NewBar())PlaySound("60sekcallup.wav");
            UDSignal=SIGNAL_NONE;
            AlarmFlagA=true;

           }
         //return(0);

         if(UDSignal==SIGNAL_DOWN)
           {

            if(NewBar())PlaySound("60sekputdown.wav");
            UDSignal=SIGNAL_NONE;
            AlarmFlagB=true;
           }
         // return(0);

        }
      //return(0);

      if(AlarmFlagA) XComment("\nUP HOCH! ",true);
      if(AlarmFlagB) XComment("\nDOWN TIEF! ",true);
      AlarmFlagA = false;
      AlarmFlagB = false;
      if(LimitFlag) XComment("\nADX_Main BELOW LIMIT: "+ADXMainLevel+" !!!!!",true);
      else XComment("\nADX_Main Limit: "+ADXMainLevel,true);
      XComment("\nSignal: "+UDSignal,false);
      XComment("\nZ: "+zz,false);
      //XComment("\nPauseTimer: "+PauseTimer,false);

     }
   return(0);

  }
//+------------------------------------------------------------------+
To add comments, please log in or register