Play sound once when indicator arrow appears

 

Hi all,

I have an indicator I have coded that displays a buy/sell arrow when certain conditions are met. I want an alert sound when a new arrow appears and for the sound alert to play only once.

The code below continues to play the alert sound on every tick. I get why it does but how do I amend it so it plays only once but the Buy/Sell signals appear as normal? (I am having difficulty I think because of the int i for loop that is setup?) I have been searching and searching and i can't work it out.

Can anyone help a novice out please?

Many thanks

Jon


#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_width1 1
#property indicator_width2 1

double ema5;
double ema18;
double ema62;
double BarOpen;
double BarClose;
double BarHigh;
double BarLow;
double BuySignal[];
double SellSignal[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
  //---- indicators
  SetIndexStyle(0, DRAW_ARROW, EMPTY);
  SetIndexArrow(0, 233);
  SetIndexBuffer(0, BuySignal);
  SetIndexStyle(1, DRAW_ARROW, EMPTY);
  SetIndexArrow(1, 234);
  SetIndexBuffer(1, SellSignal);

  return(0);
}
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
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[])
{
  ema5 = 0;
  ema18 = 0;
  ema62 = 0;
  int counted_bars = IndicatorCounted();
  int i;
  int limit;
  if(counted_bars < 0) 
  return(-1);
  if(counted_bars > 0) 
  counted_bars--;
  limit = Bars - counted_bars;
  for(i=1; i<=limit; i++)
  {
    ema5 = iMA(NULL,0,5,0,MODE_EMA,PRICE_CLOSE,i);
    ema18 = iMA(NULL,0,18,0,MODE_EMA,PRICE_CLOSE,i);
    ema62 = iMA(NULL,0,62,0,MODE_EMA,PRICE_CLOSE,i);
    BarClose = iClose(NULL,0,i);
    BarLow = iLow (NULL,0,i);
    BarHigh = iHigh (NULL,0,i);
    BarOpen = iOpen (NULL,0,i);
 
       
    if (ema5>ema18 && ema18>ema62 && BarClose>ema5 && BarClose>BarOpen && BarLow<ema5)
     {
      BuySignal[i]=Low[i] - 2;
      PlaySound("alert.wav"); 
     } 
    if (ema5<ema18 && ema18<ema62 && BarClose<ema5 && BarClose<BarOpen && BarHigh>ema5)
     {
      SellSignal[i]=High[i] + 5;
      PlaySound("alert.wav");   
     }
     
   }
 
      
   return(rates_total);

}

 
                            
  


 
jonp7777 continues to play the alert sound on every tick.
  1. It has nothing to do with your loop except you shouldn't be alerting except on bar zero.
  2. You are alerting on a signal (ema5<ema18…). You can either:
    1. Remember the bar you last alerted on and don't repeat. Or
    2. Act on a change of signal.
                MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 2017.12.12

  3. You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 2016.05.11

    Your maximum lookback is 62.

 
William Roeder:
  1. It has nothing to do with your loop except you shouldn't be alerting except on bar zero.
  2. You are alerting on a signal (ema5<ema18…). You can either:
    1. Remember the bar you last alerted on and don't repeat. Or
    2. Act on a change of signal.
                MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 2017.12.12

  3. You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 2016.05.11

    Your maximum lookback is 62.


Thank you for your response William, much appreciated.

I am looking at the links you sent in detail. Not quite sure how and where to apply the change of signal in my code though.

As you can probably tell I have cobbled together bits of code to get this far...

Cheers

Jon

 

To be clear, when i load the indicator I want to the arrows to be drawn historically where the condition has been met and only play a sound alert (once) when a new arrow appears as the condition is met again going forward.

Would appreciate a further clue on how to apply the 'change of signal' William if possible?

Thank you!

 
jonp7777: Would appreciate a further clue on how to apply

Do you see words with underlines? Those are links. They take you to more information.

You would know how, had you bothered to click on the provided link.

 
William Roeder:

Do you see words with underlines? Those are links. They take you to more information.

You would know how, had you bothered to click on the provided link.

Was there any need for that rude reply? I am simply a novice looking for direction.

I am well aware they are links, I thanked you for them, and have "bothered" to go through them. 

I applied the change of signal to my code and had no joy hence my question for further help.

 
jonp7777: Was there any need for that rude reply?

Yes. I provided the code that tests for "a change of signal" yet you asked:

jonp7777: Would appreciate a further clue on how to apply the 'change of signal'

How To Ask Questions The Smart Way. 2004
     How To Interpret Answers.
          Dealing with rudeness.

MT4: Learn to code it.
MT5: Begin learning to code it.
If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.

 
William Roeder:

Yes. 


No. There wasn't.

You've certainly made your point though. Jeez.