![]() |
|
|||||||
| General Programming General NinjaScript programming questions. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Junior Member
Join Date: May 2009
Posts: 18
Thanks: 0
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#2 | ||
|
Senior Member
Join Date: Aug 2008
Location: Netherlands
Posts: 159
Thanks: 17
Thanked 5 times in 5 posts
|
Hey Fredyyy,
Quote:
Quote:
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); 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);
}
Good luck and I hope my reaction is of some use to you.
|
||
|
|
|
|
|
#3 |
|
Junior Member
Join Date: May 2009
Posts: 18
Thanks: 0
Thanked 0 times in 0 posts
|
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.... |
|
|
|
|
|
#4 |
|
Senior Member
|
#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: |
|
|
|
|
|
#5 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,379
Thanks: 252
Thanked 966 times in 949 posts
|
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?
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#6 |
|
Senior Member
|
10 minutes chart.
|
|
|
|
|
|
#7 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,379
Thanks: 252
Thanked 966 times in 949 posts
|
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.
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#8 |
|
Senior Member
Join Date: Jul 2010
Posts: 279
Thanks: 4
Thanked 1 time in 1 post
|
what do I check,plugged in speakers and nt doesnt announce buys sells etc.
thanks |
|
|
|
|
|
#9 |
|
NinjaTrader Customer Service
Join Date: Sep 2008
Location: Germany
Posts: 22,379
Thanks: 252
Thanked 966 times in 949 posts
|
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
Bertrand
NinjaTrader Customer Service |
|
|
|
|
|
#10 |
|
Senior Member
Join Date: Jul 2010
Posts: 279
Thanks: 4
Thanked 1 time in 1 post
|
after I download "16" nt,how do i sttach it to my existing platform?
|
|
|
|
|
|
#11 |
|
NinjaTrader Customer Service
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
|
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.
Austin
NinjaTrader Customer Service |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |