Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
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?
Thanks.
Can you help answer these questions from other members on NexusFi?
You have two tests going on here. They might be both true at the same time or separately. Without seeing more of your code it is hard to tell how to rewrite it.
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.
I think if you are calculation on bar close = true, you could do
if (ShowArrows)
{
Draw...
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.
Thanks guys, I really appreciate your help. Now I understand the problem.
I am using CalculateOnBarClose=false and will figure out how to code around it.
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;
2)
if (whateverConditions && alertcounter == 0)
{
Alert Stuff
alertcounter++;
}
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).