NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 12-09-2009, 06:41 PM   #1
fredyyy
Junior Member
 
Join Date: May 2009
Posts: 18
Thanks: 0
Thanked 0 times in 0 posts
Default How to make this work? SOUND ALERT

Hey guys, im sorry if this is repost, but i've looked around and i still cant get trough this:

Im trying to create an indicator which gives out a sound alert at the first bar tick after a MA crossover.

When i write it, i have to "compile" it to finish it right?

Im using two variable moving averages (a slow one with MAperiod and typeint parameters 150, 4 and a fast one with 30, 4)

Ive coded a lot but when im compiling the indicator it always gives me an error.

Here's what i got so far:



#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
#endregion

private bool soundOn = true; // on/off switch for audio alerts
private string alert1Sound = "long.wav"; //audio alert for Condition1
private string alert2Sound ="short.wav"; //audio alert for Condition2
private bool Condition1 = CrossAbove(MAV(30,30,4), MAV(150,150,4));//will be set to the conditions for alert1
private bool Condition2 = CrossBelow(MAV(30,30,4), MAV(150,150,4)); //will be set to the conditions for alert2

(Are the bool statements above well written? are they in the right space? I didnt know what to put in the MAV parameters since i only know 2 and it requires 3... the MAV im using is the NT standart)

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (Condition1 && soundOn && FirstTickOfBar) PlaySound(alert1Sound);
if (Condition2 && soundOn && FirstTickOfBar) PlaySound(alert2Sound);
}

#region Properties
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries Plot0
{
get { return Values[0]; }
}

[Description("")]
[Category("Parameters")]
public int MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); }
}
#endregion
}
[Description("what will help the user decide how to choose their entry")]
[Category("Parameters")]
public string LongSound (it gives me an error right here, saying something's wrong with this line)
{
get { return alert1Sound; } // this name matches that in the Variables section
set { alert1Sound = value; }
}
[Description("what will help the user decide how to choose their entry ")]
[Category("Parameters")]
class string ShortSound (and this line)
{
get { return alert2Sound; } // this name matches that in the Variables section
set { alert2Sound = value; }
}
}


You can cut out anything you find to be wrong or useless..

Thanks a lot to anyone who can give me a hand
fredyyy is offline  
Reply With Quote
Old 12-10-2009, 03:19 AM   #2
J_o_s
Senior Member
 
Join Date: Aug 2008
Location: Netherlands
Posts: 159
Thanks: 17
Thanked 5 times in 5 posts
Default

Hey Fredyyy,
Quote:
Originally Posted by fredyyy View Post
(....)
private bool soundOn = true; // on/off switch for audio alerts
private string alert1Sound = "long.wav"; //audio alert for Condition1
private string alert2Sound ="short.wav"; //audio alert for Condition2
This could work, however, you shouldn't forget to "tell" Ninjatrader in which directory these files are located (through the use of Alert() is this done automatically, see below).

Quote:
private bool Condition1 = CrossAbove(MAV(30,30,4), MAV(150,150,4));//will be set to the conditions for alert1
private bool Condition2 = CrossBelow(MAV(30,30,4), MAV(150,150,4)); //will be set to the conditions for alert2

(Are the bool statements above well written? are they in the right space? I didnt know what to put in the MAV parameters since i only know 2 and it requires 3... the MAV im using is the NT standart)
What do you mean with MAV? A simple moving average I guess? Then you have to use the SMA. Also, you're missing the lookback period of the CrossAbove statement.

I'm wondering what kind of 'moving averages' you intend to use, because in your post you state you have two moving averages (a long period and a short period). However, in your code I see 3 different periods? (150 bars, 4 bars, and 30 bars).

Perhaps you could try this:
Code:
// gives "true" if the SMA 30 closes above the SMA 150 on the last bar
private bool condition1 = CrossAbove(SMA(Close, 30), SMA(Close, 150), 1);  
 
// gives "true" if the SMA 30 closes below the SMA 150 on the last bar
private bool condition2 = CrossBelow(SMA(Close, 30), SMA(Close, 150), 1);
And for your sound alert:
Code:
private string alert1Sound = "long.wav"; 
// the name of the file in the Ninjatrader folder (probably C:\Program Files\NinjaTrader Installation Folder\sounds)
 
private string alert2Sound ="short.wav"; 
//the name of the file in the Ninjatrader folder (probably C:\Program Files\NinjaTrader Installation Folder\sounds)
 
(...)
if(condition1 == true)
{
Alert("Buy signal", NinjaTrader.Cbi.Priority.High, "A crossabove just took place", alert1Sound, 0, Color.Black, Color.Yellow);
}
if(condition2 == true)
{
Alert("Sell signal", NinjaTrader.Cbi.Priority.High, "A crossbelow just took place", alert2Sound, 0, Color.Black, Color.Yellow);
}
See also this help section on Alert().


Good luck and I hope my reaction is of some use to you.
J_o_s is offline  
Reply With Quote
Old 12-10-2009, 09:20 AM   #3
fredyyy
Junior Member
 
Join Date: May 2009
Posts: 18
Thanks: 0
Thanked 0 times in 0 posts
Default J o s

