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

Strange values from marketdepth indicator when called from strategy

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

    Strange values from marketdepth indicator when called from strategy

    Hi, I have some problems with an indicator and a strategy that uses this indicator. Code as follows:
    Indicator
    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 MarketDepthIndicator : Indicator
        {
            #region Variables
            // Wizard generated variables
                private int myInput0 = 1; // Default setting for MyInput0
            // 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, "LastAskVolume"));
                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.
                //LastAskVolume.Set(Close[0]);
            }
            
            protected override void OnMarketDepth(MarketDepthEventArgs e)
            {    
            if (e.MarketDataType == MarketDataType.Ask && (e.Operation == Operation.Update || e.Operation == Operation.Insert)) {
             //Print(e.Operation + " - The most recent ask Price= " + e.Price + " Volume= " + e.Volume);
                LastAskVolume.Set((double)e.Volume);
            }
            }
            #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 LastAskVolume
            {
                get { return Values[0]; }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public int MyInput0
            {
                get { return myInput0; }
                set { myInput0 = 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 MarketDepthIndicator[] cacheMarketDepthIndicator = null;
    
            private static MarketDepthIndicator checkMarketDepthIndicator = new MarketDepthIndicator();
    
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            public MarketDepthIndicator MarketDepthIndicator(int myInput0)
            {
                return MarketDepthIndicator(Input, myInput0);
            }
    
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            public MarketDepthIndicator MarketDepthIndicator(Data.IDataSeries input, int myInput0)
            {
                if (cacheMarketDepthIndicator != null)
                    for (int idx = 0; idx < cacheMarketDepthIndicator.Length; idx++)
                        if (cacheMarketDepthIndicator[idx].MyInput0 == myInput0 && cacheMarketDepthIndicator[idx].EqualsInput(input))
                            return cacheMarketDepthIndicator[idx];
    
                lock (checkMarketDepthIndicator)
                {
                    checkMarketDepthIndicator.MyInput0 = myInput0;
                    myInput0 = checkMarketDepthIndicator.MyInput0;
    
                    if (cacheMarketDepthIndicator != null)
                        for (int idx = 0; idx < cacheMarketDepthIndicator.Length; idx++)
                            if (cacheMarketDepthIndicator[idx].MyInput0 == myInput0 && cacheMarketDepthIndicator[idx].EqualsInput(input))
                                return cacheMarketDepthIndicator[idx];
    
                    MarketDepthIndicator indicator = new MarketDepthIndicator();
                    indicator.BarsRequired = BarsRequired;
                    indicator.CalculateOnBarClose = CalculateOnBarClose;
    #if NT7
                    indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                    indicator.MaximumBarsLookBack = MaximumBarsLookBack;
    #endif
                    indicator.Input = input;
                    indicator.MyInput0 = myInput0;
                    Indicators.Add(indicator);
                    indicator.SetUp();
    
                    MarketDepthIndicator[] tmp = new MarketDepthIndicator[cacheMarketDepthIndicator == null ? 1 : cacheMarketDepthIndicator.Length + 1];
                    if (cacheMarketDepthIndicator != null)
                        cacheMarketDepthIndicator.CopyTo(tmp, 0);
                    tmp[tmp.Length - 1] = indicator;
                    cacheMarketDepthIndicator = 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.MarketDepthIndicator MarketDepthIndicator(int myInput0)
            {
                return _indicator.MarketDepthIndicator(Input, myInput0);
            }
    
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            public Indicator.MarketDepthIndicator MarketDepthIndicator(Data.IDataSeries input, int myInput0)
            {
                return _indicator.MarketDepthIndicator(input, myInput0);
            }
        }
    }
    
    // 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.MarketDepthIndicator MarketDepthIndicator(int myInput0)
            {
                return _indicator.MarketDepthIndicator(Input, myInput0);
            }
    
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            public Indicator.MarketDepthIndicator MarketDepthIndicator(Data.IDataSeries input, int myInput0)
            {
                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.MarketDepthIndicator(input, myInput0);
            }
        }
    }
    #endregion
    Strategy
    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.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Enter the description of your strategy here
        /// </summary>
        [Description("Enter the description of your strategy here")]
        public class MarketDepthStrategy : Strategy
        {
            #region Variables
            // Wizard generated variables
            private int myInput0 = 1; // Default setting for MyInput0
            // User defined variables (add any user defined variables below)
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
            }
            protected override void OnMarketDepth(MarketDepthEventArgs e)
            { 
                DataSeries askVol = MarketDepthIndicator(1).LastAskVolume;
                if(askVol[0]>1000) {
                Print("MarketDepthStrategy - Enter Long Limit, askVol[0]= "+askVol[0]);
                EnterLongLimit(DefaultQuantity, GetCurrentAsk(), "");
                }
            }
            #region Properties
            [Description("")]
            [GridCategory("Parameters")]
            public int MyInput0
            {
                get { return myInput0; }
                set { myInput0 = Math.Max(1, value); }
            }
            #endregion
        }
    }
    Console output
    Code:
    MarketDepthStrategy - Enter Long Limit, askVol[0]= 1560,75
    The indicator plots the accurate values when using it in a chart (the latest changed ask volume) but as one can see it returns what looks like a price when called from a strategy. Any help is greatly appreciated.

    #2
    Hi Ntapabepa,

    Thanks for posting.

    From your code it looks like that the strategy may be pulling a Dummy Value for the askVol[0] when checking the other indicator. In other words you the indicators plots may be undefined for any bar, and the input typically returns the close price instead.

    You may need to ensure the indicator plots are valid before programmatically accessing them. Attached below is a reference sample on how to code and check for your plots values.
    http://www.ninjatrader.com/support/f...ad.php?t=33061

    Let me know if I can be of further assistance
    Cal H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by mishhh, 05-25-2010, 08:54 AM
    19 responses
    6,189 views
    0 likes
    Last Post rene69851  
    Started by gwenael, Today, 09:29 AM
    0 responses
    4 views
    0 likes
    Last Post gwenael
    by gwenael
     
    Started by Karado58, 11-26-2012, 02:57 PM
    8 responses
    14,830 views
    0 likes
    Last Post Option Whisperer  
    Started by Option Whisperer, Today, 09:05 AM
    0 responses
    2 views
    0 likes
    Last Post Option Whisperer  
    Started by cre8able, Yesterday, 01:16 PM
    3 responses
    11 views
    0 likes
    Last Post cre8able  
    Working...
    X