NinjaTrader Support Forum  

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 02-10-2012, 11:08 AM   #1
Europetrader
Junior Member
 
Join Date: Dec 2011
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
Default Calculculating spread between two SMA's

Im new to programming, and struggle to figure out the coding for this verry simple value.

I want to get a indicator reading of the spread between a fast and a slow moving average. (FastSMA - SlowSMA).

I realize this probably is a verry simple code, therfore I hope someone can point me in the right direction?
Europetrader is offline  
Reply With Quote
Old 02-10-2012, 12:10 PM   #2
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

Hello Europetrader,
Thanks for your post and I am happy to assist you.
Please use this code
Code:
double diff = SMA(13)[0] - SMA(34)[0];
Please let me know if I can assist you any further.
NinjaTrader_Joydeep is offline  
Reply With Quote
Old 02-11-2012, 08:42 AM   #3
Europetrader
Junior Member
 
Join Date: Dec 2011
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
Default

Thank you. I managed to render the code without getting any errors, but when I try it out in the chart it is blank (and the log doesnt show any errors)? I used the wizard to generate the SMA variables as I want to be able to change the averages easy.

I have copied the code below. Appreciate it if you can take a look?

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 TAZ : Indicator
    {
        #region Variables
        // Wizard generated variables
            private int fastMA = 10; // Default setting for FastMA
            private int slowMA = 40; // Default setting for SlowMA
        // 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.
            Double diff = (SMA(FastMA)[0] - SMA(SlowMA)[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]; }
        }

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

        [Description("ma40")]
        [GridCategory("Parameters")]
        public int SlowMA
        {
            get { return slowMA; }
            set { slowMA = 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 TAZ[] cacheTAZ = null;

        private static TAZ checkTAZ = new TAZ();

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

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public TAZ TAZ(Data.IDataSeries input, int fastMA, int slowMA)
        {
            if (cacheTAZ != null)
                for (int idx = 0; idx < cacheTAZ.Length; idx++)
                    if (cacheTAZ[idx].FastMA == fastMA && cacheTAZ[idx].SlowMA == slowMA && cacheTAZ[idx].EqualsInput(input))
                        return cacheTAZ[idx];

            lock (checkTAZ)
            {
                checkTAZ.FastMA = fastMA;
                fastMA = checkTAZ.FastMA;
                checkTAZ.SlowMA = slowMA;
                slowMA = checkTAZ.SlowMA;

                if (cacheTAZ != null)
                    for (int idx = 0; idx < cacheTAZ.Length; idx++)
                        if (cacheTAZ[idx].FastMA == fastMA && cacheTAZ[idx].SlowMA == slowMA && cacheTAZ[idx].EqualsInput(input))
                            return cacheTAZ[idx];

                TAZ indicator = new TAZ();
                indicator.BarsRequired = BarsRequired;
                indicator.CalculateOnBarClose = CalculateOnBarClose;
#if NT7
                indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                indicator.MaximumBarsLookBack = MaximumBarsLookBack;
#endif
                indicator.Input = input;
                indicator.FastMA = fastMA;
                indicator.SlowMA = slowMA;
                Indicators.Add(indicator);
                indicator.SetUp();

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

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

// 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.TAZ TAZ(int fastMA, int slowMA)
        {
            return _indicator.TAZ(Input, fastMA, slowMA);
        }

        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        /// <returns></returns>
        public Indicator.TAZ TAZ(Data.IDataSeries input, int fastMA, int slowMA)
        {
            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.TAZ(input, fastMA, slowMA);
        }
    }
}
#endregion
Europetrader is offline  
Reply With Quote
Old 02-11-2012, 06:15 PM   #4
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,356
Thanks: 24
Thanked 1,304 times in 1,067 posts
Send a message via Skype™ to koganam
Default

This is your current code that handles the OnBarUpdate() event:
Code:
        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.
            Double diff = (SMA(FastMA)[0] - SMA(SlowMA)[0]);
        }
It assigns your desired output to a double variable and does nothing else. To show it as a Plot, you need to set the Plot to the required value.

Code:
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.
            Double diff = (SMA(FastMA)[0] - SMA(SlowMA)[0]);
            Plot0.Set(diff);
        }
I would suggest that you probably want to work through the tutorials in the Help documentation. That way, you will better understand the structure and flow of coding NT indicators and strategies.

I assure you that the piecemeal approach of trial-and-error, and asking specific questions and getting the resultant narrowly tailored answer, such as the one I give here, is much the slower process of trying to understand what is going on.
koganam is offline  
Reply With Quote
Old 02-12-2012, 11:44 AM   #5
Europetrader
Junior Member
 
Join Date: Dec 2011
Posts: 7
Thanks: 0
Thanked 0 times in 0 posts
Default

Thanks alot for the help. I will
Europetrader is offline  
Reply With Quote
Old 02-13-2012, 07:00 AM   #6
NinjaTrader_Joydeep
NinjaTrader Customer Service
 
NinjaTrader_Joydeep's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 3,286
Thanks: 580
Thanked 546 times in 541 posts
Default

Hello Eutopetrader,
Please go through the Plot class to get more idea on how NinjaTrader draws a data series http://www.ninjatrader.com/support/h...plot_class.htm

@ koganam - Thanks for the debug.
NinjaTrader_Joydeep is offline  
Reply With Quote
Reply

Tags
sma, spread

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
Plot SMA's tacticaltrader Indicator Development 3 09-19-2011 11:48 PM
spread werido Suggestions And Feedback 1 10-29-2009 09:12 AM
Spread rastotrader Automated Trading 5 10-21-2009 12:30 PM
spread trading SuperDriveGuy SuperDOM and other Order Entry Windows 4 07-31-2009 07:36 AM
The Spread beastment Strategy Analyzer 2 01-28-2009 07:25 AM


All times are GMT -6. The time now is 03:38 PM.