Please HELP! How to make PlaySound for alert only make a sound 3 times per candle not on every tick?

Don_xyZ
45
Don_xyZ  

Hi,

I am trying to make a proximity indicator based on 2 EMAs but I don't know how to make the sound alert only ring 3 times per candle. Can someone please help me with this? This is what I've done so far.


Thank you in advance!


void OnTick()
  {

   // create Array for prices
   double myMovingAverageArray1[], myMovingAverageArray2[];

   //define the properties of the Moving Average1
   int movingAverageDefinition1 = iMA (_Symbol,_Period,
34 ,0,MODE_EMA,PRICE_CLOSE);

   //define the properties of the Moving Average2
   int movingAverageDefinition2 = iMA (_Symbol,_Period,
55 ,0,MODE_EMA,PRICE_CLOSE); 

   //sort the price Array1 from the current candle downwards
   ArraySetAsSeries(myMovingAverageArray1,true);

   //sort the price Array2 from the current candle downwards
   ArraySetAsSeries(myMovingAverageArray2,true);

   //Defined MA1, one line, current candle, 3 candles, store result
   CopyBuffer(movingAverageDefinition1,0,0,3,myMovingAverageArray1);

   //Defined MA2, one line, current candle, 3 candles, store result
   CopyBuffer(movingAverageDefinition2,0,0,3,myMovingAverageArray2);

   if ( //Potential Buy trade alert
   (myMovingAverageArray2[0] > myMovingAverageArray1[0])
   && (myMovingAverageArray2[0] - myMovingAverageArray1[0] <0.00010)
   && (myMovingAverageArray2[0] - myMovingAverageArray1[0] >0.00000)
   )
   
   PlaySound("Store_Door_Chime-Mike_Koenig-570742973.wav");
   
  
   if ( //Potential Sell trade alert 
   (myMovingAverageArray1[0] > myMovingAverageArray2[0])
   && (myMovingAverageArray1[0] - myMovingAverageArray2[0] <0.00010)
   && (myMovingAverageArray1[0] - myMovingAverageArray2[0] >0.00000)
   )
   
   PlaySound("decay.wav");
   
  
  }
//+------------------------------------------------------------------+



Don_xyZ
45
Don_xyZ  
I forgot to mention, the code is for mql5.
Vladimir Karputov
Moderator
224652
Vladimir Karputov  
Don_xyZ :

First, fix the error of receiving the indicator handle: in MQL5, the indicator handle MUST be received ONCE (this is done in OnTick).

Example:  Receiving data from an indicator in an MQL5.

How to start with MQL5
How to start with MQL5
  • 2020.09.17
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
William Roeder
23727
William Roeder  
  1. Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. 2004
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 2020.07.05
              How to call indicators in MQL5 - MQL5 Articles 12 March 2010

  2. && (myMovingAverageArray1[0] - myMovingAverageArray2[0] <0.00010)
    Don't hard code constants - your code breaks on JPY pairs. If you want one PIP code it that way (1*PIP).

    PIP, Point, or Tick are all different in general.
              What is a TICK? - MQL4 programming forum 2014.08.03

    Unless you manually adjust your SL/TP for each separate symbol, using Point means code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points), and metals. Compute what a PIP is and use it, not points.
              How to manage JPY pairs with parameters? - MQL4 programming forum 2017.02.09
              Slippage defined in index points - Expert Advisors and Automated Trading - MQL5 programming forum 2018.01.15

  3.   (myMovingAverageArray2[0] > myMovingAverageArray1[0])
       && (myMovingAverageArray2[0] - myMovingAverageArray1[0] <0.00010)
       && (myMovingAverageArray2[0] - myMovingAverageArray1[0] >0.00000)

    The first and third conditions are identical. One or the other - not both.

  4. Don_xyZ: I am trying to make a proximity indicator based on 2 EMAs but I don't know how to make the sound alert only ring 3 times per candle. Can someone please help me with this? This is what I've done so far.

    Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help 2017.04.21

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum 2018.05.12

    if(condition){
       static datetime mute=0;
       datetime now = TimeCurrent();
       if(mute < now){ 
          mute = now + PeriodSeconds() / 3; // 3 times per candle max.
          Alert(…);
    }  }
    Is that so hard that you couldn't even attempt it?
Don_xyZ
45
Don_xyZ  
William Roeder:
  1. Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. 2004
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 2020.07.05
              How to call indicators in MQL5 - MQL5 Articles 12 March 2010

  2. Don't hard code constants - your code breaks on JPY pairs. If you want one PIP code it that way (1*PIP).

    PIP, Point, or Tick are all different in general.
              What is a TICK? - MQL4 programming forum 2014.08.03

    Unless you manually adjust your SL/TP for each separate symbol, using Point means code breaks on 4 digit brokers, exotics (e.g. USDZAR where spread is over 500 points), and metals. Compute what a PIP is and use it, not points.
              How to manage JPY pairs with parameters? - MQL4 programming forum 2017.02.09
              Slippage defined in index points - Expert Advisors and Automated Trading - MQL5 programming forum 2018.01.15

  3. The first and third conditions are identical. One or the other - not both.

  4. Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help 2017.04.21

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum 2018.05.12

    Is that so hard that you couldn't even attempt it?

So sorry if I appeared that way.

Fact is I am lacking in coding skills and admit that openly, I am still trying to learn and that code is the extent of my knowledge in mql5.

I really have no previous coding experience before, no IT background whatsoever so yes it's difficult for me.

I have since bought a course but none is available for mql5 (there are but judging by the reviews from the site I deemed it not worth my time) so I bought the one for mql4 instead and I am already able to make a very simple EA for EMA crossover on mql4 now which is of course absolutely meaningless compared to your library of knowledge.

That said, I am thankful that you showed some pointers to this newbie. Enjoy your day!


ps: yes, the code is designed specifically for GBP/USD and EUR/USD, I don't have any intention on using it on other pairs.