Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CCI w/ Audible Alert

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

    CCI w/ Audible Alert

    I'm sorry if this has already been covered, but I am trying to create a custom indicator for the CCI that alerts when price crosses above/below +/-180. I am not an expert at programming, but I tried adjusting some previous code for a crossalert that I found here and this is what I came up with. The problem is that when I go to compile it, it gives an error stating that "The namespace 'NinjaTrader.Indicator' already contains a definition for 'CrossAlert'. If anyone could help me refine this, I'd GREATLY appreciate it! Here is my code:

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

    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    /// <summary>
    ///
    /// </summary>
    [Description("")]
    [Gui.Design.DisplayName("")]
    public class CCIAlerts : Indicator
    {
    #region Variables
    // Wizard generated variables
    private int fast = 5; // Default setting for Fast
    private int slow = 15; // Default setting for Slow
    // 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()
    {
    Period = 45;
    Slow = 15;
    CalculateOnBarClose = false;
    Overlay = true;
    PriceTypeSupported = false;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (CrossAbove(CCI(Period), 180))
    {
    // Mark the cross above bar with a green dot
    DrawDot(Time[0].ToString(), 0, High[0] + TickSize, Color.Green);

    // Only generate an alert on real-time data
    if (!Historical)
    Alert(Time[0].ToString(), NinjaTrader.Cbi.Priority.Medium, "Cross Above", Cbi.Core.InstallDir + @"\sounds\Alert4.wav", 0, Color.Black, Color.Yellow);
    }
    else if (CrossBelow(CCI(Period), -180))
    {
    // Mark the cross below bar with a red dot
    DrawDot(Time[0].ToString(), 0, Low[0] - TickSize, Color.Red);

    // Only generate an alert on real-time data
    if (!Historical)
    Alert(Time[0].ToString(), NinjaTrader.Cbi.Priority.Medium, "Cross Below", Cbi.Core.InstallDir + @"\sounds\Alert4.wav", 0, Color.Yellow, Color.Black);
    }
    }

    #region Properties

    [Description("Fast period")]
    [Category("Parameters")]
    public int Fast
    {
    get { return fast; }
    set { fast = Math.Max(1, value); }
    }

    [Description("Slow period")]
    [Category("Parameters")]
    public int Slow
    {
    get { return slow; }
    set { slow = Math.Max(1, 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 CCIAlerts[] cacheCCIAlerts = null;

    private static CCIAlerts checkCCIAlerts = new CCIAlerts();

    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    public CCIAlerts CCIAlerts(int fast, int slow)
    {
    return CCIAlerts(Input, fast, slow);
    }

    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    public CCIAlerts CCIAlerts(Data.IDataSeries input, int fast, int slow)
    {
    checkCCIAlerts.Fast = fast;
    fast = checkCCIAlerts.Fast;
    checkCCIAlerts.Slow = slow;
    slow = checkCCIAlerts.Slow;

    if (cacheCCIAlerts != null)
    for (int idx = 0; idx < cacheCCIAlerts.Length; idx++)
    if (cacheCCIAlerts[idx].Fast == fast && cacheCCIAlerts[idx].Slow == slow && cacheCCIAlerts[idx].EqualsInput(input))
    return cacheCCIAlerts[idx];

    CCIAlerts indicator = new CCIAlerts();
    indicator.SetUp();
    indicator.CalculateOnBarClose = CalculateOnBarClose;
    indicator.Input = input;
    indicator.Fast = fast;
    indicator.Slow = slow;

    CCIAlerts[] tmp = new CCIAlerts[cacheCCIAlerts == null ? 1 : cacheCCIAlerts.Length + 1];
    if (cacheCCIAlerts != null)
    cacheCCIAlerts.CopyTo(tmp, 0);
    tmp[tmp.Length - 1] = indicator;
    cacheCCIAlerts = tmp;
    Indicators.Add(indicator);

    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>
    ///
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.CCIAlerts CCIAlerts(int fast, int slow)
    {
    return _indicator.CCIAlerts(Input, fast, slow);
    }

    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    public Indicator.CCIAlerts CCIAlerts(Data.IDataSeries input, int fast, int slow)
    {
    return _indicator.CCIAlerts(input, fast, slow);
    }

    }
    }

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    public partial class Strategy : StrategyBase
    {
    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.CCIAlerts CCIAlerts(int fast, int slow)
    {
    return _indicator.CCIAlerts(Input, fast, slow);
    }

    /// <summary>
    ///
    /// </summary>
    /// <returns></returns>
    public Indicator.CCIAlerts CCIAlerts(Data.IDataSeries input, int fast, int slow)
    {
    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.CCIAlerts(input, fast, slow);
    }

    }
    }
    #endregion

    #2
    You do have multiple implementations of the CrossAlert indiator. Just remove the superfluous ones and you will be good.
    DierkNinjaTrader Customer Service

    Comment


      #3
      I made several attempts with no luck All I need is a SIMPLE indicator that gives an audible alert when cci crosses +/- 180... I can't seem to get anything to compile without errors. It would be awesome if someone could help me with the code...

      Comment


        #4
        Just to double check: Before starting your efforts you need to make sure you NinjaScript files compile. Please verify by removing the erroneous file.
        DierkNinjaTrader Customer Service

        Comment


          #5
          Okay, so I managed to put together some code that compiles correctly. The code seems to be working - it triggers a sound when the CCI crosses above or below +/- 180. The problem now is getting it to stop repeating the sound repeatedly during the bar that it triggers. I tried inserting the "ResetAlerts" method but that didn't seem to do anything. Is there a way to set this so that the sound only goes off once and then resets until the next time it crosses above or below? Thanks.
          protected override void Initialize()
          {
          CalculateOnBarClose = false;
          PriceTypeSupported = true;
          }

          /// <summary>
          /// Calculates the indicator value(s) at the current index.
          /// </summary>
          protected override void OnBarUpdate()
          {
          // Condition set 1
          if (CrossAbove(CCI(45), 180, 1))
          {
          Alert("Short Signal", NinjaTrader.Cbi.Priority.High, "Short Signal", @"D:\NinjaTrader 6\sounds\Alert4.wav", 10, Color.Black, Color.Yellow);
          ResetAlert("reset");
          }

          // Condition set 2
          if (CrossBelow(CCI(45), -180, 1))
          {
          Alert("Long Signal", NinjaTrader.Cbi.Priority.High, "Long Signal", @"D:\NinjaTrader 6\sounds\Alert4.wav", 10, Color.Black, Color.Yellow);
          ResetAlert("reset");
          }
          }

          Comment


            #6
            Unfortunately we are unable to provide support down to a level of actually coding or revising a strategy. Your might consider contacting a NinjaTrader consultant: http://www.ninjatrader.com/webnew/pa...injaScript.htm

            Also: You might consider contacting this user who appears to deal with a similar issue: http://www.ninjatrader-support.com/v...ght=consultant
            DierkNinjaTrader Customer Service

            Comment


              #7
              You will have to do some coding where you use variables to set flags when or when not to trigger the alerts.
              RayNinjaTrader Customer Service

              Comment


                #8
                Couldn't this work the same way as "rearming"? I had it set to 10 before but it repeatedly sounded the alert... I feel like this should be something very simple that I'm overlooking...

                Comment


                  #9
                  Some hints: By the looks of it there are several flaws in the code below:
                  - ResetAlerts("reset") resets and alert for ID "reset", but your code never sets such an alert
                  - why would you want to the reset and alert right after you submitted an alert?

                  Your might consider contacting a NinjaTrader consultant who could help you working on your actual strategy: http://www.ninjatrader.com/webnew/pa...injaScript.htm

                  Due to bandwidth reason we are unfortunately unable to provide support down to that level.

                  Thanks for your understanding.
                  DierkNinjaTrader Customer Service

                  Comment


                    #10
                    Re-arm means that the alert will not sound even if you call the method for the duration of the re-arm time.
                    RayNinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Ray View Post
                      Re-arm means that the alert will not sound even if you call the method for the duration of the re-arm time.
                      That would actually be totally fine, as once I get a signal, I don't expect another one for several minutes. So, just to clarify, if I set the rearm time in the alert to 120 seconds, the signal will go off ONCE and then not look for or sound another alert until 120 seconds after the first signal, correct?? If so, that would be great...

                      Comment


                        #12
                        That is correct.
                        RayNinjaTrader Customer Service

                        Comment


                          #13
                          Thanks for your help, Ray!

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by molecool, Today, 11:26 AM
                          0 responses
                          1 view
                          0 likes
                          Last Post molecool  
                          Started by molecool, Today, 11:23 AM
                          0 responses
                          2 views
                          0 likes
                          Last Post molecool  
                          Started by ballboy11, 01-09-2019, 08:40 AM
                          2 responses
                          325 views
                          0 likes
                          Last Post molecool  
                          Started by 12VMan, Today, 11:07 AM
                          1 response
                          10 views
                          0 likes
                          Last Post NinjaTrader_Eleanore  
                          Started by Bmatarese, Today, 11:01 AM
                          1 response
                          6 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Working...
                          X