Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

How to make this work? SOUND ALERT

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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

    #2
    Hey Fredyyy,
    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).

    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.

    Comment


      #3
      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....

      Comment


        #4
        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:

        Comment


          #5
          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?
          BertrandNinjaTrader Customer Service

          Comment


            #6
            10 minutes chart.

            Comment


              #7
              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.
              BertrandNinjaTrader Customer Service

              Comment


                #8
                no audio

                what do I check,plugged in speakers and nt doesnt announce buys sells etc.
                thanks

                Comment


                  #9
                  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
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    nt &quot;16&quot;

                    after I download "16" nt,how do i sttach it to my existing platform?

                    Comment


                      #11
                      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.
                      AustinNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by mgco4you, Today, 09:46 PM
                      1 response
                      3 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by wzgy0920, Today, 09:53 PM
                      0 responses
                      3 views
                      0 likes
                      Last Post wzgy0920  
                      Started by Rapine Heihei, Today, 08:19 PM
                      1 response
                      8 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by Rapine Heihei, Today, 08:25 PM
                      0 responses
                      6 views
                      0 likes
                      Last Post Rapine Heihei  
                      Started by f.saeidi, Today, 08:01 PM
                      1 response
                      9 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Working...
                      X