NexusFi: Find Your Edge


Home Menu

 





How do you play a sound only once while a condition is true over multiple bars?


Discussion in NinjaTrader

Updated
    1. trending_up 1,769 views
    2. thumb_up 0 thanks given
    3. group 4 followers
    1. forum 2 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
 whiterhino 
Seattle, Washington
 
Experience: Intermediate
Platform: NinjaTrader
Trading: ES,NQ RTY, YM, GC, CL, SI, etc
Posts: 6 since Mar 2011
Thanks Given: 6
Thanks Received: 1

Greetings!
Can anyone explain how to play a sound file once while x is true. Not once per bar, just once until x is false, then the sound file would play only the first time x is true again. No matter how many bars x is true, it only plays a sound the very first time in a sequence where x is true.

I tried this below but it still sounds once per bar, not just once, while x (a rising stoch indicator) is true over multiple bars.

///SOUND ALERT and Plot Color
///
///DS Rising

bool BoolSound = false;
int SoundCounter = 0;

if(
BoolSound == false
&& DoubleStochastics(5)[0] > DoubleStochastics(5)[1]
)
{
BoolSound = true;
PlotBrushes[3][0] = Brushes.PowderBlue; //MTF barsarray 3
SoundCounter = 1;
}
else {BoolSound = false; SoundCounter = 0;}

if (CurrentBar == currentBarFlag )
return;


if(BoolSound == true
&& SoundCounter == 1)
{
PlaySound(SoundFileBuy);
currentBarFlag = CurrentBar;
SoundCounter = SoundCounter + 1;
}

Thank you for your consideration!


Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
My Rant with AMP Futures
Trading Reviews and Vendors
No book, only a stair: journaling fills where price move …
Cryptocurrency
What we know about Binances $6.3bn hack - BBC News
Cryptocurrency
Building a free browser DOM/SMC simulator - need some ad …
Platforms and Indicators
requesting .nrd replay files from 2026 spy and qqq
The Elite Circle
 
  #2 (permalink)
 TraderGB 
Dallas, TX
 
Experience: Intermediate
Platform: NinjaTrader8
Trading: ES, CL
Posts: 183 since Mar 2012
Thanks Given: 1
Thanks Received: 414

whiterhino,

See if the code below resolves your issue.

TraderGB

 
Code
bool trendUp = true;

if (DoubleStochastics(5)[0] > DoubleStochastics(5)[1] && !trendUp)
{
   trendUp = true;
   PlaySound(SoundFileBuy);
}
else if (DoubleStochastics(5)[0] < DoubleStochastics(5)[1] && trendUp)
{
   trendUp = false;
   PlaySound(SoundFileSell);
}

whiterhino View Post
Greetings!
Can anyone explain how to play a sound file once while x is true. Not once per bar, just once until x is false, then the sound file would play only the first time x is true again. No matter how many bars x is true, it only plays a sound the very first time in a sequence where x is true.

I tried this below but it still sounds once per bar, not just once, while x (a rising stoch indicator) is true over multiple bars.

///SOUND ALERT and Plot Color
///
///DS Rising

bool BoolSound = false;
int SoundCounter = 0;

if(
BoolSound == false
&& DoubleStochastics(5)[0] > DoubleStochastics(5)[1]
)
{
BoolSound = true;
PlotBrushes[3][0] = Brushes.PowderBlue; //MTF barsarray 3
SoundCounter = 1;
}
else {BoolSound = false; SoundCounter = 0;}

if (CurrentBar == currentBarFlag )
return;


if(BoolSound == true
&& SoundCounter == 1)
{
PlaySound(SoundFileBuy);
currentBarFlag = CurrentBar;
SoundCounter = SoundCounter + 1;
}

Thank you for your consideration!


Reply With Quote
  #3 (permalink)
 
DavidBodhi's Avatar
 DavidBodhi 
Milwaukee, WI, USA
 
Experience: Intermediate
Platform: NinjaTrader, thinkorswim
Trading: Equities, stocks
Frequency: Many times daily
Duration: Seconds
Posts: 210 since Oct 2014
Thanks Given: 23
Thanks Received: 210


Hello, whiterhino.

In this kind of situation I create another boolean that flips when the sound has been played.

For example:

private bool SoundPlayed = false;

if (CONDITIONS ARE TRUE
&& SoundPlayed == false)
{
PlaySound;
SoundPlayed = true;
)

Then, when CONDITIONS are no longer true:

SoundPlayed = false;

This may require another if() statement if there are other things you want to happen independent of whether the sound was played or not, but the above concept will do the job. Just make sure to flip SoundPlayed back to false at the appropriate time.



whiterhino View Post
Greetings!
Can anyone explain how to play a sound file once while x is true. Not once per bar, just once until x is false, then the sound file would play only the first time x is true again. No matter how many bars x is true, it only plays a sound the very first time in a sequence where x is true.

I tried this below but it still sounds once per bar, not just once, while x (a rising stoch indicator) is true over multiple bars.

///SOUND ALERT and Plot Color
///
///DS Rising

bool BoolSound = false;
int SoundCounter = 0;

if(
BoolSound == false
&& DoubleStochastics(5)[0] > DoubleStochastics(5)[1]
)
{
BoolSound = true;
PlotBrushes[3][0] = Brushes.PowderBlue; //MTF barsarray 3
SoundCounter = 1;
}
else {BoolSound = false; SoundCounter = 0;}

if (CurrentBar == currentBarFlag )
return;


if(BoolSound == true
&& SoundCounter == 1)
{
PlaySound(SoundFileBuy);
currentBarFlag = CurrentBar;
SoundCounter = SoundCounter + 1;
}

Thank you for your consideration!


Follow me on X Reply With Quote




Last Updated on November 21, 2021


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts