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,111 views
    2. thumb_up 0 thanks given
    3. group 4 followers
    1. forum 2 posts
    2. attach_file 0 attachments




 
Search this Thread

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

  #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?
Better Renko Gaps
The Elite Circle
Deepmoney LLM
Elite Quantitative GenAI/LLM
Futures True Range Report
The Elite Circle
ZombieSqueeze
Platforms and Indicators
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
59 thanks
Funded Trader platforms
36 thanks
NexusFi site changelog and issues/problem reporting
25 thanks
GFIs1 1 DAX trade per day journal
19 thanks
The Program
18 thanks
  #2 (permalink)
 TraderGB 
Dallas, TX
 
Experience: Intermediate
Platform: NinjaTrader8
Trading: ES, CL
Posts: 183 since Mar 2012
Thanks Given: 1
Thanks Received: 411

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: 207


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 Twitter Reply With Quote




Last Updated on November 21, 2021


© 2024 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 - Privacy Policy - Downloads - Top
no new posts