Thank you!

You are right about the Simple moving averages, my bad!

This is what i corrected after your reply:

#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// User defined variables (add any user defined variables below)
#endregion

private bool soundOn = true; // on/off switch for audio alerts
private string alert1Sound = "long.wav"; //audio alert for Condition1
private string alert2Sound ="short.wav"; //audio alert for Condition2
private bool condition1 = CrossAbove(SMA(Close, 30), SMA(Close, 150), 1);//will be set to the conditions for alert1
private bool condition2 = CrossBelow(SMA(Close, 30), SMA(Close, 150), 1); //will be set to the conditions for alert2

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
private string alert1Sound = "long.wav";
// the name of the file in the Ninjatrader folder (probably C:\Program Files\NinjaTrader Installation Folder\sounds) (it is exactly in that folder, do i need to change anything?)

private string alert2Sound ="short.wav";
//the name of the file in the Ninjatrader folder (probably C:\Program Files\NinjaTrader Installation Folder\sounds)

(...) ( <--- Should i leave this? )
if(condition1 == true)
{
Alert("Buy signal", NinjaTrader.Cbi.Priority.High, "A crossabove just took place", alert1Sound, 0, Color.Black, Color.Yellow);
}
if(condition2 == true)
{
Alert("Sell signal", NinjaTrader.Cbi.Priority.High, "A crossbelow just took place", alert2Sound, 0, Color.Black, Color.Yellow);
}
}

#region Properties
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries Plot0
{
get { return Values[0]; }
}

[Description("")]
[Category("Parameters")]
public int MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); }
}
#endregion
}
[Description("what will help the user decide how to choose their entry")]
[Category("Parameters")]
public string LongSound
{
get { return alert1Sound; } // this name matches that in the Variables section
set { alert1Sound = value; }
}
[Description("what will help the user decide how to choose their entry ")]
[Category("Parameters")]
class string ShortSound
{
get { return alert2Sound; } // this name matches that in the Variables section
set { alert2Sound = value; }
}
}

Have i made the corrections properly? Did i pasted in the right space?


also, Is there any chance to match this to only emit a sound alert when the crossover is followed (in the next 3 or 5 bars) by a cross above/below zero of the ElliotOscilator (fast 71, slow 93, close) ?

Ive made this indicator especially slow, so that i only care about the MA crossovers when the Elliot crosses zero.

What do you think?

Sorry for my n00bieness at programming btw, im really clueless....
fredyyy is offline  
Reply With Quote
Old 07-29-2010, 02:43 AM   #4
luxurious_04
Senior Member
 
Join Date: Jul 2010
Location: Philippines
Posts: 248
Thanks: 7
Thanked 6 times in 4 posts
Send a message via Skype™ to luxurious_04
Default Help please....

#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Gui.Chart;
#endregion

// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
/// <summary>
/// It will alert if the current time is equal to 2:30 in the morning at every friday.
/// </summary>
[Description("It will alert if the current time is equal to 2:30 in the morning at every friday.")]
public class FridayAlert : Indicator
{
#region Variables
// Wizard generated variables
private string alert1 = tradeAlert; // Default setting for Alert1
// User defined variables (add any user defined variables below)
#endregion

/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
Overlay = false;
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Use this method for calculating your indicator values. Assign a value to each
// plot below by replacing 'Close[0]' with your own formula.
//Plot0.Set(Close[0]);
if (Time[0].DayOfWeek==DayOfWeek.Friday && (ToTime(Time[0]==023000)))
{
Alert("Friday Alert",NinjaTrader.Cbi.Priority.Medium,"Sound Alert",alert1,0,Color.Black,Color.Yellow);
}
}

#region Properties
[Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
[XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
public DataSeries Plot0
{
get { return Values[0]; }
}

[Description("Alert if 2:30 am")]
[GridCategory("Parameters")]
public bool Alert1
{
get { return alert1; }
set { alert1 = value; }
}
#endregion
}
}

#region NinjaScript generated code. Neither change nor remove.
// This namespace holds all indicators and is required. Do not change it.
namespace NinjaTrader.Indicator
{
public partial class Indicator : IndicatorBase
{
private FridayAlert[] cacheFridayAlert = null;

private static FridayAlert checkFridayAlert = new FridayAlert();

/// <summary>
/// It will alert if the current time is equal to 2:30 in the morning at every friday.
/// </summary>
/// <returns></returns>
public FridayAlert FridayAlert(bool alert1)
{
return FridayAlert(Input, alert1);
}

/// <summary>
/// It will alert if the current time is equal to 2:30 in the morning at every friday.
/// </summary>
/// <returns></returns>
public FridayAlert FridayAlert(Data.IDataSeries input, bool alert1)
{
if (cacheFridayAlert != null)
for (int idx = 0; idx < cacheFridayAlert.Length; idx++)
if (cacheFridayAlert[idx].Alert1 == alert1 && cacheFridayAlert[idx].EqualsInput(input))
return cacheFridayAlert[idx];

lock (checkFridayAlert)
{
checkFridayAlert.Alert1 = alert1;
alert1 = checkFridayAlert.Alert1;

if (cacheFridayAlert != null)
for (int idx = 0; idx < cacheFridayAlert.Length; idx++)
if (cacheFridayAlert[idx].Alert1 == alert1 && cacheFridayAlert[idx].EqualsInput(input))
return cacheFridayAlert[idx];

FridayAlert indicator = new FridayAlert();
indicator.BarsRequired = BarsRequired;
indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
indicator.Input = input;
indicator.Alert1 = alert1;
Indicators.Add(indicator);
indicator.SetUp();

FridayAlert[] tmp = new FridayAlert[cacheFridayAlert == null ? 1 : cacheFridayAlert.Length + 1];
if (cacheFridayAlert != null)
cacheFridayAlert.CopyTo(tmp, 0);
tmp[tmp.Length - 1] = indicator;
cacheFridayAlert = tmp;
return indicator;
}
}
}
}

