I'm trying to play a sound at the same time that I get an arrow. I'm using the code below and get both an arrow and a sound when the conditions are true but I also get a sound when there is no arrow.
Can someone please tell me what I'm doing wrong?
Please register on futures.io to view futures trading content such as post attachment(s), image(s), and screenshot(s).
I'm testing for a bunch of conditions and if they are all true I draw an arrow. This part is working OK, no problems at all.
I now want to play an alert sound if the same conditions are true in addition to drawing the arrow. Do I need to repeat the code and test for all the same conditions again or is there some way to say "draw an arrow" AND "play a sound" for the same set of conditions?
Using PlaySound can be tricky because it will repeatedly play the sound while the condition exists. Therefore it is not very suitable out of the box for realtime, CalculateOnBarClose=false type indicators unless you code it right. You might want to set a bool variable to true when your condition triggers and on FirstTickofBar (the next bar) test for the bool and play the sound. Change variable to false. Playing in real time can work pretty much then same but every time your condition exists you would get the sound, even multiple times per bar unless you restrict it with code. You will have to figure out what you really want and code around it.
The following user says Thank You to eDanny for this post:
I think if you are calculation on bar close = true, you could do
if (soundOn &&...
if calc on bar close = false, then you would have to have the sound conditional on a flag and outside the brackets, since the arrow condition might be met but it might not be the 1st tick of bar anymore to trigger the sound.
The following user says Thank You to Tarkus11 for this post:
FYI, I played around with setting a bool variable, testing it and resetting it but in the end I decided to use the Alert function. Much easier to code and I also have the results in the Alert window as a bonus!
Just an FYI - I ran into this in the past too. For my purposes, the way I dealt with my own alert approach was
1) private int alertcounter = 0;
if (whateverConditions && alertcounter == 0)
3) Somewhere outside the brackets has to be some logical condition that resets alertcounter to 0 again for the next time. Like if the condition was an upcross of a shorter moving avg over a longer mov avg, when they cross in the other direction, alertcounter would be reset to 0 (though maybe there should be an upcrossalertcounter and downcrossalertcounter in that case...).
I could do stuff with the integer counter (like use modulus (%)) to alert on odd or even counter or whatever (if I chose not to reset it).