Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 09-19-2011, 09:58 AM   #1
cbart_1
Member
 
Join Date: Jul 2009
Location: Texas
Posts: 62
Thanks: 7
Thanked 0 times in 0 posts
Default making an alert sound only once on bar close

i have an alert that i use but i cant seem to figure out how to make it only sound once when it is set to alerm on the bar close. Right now it will sound on each tick of the bar. I want the alert to sound once as soon as the the two lines it is looking at crosses on the current bar but not to sound on each tick jump. In other words i want it to be a live indicator i guess is what you would call it.

if(sMAD[1] > sMA[1] && sMAD[0] < sMA[0] && sTime[0] == 1)
{
if(!Historical && sTime[0] == 1)
{
vPlayCount =
3;
PlaySound(iAlert);
}
}

This is what i have for now.
cbart_1 is offline  
Reply With Quote
Old 09-19-2011, 10:19 AM   #2
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,128
Thanks: 249
Thanked 427 times in 423 posts
Default

Hello cbart_1,

You could set CalculateOnBarClose = true and it will only check on bar close.

If you want to run your script with COBC = false, but only want this evaluated at bar close, could work with FirstTickOfBar for this, by adding FirstTickOfBar to your condition and referencing 1 bar back like in this reference sample.

FirstTickOfBar is also a good mechanism for resetting a bool flag that could control sound playback.

if (myBool)
{
PlaySound(iAlert);
myBool = false;
}

if (FirstTickOfBar)
myBool = true;
NinjaTrader_RyanM is offline  
Reply With Quote
Old 09-19-2011, 10:43 AM   #3
cbart_1
Member
 
Join Date: Jul 2009
Location: Texas
Posts: 62
Thanks: 7
Thanked 0 times in 0 posts
Default waht i said was wrong

I meant i have the indicator calculate on bar close set to false. This way if the bar ticks going up or down make the lines cross i only want the alarm to sound once, because the bar ticks make the lines cross multiple times on 1 bar. I just want it to sound once. This way it wont sound the alarm on every tick in a 1, 5, 10, 15, or 30 minute bar.
cbart_1 is offline  
Reply With Quote
Old 09-19-2011, 10:49 AM   #4
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,128
Thanks: 249
Thanked 427 times in 423 posts
Default

Thanks for the reply. Using a bool flag and resetting at FirstTickOfBar is a good option for this then.
NinjaTrader_RyanM is offline  
Reply With Quote
Old 09-19-2011, 10:56 AM   #5
cbart_1
Member
 
Join Date: Jul 2009
Location: Texas
Posts: 62
Thanks: 7
Thanked 0 times in 0 posts
Default i think i got it maybe

I used the FirstTickOfBar and made a variable true and made it false in the crossing code. So i think this way it should set it back to being true on the beginning of each bar.

if (FirstTickOfBar)
{
FBTick =
true;
}
if(sMAD[1] > sMA[1] && sMAD[0] < sMA[0] && sTime[0] == 1 && FBTick == true)
{
if(!Historical && sTime[0] == 1)
{
vPlayCount =
3;
PlaySound(iAlert);
FBTick =
false;
}
}
cbart_1 is offline  
Reply With Quote
Old 09-19-2011, 11:11 AM   #6
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,128
Thanks: 249
Thanked 427 times in 423 posts
Default

Yes, that's close, but will also want to include a check to the bool flag before playing sound.

if (FBTick)
{
PlaySound(iAlert);
FBTick = false;
}
NinjaTrader_RyanM is offline  
Reply With Quote
Old 09-19-2011, 12:22 PM   #7
cbart_1
Member
 
Join Date: Jul 2009
Location: Texas
Posts: 62
Thanks: 7
Thanked 0 times in 0 posts
Default now i get no alarm

Ok this is how i put it in my code but now i dont get an alarm.

if
(FirstTickOfBar)
{
FBTick =
true;
}

if(sMAD[1] > sMA[1] && sMAD[0] < sMA[0] && sTime[0] == 1)
{
if(!Historical && sTime[0] == 1)
{
if (FBTick)
{
vPlayCount =
3;
PlaySound(iAlert);
FBTick =
false;
}
}
}
cbart_1 is offline  
Reply With Quote
Old 09-19-2011, 01:18 PM   #8
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,128
Thanks: 249
Thanked 427 times in 423 posts
Default

Sorry, I'm not sure why that wouldn't work. Using bool flags is a common technique for executing something only once. It does look like you took some other parts of your script and included it after the bool check. Maybe that part of your script needs to be executed more than once per bar? I would try using my previous two examples used, where only PlaySound and bool assignment executes after your FirstTick returns true.

It's also good to learn techniques for debugging and simplifying your code, so you can follow code flow and work through these types of issues. Please see here for help debugging:
http://www.ninjatrader.com/support/f...ead.php?t=3418
NinjaTrader_RyanM is offline  
Reply With Quote
Old 09-19-2011, 01:32 PM   #9
cbart_1
Member
 
Join Date: Jul 2009
Location: Texas
Posts: 62
Thanks: 7
Thanked 0 times in 0 posts
Default

ok thank you it is someting wrong with my sound code. i will go through it and find out what is wrong.
cbart_1 is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
email alert every bar close meirba Miscellaneous Support 1 03-26-2011 10:16 PM
Alert throws to Alert window but no sound file... Blash Indicator Development 4 02-15-2011 10:26 AM
sound alert() for a big bar crmcwi Indicator Development 2 05-11-2010 05:30 AM
Alert on bar close, Chart Trader gg80108 Miscellaneous Support 1 12-29-2008 10:25 AM
Making an Alert on conditional indicator ericadam Charting 1 09-05-2008 02:26 PM


All times are GMT -6. The time now is 10:35 PM.

FULL RISK DISCLOSURE: Futures trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.