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



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


Discussion in NinjaTrader

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




Welcome to futures io: the largest futures trading community on the planet, with well over 125,000 members
  • 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 and simple.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Search this Thread
 

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

(login for full post details)
  #1 (permalink)
 whiterhino 
Seattle, Washington
 
Experience: Intermediate
Platform: NinjaTrader
Trading: ES,NQ RTY, YM, GC, CL, SI, etc
 
Posts: 6 since Mar 2011
Thanks: 6 given, 1 received

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 futures io?
Google Keyword Search Alerts
Traders Hideout
pay for programmer coding a custom indicator
Traders Hideout
Does anyone have the code for Anchored Vwap to use in Qu …
Traders Hideout
Store value in variable when emas crossover easy language
EasyLanguage Programming
NT 8 Strateg Builder issue I am baffled! Anyone?
NinjaTrader
 
 
(login for full post details)
  #2 (permalink)
 TraderGB 
Dallas, TX
 
Experience: Intermediate
Platform: NinjaTrader8
Trading: ES, CL
 
Posts: 167 since Mar 2012
Thanks: 1 given, 358 received

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
 
(login for full post details)
  #3 (permalink)
 DavidBodhi 
Milwaukee, WI, USA
 
Experience: Intermediate
Platform: NinjaTrader
Trading: Equities
 
DavidBodhi's Avatar
 
Posts: 183 since Oct 2014
Thanks: 17 given, 187 received


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


futures io Trading Community Platforms and Indicators NinjaTrader > How do you play a sound only once while a condition is true over multiple bars?


Last Updated on November 21, 2021


Upcoming Webinars and Events
 

NinjaTrader Indicator Challenge!

Ongoing
     



Copyright © 2021 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada), info@futures.io
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.
no new posts