// This namespace holds all market analyzer column definitions and is required. Do not change it.
namespace NinjaTrader.MarketAnalyzer
{
public partial class Column : ColumnBase
{
/// <summary>
/// It will alert if the current time is equal to 2:30 in the morning at every friday.
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.FridayAlert FridayAlert(bool alert1)
{
return _indicator.FridayAlert(Input, alert1);
}

/// <summary>
/// It will alert if the current time is equal to 2:30 in the morning at every friday.
/// </summary>
/// <returns></returns>
public Indicator.FridayAlert FridayAlert(Data.IDataSeries input, bool alert1)
{
return _indicator.FridayAlert(input, alert1);
}
}
}

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
public partial class Strategy : StrategyBase
{
/// <summary>
/// It will alert if the current time is equal to 2:30 in the morning at every friday.
/// </summary>
/// <returns></returns>
[Gui.Design.WizardCondition("Indicator")]
public Indicator.FridayAlert FridayAlert(bool alert1)
{
return _indicator.FridayAlert(Input, alert1);
}

/// <summary>
/// It will alert if the current time is equal to 2:30 in the morning at every friday.
/// </summary>
/// <returns></returns>
public Indicator.FridayAlert FridayAlert(Data.IDataSeries input, bool alert1)
{
if (InInitialize && input == null)
throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

return _indicator.FridayAlert(input, alert1);
}
}
}
#endregion

errors:
luxurious_04 is offline  
Reply With Quote
Old 07-29-2010, 07:55 AM   #5
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,379
Thanks: 252
Thanked 966 times in 949 posts
Default

luxurious_04, on what chart and timeframe are you running this script on? Are you sure there's a 2:30 timestamp triggering your condition to raise the Alert?
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 08-03-2010, 09:14 PM   #6
luxurious_04
Senior Member
 
Join Date: Jul 2010
Location: Philippines
Posts: 248
Thanks: 7
Thanked 6 times in 4 posts
Send a message via Skype™ to luxurious_04
Default

10 minutes chart.
luxurious_04 is offline  
Reply With Quote
Old 08-04-2010, 06:08 AM   #7
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,379
Thanks: 252
Thanked 966 times in 949 posts
Default

Ok, have you checked your charts that you indeed get this timestamp in the session range displayed? Do you also have the Alert window open to monitor this alert being triggered? Please also note those will work in realtime or Market Replay only.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 08-11-2010, 10:39 AM   #8
laredo78676
Senior Member
 
Join Date: Jul 2010
Posts: 279
Thanks: 4
Thanked 1 time in 1 post
Default no audio

what do I check,plugged in speakers and nt doesnt announce buys sells etc.
thanks
laredo78676 is offline  
Reply With Quote
Old 08-11-2010, 10:44 AM   #9
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,379
Thanks: 252
Thanked 966 times in 949 posts
Default

Are regular sounds from PC / Windows install working as expected?

Are NinjaTrader's sounds enabled (checked) under Tools > Options > General?

Are you running the latest NT release version 6.5.1000.16?

Thanks
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 08-11-2010, 02:16 PM   #10
laredo78676
Senior Member
 
Join Date: Jul 2010
Posts: 279
Thanks: 4
Thanked 1 time in 1 post
Default nt "16"

after I download "16" nt,how do i sttach it to my existing platform?
laredo78676 is offline  
Reply With Quote
Old 08-11-2010, 02:55 PM   #11
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
Default

laredo, I'm not exactly sure what you mean. NinjaTrader 6.5.1000.16 is just the most current release of NinjaTrader 6.5. You install it, which would then make it your current platform, and then you can import the indicator again and see if that solves the issue.
NinjaTrader_Austin 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
Single Sound Alert DavidHP General Programming 3 09-27-2009 10:39 AM
Sound an alert from C# clam61 Strategy Development 4 04-23-2009 02:13 AM
Alert without sound xewoox General Programming 1 01-06-2009 07:38 AM
sound alert MoreYummy Automated Trading 5 08-26-2008 10:03 AM
TickCounter comes with sound alert? Greenmoney21 Suggestions And Feedback 12 08-07-2008 05:38 AM


All times are GMT -6. The time now is 06:31 AM.