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

Help!

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

    Help!

    I have made a couple of indicatorsin the past and this morning I can't get the simple code below to print. Please help..I have tested by removing the conditon and it will plot the MA but as soon as I addthe logic, it will not print. I have never had a problem like this before

    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    [Description("Enter the description of your new custom indicator here")]
    public class ITG : Indicator
    {
    #region Variables
    // Wizard generated variables
    private int ma1 = 3; // Default setting for Ma1

    // 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.Black), PlotStyle.Line, "Plot0"));
    Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "Plot1"));
    Add(new Plot(Color.FromKnownColor(KnownColor.DarkViolet), PlotStyle.Line, "Plot2"));
    Add(new Plot(Color.FromKnownColor(KnownColor.Firebrick), PlotStyle.Line, "Plot3"));
    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.
    if (SMA(3)[0]>SMA(3)[1])
    {
    Plot0.Set(1);
    }
    else
    {
    Plot0.Set(0);
    }

    }

    #2
    Hello jngrim,
    Thanks for your note.

    To assist you further can you please post the actual cs file. You will find the cs file in the folder ~\Documents\NinjaTrader 7\bin\Custom\Indicator\
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      ... tested by removing the conditon and it will plot the MA...
      What condition? I do not see any condition in the code that you posted.

      However, just looking at the code that you did post, you should probably have an "index out of range" exception in your log.

      You will need to validate the existence of the bars that you are accessing.

      Reference this post: http://www.ninjatrader.com/support/f...91077#poststop in this thread: http://www.ninjatrader.com/support/f...91066#poststop

      Comment


        #4
        See attached..thanks


        #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>
        /// Enter the description of your new custom indicator here
        /// </summary>
        [Description("Enter the description of your new custom indicator here")]
        public class ITG : Indicator
        {
        #region Variables
        // Wizard generated variables
        private int ma1 = 3; // Default setting for Ma1
        private int ma2 = 6; // Default setting for Ma2
        private int ma3 = 12; // Default setting for Ma3
        private int ma4 = 24; // Default setting for Ma4
        // 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.Black), PlotStyle.Line, "Plot0"));
        Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "Plot1"));
        Add(new Plot(Color.FromKnownColor(KnownColor.DarkViolet), PlotStyle.Line, "Plot2"));
        Add(new Plot(Color.FromKnownColor(KnownColor.Firebrick), PlotStyle.Line, "Plot3"));
        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.
        if (SMA(3)[0]>SMA(3)[1])
        {
        Plot0.Set(1);
        }
        else
        {
        Plot0.Set(0);
        }
        //Plot1.Set(SMA(Ma1)[0]>=SMA(Ma1)[1] ? 1:0);
        //Plot2.Set(SMA(Ma3)[0]);
        //Plot3.Set(SMA(Ma4)[0]);
        }

        #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]; }
        }

        [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 Plot1
        {
        get { return Values[1]; }
        }

        [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 Plot2
        {
        get { return Values[2]; }
        }

        [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 Plot3
        {
        get { return Values[3]; }
        }

        [Description("")]
        [GridCategory("Parameters")]
        public int Ma1
        {
        get { return ma1; }
        set { ma1 = Math.Max(1, value); }
        }

        [Description("")]
        [GridCategory("Parameters")]
        public int Ma2
        {
        get { return ma2; }
        set { ma2 = Math.Max(1, value); }
        }

        [Description("")]
        [GridCategory("Parameters")]
        public int Ma3
        {
        get { return ma3; }
        set { ma3 = Math.Max(1, value); }
        }

        [Description("")]
        [GridCategory("Parameters")]
        public int Ma4
        {
        get { return ma4; }
        set { ma4 = 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 ITG[] cacheITG = null;

        private static ITG checkITG = new ITG();

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public ITG ITG(int ma1, int ma2, int ma3, int ma4)
        {
        return ITG(Input, ma1, ma2, ma3, ma4);
        }

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public ITG ITG(Data.IDataSeries input, int ma1, int ma2, int ma3, int ma4)
        {
        if (cacheITG != null)
        for (int idx = 0; idx < cacheITG.Length; idx++)
        if (cacheITG[idx].Ma1 == ma1 && cacheITG[idx].Ma2 == ma2 && cacheITG[idx].Ma3 == ma3 && cacheITG[idx].Ma4 == ma4 && cacheITG[idx].EqualsInput(input))
        return cacheITG[idx];

        lock (checkITG)
        {
        checkITG.Ma1 = ma1;
        ma1 = checkITG.Ma1;
        checkITG.Ma2 = ma2;
        ma2 = checkITG.Ma2;
        checkITG.Ma3 = ma3;
        ma3 = checkITG.Ma3;
        checkITG.Ma4 = ma4;
        ma4 = checkITG.Ma4;

        if (cacheITG != null)
        for (int idx = 0; idx < cacheITG.Length; idx++)
        if (cacheITG[idx].Ma1 == ma1 && cacheITG[idx].Ma2 == ma2 && cacheITG[idx].Ma3 == ma3 && cacheITG[idx].Ma4 == ma4 && cacheITG[idx].EqualsInput(input))
        return cacheITG[idx];

        ITG indicator = new ITG();
        indicator.BarsRequired = BarsRequired;
        indicator.CalculateOnBarClose = CalculateOnBarClose;
        #if NT7
        indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
        indicator.MaximumBarsLookBack = MaximumBarsLookBack;
        #endif
        indicator.Input = input;
        indicator.Ma1 = ma1;
        indicator.Ma2 = ma2;
        indicator.Ma3 = ma3;
        indicator.Ma4 = ma4;
        Indicators.Add(indicator);
        indicator.SetUp();

        ITG[] tmp = new ITG[cacheITG == null ? 1 : cacheITG.Length + 1];
        if (cacheITG != null)
        cacheITG.CopyTo(tmp, 0);
        tmp[tmp.Length - 1] = indicator;
        cacheITG = 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>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        [Gui.Design.WizardCondition("Indicator")]
        public Indicator.ITG ITG(int ma1, int ma2, int ma3, int ma4)
        {
        return _indicator.ITG(Input, ma1, ma2, ma3, ma4);
        }

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public Indicator.ITG ITG(Data.IDataSeries input, int ma1, int ma2, int ma3, int ma4)
        {
        return _indicator.ITG(input, ma1, ma2, ma3, ma4);
        }
        }
        }

        // This namespace holds all strategies and is required. Do not change it.
        namespace NinjaTrader.Strategy
        {
        public partial class Strategy : StrategyBase
        {
        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        [Gui.Design.WizardCondition("Indicator")]
        public Indicator.ITG ITG(int ma1, int ma2, int ma3, int ma4)
        {
        return _indicator.ITG(Input, ma1, ma2, ma3, ma4);
        }

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public Indicator.ITG ITG(Data.IDataSeries input, int ma1, int ma2, int ma3, int ma4)
        {
        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.ITG(input, ma1, ma2, ma3, ma4);
        }
        }
        }
        #endregion

        Comment


          #5
          Please identify the single line with the condition that you are talking about.

          Comment


            #6
            Please try the following in your OnBarUpdate section of the code which i have made bold

            Code:
            protected override void OnBarUpdate()
            {
            [B]if(CurrentBar < 1)
            return;
            [/B]
            if (SMA(3)[0]>SMA(3)[1])
            {
            Plot0.Set(1);
            }
            else 
            {
            Plot0.Set(0);
            }
            //Plot1.Set(SMA(Ma1)[0]>=SMA(Ma1)[1] ? 1:0);
            //Plot2.Set(SMA(Ma3)[0]);
            //Plot3.Set(SMA(Ma4)[0]);
            }
            MatthewNinjaTrader Product Management

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by arvidvanstaey, Today, 02:19 PM
            2 responses
            6 views
            0 likes
            Last Post arvidvanstaey  
            Started by jordanq2, Today, 03:10 PM
            0 responses
            2 views
            0 likes
            Last Post jordanq2  
            Started by traderqz, Today, 12:06 AM
            10 responses
            18 views
            0 likes
            Last Post traderqz  
            Started by algospoke, 04-17-2024, 06:40 PM
            5 responses
            46 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by mmckinnm, Today, 01:34 PM
            3 responses
            5 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Working...
            X