NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 03-24-2011, 09:06 AM   #1
lookOutBelow
Senior Member
 
Join Date: Jul 2010
Posts: 214
Thanks: 0
Thanked 0 times in 0 posts
Default Writing a simple indicator

I've written a bunch of strategies, but I have yet to write an indicator. So, last night I gave it a try. Starting with the wizard, I tried to do something very simple. But it isn't doing what I want. Can someone straighten me out? This has got to be something very simple for you experienced guys.

Code:
#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 EMAWidth2 : Indicator
    {
        #region Variables
        // Wizard generated variables
            private int slowA = 3; // Default setting for SlowA
            private int slowB = 15; // Default setting for SlowB
        // 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.Red), PlotStyle.Line, "Short16"));
            //Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "Short12"));
            Add(new Line(Color.DarkViolet, 0, "Center"));
            CalculateOnBarClose    = true;
            Overlay                = false;
            PriceTypeSupported    = 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 (CurrentBar > 20) 
            {
                
                Short16.Set(EMA(slowA)[0] - EMA(slowB)[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 Short16
        {
            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 Short12
//        {
//            get { return Values[1]; }
//        }

        [Description("")]
        [GridCategory("Parameters")]
        public int SlowA
        {
            get { return slowA; }
            set { slowA = Math.Max(50, value); }
        }

        [Description("")]
        [GridCategory("Parameters")]
        public int SlowB
        {
            get { return slowB; }
            set { slowB = Math.Max(50, 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 EMAWidth2[] cacheEMAWidth2 = null;

        private static EMAWidth2 checkEMAWidth2 = new EMAWidth2();

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public EMAWidth2 EMAWidth2(int slowA, int slowB)
        {
            return EMAWidth2(Input, slowA, slowB);
        }

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public EMAWidth2 EMAWidth2(Data.IDataSeries input, int slowA, int slowB)
        {
            if (cacheEMAWidth2 != null)
                for (int idx = 0; idx < cacheEMAWidth2.Length; idx++)
                    if (cacheEMAWidth2[idx].SlowA == slowA && cacheEMAWidth2[idx].SlowB == slowB && cacheEMAWidth2[idx].EqualsInput(input))
                        return cacheEMAWidth2[idx];

            lock (checkEMAWidth2)
            {
                checkEMAWidth2.SlowA = slowA;
                slowA = checkEMAWidth2.SlowA;
                checkEMAWidth2.SlowB = slowB;
                slowB = checkEMAWidth2.SlowB;

                if (cacheEMAWidth2 != null)
                    for (int idx = 0; idx < cacheEMAWidth2.Length; idx++)
                        if (cacheEMAWidth2[idx].SlowA == slowA && cacheEMAWidth2[idx].SlowB == slowB && cacheEMAWidth2[idx].EqualsInput(input))
                            return cacheEMAWidth2[idx];

                EMAWidth2 indicator = new EMAWidth2();
                indicator.BarsRequired = BarsRequired;
                indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
                indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
                indicator.Input = input;
                indicator.SlowA = slowA;
                indicator.SlowB = slowB;
                Indicators.Add(indicator);
                indicator.SetUp();

                EMAWidth2[] tmp = new EMAWidth2[cacheEMAWidth2 == null ? 1 : cacheEMAWidth2.Length + 1];
                if (cacheEMAWidth2 != null)
                    cacheEMAWidth2.CopyTo(tmp, 0);
                tmp[tmp.Length - 1] = indicator;
                cacheEMAWidth2 = 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.EMAWidth2 EMAWidth2(int slowA, int slowB)
        {
            return _indicator.EMAWidth2(Input, slowA, slowB);
        }

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public Indicator.EMAWidth2 EMAWidth2(Data.IDataSeries input, int slowA, int slowB)
        {
            return _indicator.EMAWidth2(input, slowA, slowB);
        }
    }
}

// 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.EMAWidth2 EMAWidth2(int slowA, int slowB)
        {
            return _indicator.EMAWidth2(Input, slowA, slowB);
        }

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public Indicator.EMAWidth2 EMAWidth2(Data.IDataSeries input, int slowA, int slowB)
        {
            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.EMAWidth2(input, slowA, slowB);
        }
    }
}
#endregion
I expected the indicator would draw a simple plot of the EMA. But instead, it remains at zero. If you replace:

Short16.Set(EMA(slowA)[0] - EMA(slowB)[0]);

with:

Short16.Set(EMA(3)[0] - EMA(15)[0]);

It works fine. But with the slowA and slowB, it doesn't plot as I'd expect. What do I need to insert in there to use the slowA and slowB?
lookOutBelow is offline  
Reply With Quote
Old 03-24-2011, 09:17 AM   #2
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

Hi LookOutBelow,

The issue here is with the Min value you specified when setting this up in the wizard. You used a value of 50, so it's using this instead of the input value. You're then taking EMA(50) - EMA(50) = 0. You can resolve in code with the changes indicated below:


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

[Description("")]
[GridCategory("Parameters")]
public int SlowB
{
get { return slowB; }
set { slowB = Math.Max(1, value); }
}
NinjaTrader_RyanM is offline  
Reply With Quote
Old 03-24-2011, 09:22 AM   #3
lookOutBelow
Senior Member
 
Join Date: Jul 2010
Posts: 214
Thanks: 0
Thanked 0 times in 0 posts
Default

Doh! I knew I overlooked something stupid.

Thanks. I appreciate it.
lookOutBelow 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
Avoiding confilict while writing to text file from indicator? kw123 General Programming 2 12-29-2010 10:36 AM
Writing indicator for MarketAnalyzer - 60min bars and CalculateOnBarClose=false adamus Indicator Development 4 11-18-2010 08:20 AM
Writing an Alert from an Indicator on the Chart itself ??? jmca2000 Indicator Development 11 07-19-2010 01:31 PM
Help writing acceleration indicator markp77 Indicator Development 4 11-04-2008 02:08 AM
Indicator writing to own panel and price panel cherriman Indicator Development 6 10-13-2008 03:14 PM


All times are GMT -6. The time now is 09:15 AM.