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,665 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?
Warsh Drops Easing Bias, December Hike Now Above 50% -- …
Prediction Markets & Event Contracts
Would a node-based workflow for automated trading be useful?
Traders Hideout
Czechia Live at 52.5% as England Rides 4-2 Wave to Co-Fa …
Prediction Markets & Event Contracts
Trump Media to sell instant access to market-moving soci …
Traders Hideout
Datafeed for Atas
Platforms and Indicators
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Want your NinjaTrader indicator created, free?
5 thanks
Denied 20K payouts: Alpha Futures
1 thanks
FootPrintV2 Chart for NT8
1 thanks
BERN ALGOS algo trading journal
1 thanks
Afternoon-close session
1 thanks
  #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