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,516 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?
Hungary Called for Magyar at 97pct, Ending 16-Year Orban …
Prediction Markets & Event Contracts
Iran War Prediction Markets: Ceasefire 16%, Ground Invas …
Prediction Markets & Event Contracts
CPI Eve: Fed Hike Odds Hold at 52% for the First Time, I …
Prediction Markets & Event Contracts
SEC and CFTC Unlock Customer Cross-Margining for Treasur …
Treasury Notes and Bonds
NinjaTrader Parent Payward Acquires Bitnomial for $550M …
Platforms and Indicators
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
21 thanks
2026 Jlab journal
10 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Lady Vols Primer: Trading Volatility Journal
6 thanks
2026 Fire Horse
5 thanks
  #2 (permalink)
 TraderGB 
Dallas, TX
 
Experience: Intermediate
Platform: NinjaTrader8
Trading: ES, CL
Posts: 183 since Mar 2012
Thanks Given: 1
Thanks Received: 413

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
Trading: Equities
Posts: 209 since Oct 2014
Thanks Given: 23
Thanks Received: 209


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