Cannot create sound alert for every 1 minute

 
I tried to ring a sound whenever M1 High and Low's difference is greater than 0.01. However, the code below does not work when I attach the indicator to the chart, someone please help!
//+------------------------------------------------------------------+
//|                                               trueRangeAlert.mq5 |
//|                                                            owner |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "owner"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots 1
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
double arr[];
//--- indicator buffers mapping
SetIndexBuffer(0,arr,INDICATOR_DATA);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int  OnCalculate(
   const int        rates_total,       // price[] array size
   const int        prev_calculated,   // number of handled bars at the previous call
   const int        begin,             // index number in the price[] array meaningful data starts from
   const double&    price[]            // array of values for calculation
   )
{
  double pHigh=iHigh(_Symbol,_Period,1);
  double pLow= iLow(_Symbol,_Period,1);
  
  if ((pHigh-pLow)>0.01)
  {
    PlaySound("email.wav");
  }
  return rates_total;
}
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

Hi,

on which currency or symbol you are trying to run your indicator?
On the most currency pairs (e.g. EURUSD) the value of 0.01 means 1000 points or 100 pips
which is extremely rare for a M1 candle.

Best regards

 
Hi, I am trading with USDJPY. I think movement of JPY0.01 of USDJPY within one minute is not extreamely rare.
 

Hi,

i have tested your code in my environment and it works fine - but the sound is played
every tick and thats very often :-) I have modifyed your code a little and now the sound
is played only on a new bar.

  if (prev_calculated < rates_total)
  {
    double pHigh=iHigh(_Symbol,_Period,1);
    double pLow= iLow(_Symbol,_Period,1);
  
    if ((pHigh-pLow)>0.01)
    {
      PlaySound("email.wav");
    }
  }
  return rates_total;

If this dosen't work you should check if the soundfile exists at the right place (Sounds subdirectory in the
metatrader installation direcotry) and you can play it manually.

Best regards
 
Werner Klehr:

Hi,

i have tested your code in my environment and it works fine - but the sound is played
every tick and thats very often :-) I have modifyed your code a little and now the sound
is played only on a new bar.

If this dosen't work you should check if the soundfile exists at the right place (Sounds subdirectory in the
metatrader installation direcotry) and you can play it manually.

Best regards

Thank you very much for your reply.


Yes, it was due to misplaced sound file. Thank you for improved code too.