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

Cannot get indicator to plot from 3rd dataseries

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

    Cannot get indicator to plot from 3rd dataseries

    I'm having trouble getting anything to plot for this ATR indicator. Hoping someone can help me figure out what I am doing or not doing to get this result.

    Trying to get ATR to plot from input of 10 minute bars. I've tried with many other different values for the primary data series and it does not seem to impact my results.

    Code below:

    Code:
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    //This namespace holds Strategies in this folder and is required. Do not change it. 
    namespace NinjaTrader.NinjaScript.Strategies
    {
    	public class multiATR : Strategy
    	{
            private ATR _atr;
    
            protected override void OnStateChange()
    		{
    			if (State == State.SetDefaults)
    			{
    				Description									= @"Multi-timeframe Test";
    				Name										= "multiATR";
    				Calculate									= Calculate.OnBarClose;
    				EntriesPerDirection							= 1;
    				EntryHandling								= EntryHandling.AllEntries;
    				IsExitOnSessionCloseStrategy				= true;
    				ExitOnSessionCloseSeconds					= 30;
    				IsFillLimitOnTouch							= false;
    				MaximumBarsLookBack							= MaximumBarsLookBack.Infinite;
    				OrderFillResolution							= OrderFillResolution.Standard;
    				Slippage									= 0;
    				StartBehavior								= StartBehavior.WaitUntilFlat;
    				TimeInForce									= TimeInForce.Gtc;
    				TraceOrders									= false;
    				RealtimeErrorHandling						= RealtimeErrorHandling.StopCancelClose;
    				StopTargetHandling							= StopTargetHandling.PerEntryExecution;
    				BarsRequiredToTrade							= 20;
    				IsInstantiatedOnEachOptimizationIteration	= true;
                    periodATR                                   = 5;
                    minATRrange                                 = 4;
                    InitialCapital                              = 20000;
                }
    			else if (State == State.DataLoaded)
    			{
                    _atr = ATR(BarsArray[2], periodATR);
                    AddChartIndicator(_atr);
    
                    base.ClearOutputWindow();
                }
                else if (State == State.Configure)
                {
                    AddDataSeries(BarsPeriodType.Tick, 1);
                    AddDataSeries(BarsPeriodType.Minute, 10);
                }
            }
    
    		protected override void OnBarUpdate()
    		{
                if ((CurrentBars[0] < 252) || (CurrentBars[2] < periodATR))
                    return;
    
                if (BarsInProgress != 0)
                    return;
    
                if (IsFlat)
                {
                    if (ConditionLongEntry)
                    {
                        EnterLong();
                    }
                    else if (ConditionShortEntry)
                    {
                        EnterShort();
                    }
                }
                else if (IsLong && ConditionLongExit)
                {
                    ExitLong();
                }
                else if (IsShort && ConditionShortExit)
                {
                    ExitShort();
                }
    		}
    
            bool MarketType
            {
                get { return (_atr[0] > minATRrange); }
            }
    
            bool ConditionLongEntry
            {
                get { return true; }
            }
    
            bool ConditionShortEntry
            {
                get { return true; }
            }
    
            bool ConditionLongExit
            {
                get { return true; }
            }
    
            bool ConditionShortExit
            {
                get { return true; }
            }
    
            #region Properties
            [NinjaScriptProperty]
            [Range(0, int.MaxValue)]
            [Display(ResourceType = typeof(Custom.Resource), Name = "periodATR", Description = "ATR Lookback Periods", Order = 1, GroupName = "NinjaScriptStrategyParameters")]
            public int periodATR
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(0, double.MaxValue)]
            [Display(ResourceType = typeof(Custom.Resource), Name = "minATRrange", Description = "Minimum ATR to Trade", Order = 2, GroupName = "NinjaScriptStrategyParameters")]
            public double minATRrange
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(0, double.MaxValue)]
            [Display(ResourceType = typeof(Custom.Resource), Name = "InitialCapital", Order = 3, GroupName = "NinjaScriptStrategyParameters")]
            public double InitialCapital
            { get; set; }
    
            #endregion
    
        }
    }

    #2
    Hello RandyT,

    Thanks for your message.

    The code provided does not compile in its current state. We are able to assist best when other code that is not relevant to the question is removed so we can focus on your inquiry. I've removed the code that does not compile and have created a demonstration showing how an indicator based off of a different data series could be plotted.

    The indicator provided does plot with your code, however, it should be noted that the strategy added data series is not modifying ChartControl to have more slots available for those bars like we would see when we add an additional data series to a chart itself. This will make the indicator plot slightly skewed.

    My recommendation would be to add a plot within the strategy and to set the strategy plot to hold the value of the ATR on 10 minute data.

    I have created a video demonstrating.

    Demo - https://www.screencast.com/t/BFwJmiI1fyDX

    AddPlot() - https://ninjatrader.com/support/help...s/?addplot.htm
    IsOverlay - https://ninjatrader.com/support/help.../isoverlay.htm
    Valid Input for System Indicator methods - https://ninjatrader.com/support/help..._indicator.htm

    Please let us know if you have any additional questions.
    Last edited by NinjaTrader_Jim; 08-02-2018, 10:55 AM.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thanks for all of the effort Jim and apologies for not sanitizing that code. Forgot about some of those helper functions I use.

      Could I trouble you further to post the code you came up with? I don't allow Flash in any environment I work on and therefore cannot watch your video. Too much of a security risk.

      tks

      Comment


        #4
        Hello RandyT,

        Code is attached below. I still highly recommend watching the video in an environment where you do not have these limitations set since the video does go into further detail on how the results appear visually and how the code written works.

        Code:
        protected override void OnStateChange()
        {
        	if (State == State.SetDefaults)
        	{
        		...
        		
        		IsOverlay = false;
        		AddPlot(Brushes.White, "ATRPlot");
                }
        	else if (State == State.DataLoaded)
        	{
                        _atr = ATR(BarsArray[2], periodATR);
                        base.ClearOutputWindow();
                }
                else if (State == State.Configure)
                {
                        AddDataSeries(BarsPeriodType.Tick, 1);
                        AddDataSeries(BarsPeriodType.Minute, 10);
                }
        }
        
        protected override void OnBarUpdate()
        {
        	if (CurrentBars[2] < 1)
        		return;
                if(BarsInProgress == 0)
        	{
        		Print(_atr[0]);
        		Value[0] = _atr[0];
        	}
        }
        JimNinjaTrader Customer Service

        Comment


          #5
          Ok, so it seems the only thing I am missing is the AddPlot(). I assumed the ATR indicator included this as other indicators I am using (but removed) do.

          FWIW, Youtube doesn't require Flash.

          Thanks again. I appreciate your effort.

          Comment


            #6
            Hello RandyT,

            To clarify, AddChartIndicator() will add the indicator to the chart so it is plotted in a normal fashion.

            Since the added indicator uses a different data series that is not added to the chart, the number of bar slots on the chart will not coincide with the added data series and the plot may not appear as you expect.

            This approach advises to add a plot within the hosting NinjaScript, and to assign that plot with the value from the added indicator.

            Adding the plot within the hosting NinjaScript for this value ensures that there is a value plotted for each bar of the primary data series that we apply the NinjaScript to.

            The video will go over this in further detail.

            If you have any additional questions, please let us know.
            JimNinjaTrader Customer Service

            Comment


              #7
              Thanks Jim,

              Finding that AddPlot is not what I am missing.

              I'll keep digging. Sorry but I do not have a computer that I can sacrifice and add Flash. Installing Flash on any computer connected to a network is essentially removing all access control.

              Have a look. Anything in red means full remote control of your computer.

              IBM X-Force Exchange is a threat intelligence sharing platform enabling research on security threats, aggregation of intelligence, and collaboration with peers
              Last edited by RandyT; 08-02-2018, 12:42 PM.

              Comment


                #8
                In case the state of the first code I provided is causing confusion, here is a sanitized version that should compile without other dependencies.

                I'm just trying to get the ATR indicator to plot on a chart. It is not that it is not plotting what I expect. It is not plotting at all.

                Code:
                #region Using declarations
                using System;
                using System.Collections.Generic;
                using System.ComponentModel;
                using System.ComponentModel.DataAnnotations;
                using System.Linq;
                using System.Text;
                using System.Threading.Tasks;
                using System.Windows;
                using System.Windows.Input;
                using System.Windows.Media;
                using System.Xml.Serialization;
                using NinjaTrader.Cbi;
                using NinjaTrader.Gui;
                using NinjaTrader.Gui.Chart;
                using NinjaTrader.Gui.SuperDom;
                using NinjaTrader.Gui.Tools;
                using NinjaTrader.Data;
                using NinjaTrader.NinjaScript;
                using NinjaTrader.Core.FloatingPoint;
                using NinjaTrader.NinjaScript.Indicators;
                using NinjaTrader.NinjaScript.DrawingTools;
                #endregion
                
                //This namespace holds Strategies in this folder and is required. Do not change it. 
                namespace NinjaTrader.NinjaScript.Strategies
                {
                	public class multiATR : Strategy
                	{
                        private ATR _atr;
                
                        protected override void OnStateChange()
                		{
                			if (State == State.SetDefaults)
                			{
                				Description									= @"Multi-timeframe Test";
                				Name										= "multiATR";
                				Calculate									= Calculate.OnBarClose;
                				EntriesPerDirection							= 1;
                				EntryHandling								= EntryHandling.AllEntries;
                				IsExitOnSessionCloseStrategy				= true;
                				ExitOnSessionCloseSeconds					= 30;
                				IsFillLimitOnTouch							= false;
                				MaximumBarsLookBack							= MaximumBarsLookBack.Infinite;
                				OrderFillResolution							= OrderFillResolution.Standard;
                				Slippage									= 0;
                				StartBehavior								= StartBehavior.WaitUntilFlat;
                				TimeInForce									= TimeInForce.Gtc;
                				TraceOrders									= false;
                				RealtimeErrorHandling						= RealtimeErrorHandling.StopCancelClose;
                				StopTargetHandling							= StopTargetHandling.PerEntryExecution;
                				BarsRequiredToTrade							= 20;
                				IsInstantiatedOnEachOptimizationIteration	= true;
                                periodATR                                   = 5;
                                minATRrange                                 = 4;
                                InitialCapital                              = 20000;
                            }
                			else if (State == State.DataLoaded)
                			{
                                _atr = ATR(BarsArray[2], periodATR);
                                AddChartIndicator(_atr);
                
                                base.ClearOutputWindow();
                            }
                            else if (State == State.Configure)
                            {
                                AddDataSeries(BarsPeriodType.Tick, 1);
                                AddDataSeries(BarsPeriodType.Minute, 10);
                            }
                        }
                
                		protected override void OnBarUpdate()
                		{
                            if ((CurrentBars[0] < 252) || (CurrentBars[2] < periodATR))
                                return;
                
                            if (BarsInProgress != 0)
                                return;
                
                            if (Position.MarketPosition == MarketPosition.Flat)
                            {
                                if (ConditionLongEntry)
                                {
                                    EnterLong();
                                }
                                else if (ConditionShortEntry)
                                {
                                    EnterShort();
                                }
                            }
                            else if ((Position.MarketPosition == MarketPosition.Long) && ConditionLongExit)
                            {
                                ExitLong();
                            }
                            else if ((Position.MarketPosition == MarketPosition.Short) && ConditionShortExit)
                            {
                                ExitShort();
                            }
                		}
                
                        bool MarketType
                        {
                            get { return (_atr[0] > minATRrange); }
                        }
                
                        bool ConditionLongEntry
                        {
                            get { return true; }
                        }
                
                        bool ConditionShortEntry
                        {
                            get { return true; }
                        }
                
                        bool ConditionLongExit
                        {
                            get { return true; }
                        }
                
                        bool ConditionShortExit
                        {
                            get { return true; }
                        }
                
                        #region Properties
                        [NinjaScriptProperty]
                        [Range(0, int.MaxValue)]
                        [Display(ResourceType = typeof(Custom.Resource), Name = "periodATR", Description = "ATR Lookback Periods", Order = 1, GroupName = "NinjaScriptStrategyParameters")]
                        public int periodATR
                        { get; set; }
                
                        [NinjaScriptProperty]
                        [Range(0, double.MaxValue)]
                        [Display(ResourceType = typeof(Custom.Resource), Name = "minATRrange", Description = "Minimum ATR to Trade", Order = 2, GroupName = "NinjaScriptStrategyParameters")]
                        public double minATRrange
                        { get; set; }
                
                        [NinjaScriptProperty]
                        [Range(0, double.MaxValue)]
                        [Display(ResourceType = typeof(Custom.Resource), Name = "InitialCapital", Order = 3, GroupName = "NinjaScriptStrategyParameters")]
                        public double InitialCapital
                        { get; set; }
                
                        #endregion
                
                    }
                }

                Comment


                  #9
                  Jim, thanks for sticking with me on this. Your code example was enough to get me over the hump.

                  One last question, is it possible to change the name of the Indicator as it is displayed on the chart? These values are plotted with the name of the Strategy as it is.

                  Thanks again

                  Comment


                    #10
                    Hello RandyT,

                    I'm glad you have been able to add the indicator plot to your hosting NinjaScript in a more meaningful way.

                    Changing the way a NinjaScripts name gets displayed can be done by overriding the DisplayName property. I've included documentation reference below.

                    DisplayName - https://ninjatrader.com/support/help...isplayname.htm

                    Please let us know if we can be of further assistance.
                    JimNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by frankthearm, Today, 09:08 AM
                    7 responses
                    27 views
                    0 likes
                    Last Post NinjaTrader_Clayton  
                    Started by NRITV, Today, 01:15 PM
                    1 response
                    5 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Started by maybeimnotrader, Yesterday, 05:46 PM
                    5 responses
                    24 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by quantismo, Yesterday, 05:13 PM
                    2 responses
                    16 views
                    0 likes
                    Last Post quantismo  
                    Started by adeelshahzad, Today, 03:54 AM
                    5 responses
                    33 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Working...
                    X