• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Design Patterns

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

    Design Patterns

    Hi,
    I'm still getting my feet wet with NT, and I was hoping you could tell me if this is the most efficient way to implement the following.

    1) Many of my indicators use session open gapless series for their calculation. I implemented this by creating an indicator to calculate the gap adjustment:

    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>
        /// Dataseries of session open gap adjustments.  Used in creating gapless indicators.
        /// </summary>
        [Description("Dataseries of session open gap adjustments.  Used in creating gapless indicators.")]
        public class GtGapAdjustment : Indicator
        {
            #region Variables
            // Wizard generated variables
                private double gapPercent = 0; // Default setting for GapPercent
            // 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.Cyan, "GapAdj"));
    
                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 (CurrentBar == 0)
                    Value.Set(0d);
                else if (Bars.FirstBarOfSession && FirstTickOfBar)
                    Value.Set(Value[1] + ( gapPercent * 0.01 * (Open[0] - Close[1])));
                else
                    Value.Set(Value[1]);
    			
            }
    
            #region Properties
    
            [Description("Gap Percent")]
            [GridCategory("Parameters")]
            public double GapPercent
            {
                get { return gapPercent; }
                set { gapPercent = Math.Min(100, Math.Max(0, value)); }
            }
            #endregion
        }
    }
    Then I also created a indicator that has 4 properties for the OHLC adjusted Bars along with the gap adjustment. These are the dataseries I would pass into indicators to calculate their gapless values.

    An example of how I would use it is as follows:
    DataSeries highGapless = GtGaplessBars().HighGapless;
    double tmp = SMA(highGapless, 30)[0] + GtGaplessBars().GapAdjustment[0]);

    Is this the best way to approach it?

    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
    
    namespace NinjaTrader.Indicator
    {
        [Description("Bar series where session open gaps are eliminated.")]
        public class GtGaplessBars : Indicator
        {
            #region Variables
            private double      gapPercent      = 0;
    
            private DataSeries  gapAdjustment;
            private DataSeries  openGapless;
            private DataSeries  highGapless;
            private DataSeries  lowGapless;
            private DataSeries  closeGapless;
            #endregion
    
            protected override void Initialize()
            {
                gapAdjustment = new DataSeries(this);
                openGapless = new DataSeries(this);
                highGapless = new DataSeries(this);
                lowGapless = new DataSeries(this);
                closeGapless = new DataSeries(this);
            }
    
            protected override void OnBarUpdate()
            {
                gapAdjustment.Set(GtGapAdjustment(gapPercent)[0]);
                openGapless.Set(Open[0] - gapAdjustment[0]);
                highGapless.Set(High[0] - gapAdjustment[0]);
                lowGapless.Set(Low[0] - gapAdjustment[0]);
                closeGapless.Set(Close[0] - gapAdjustment[0]);
            }
    
            #region Properties
            [Description("Gap percent (0 = no gap, 100 = unadjusted)")]
            [GridCategory("Parameters")]
            public double GapPercent
            {
                get { return gapPercent; }
                set { gapPercent = Math.Min(100, Math.Max(0, value)); }
            }
    
            [Browsable(false)]
            [XmlIgnore()]
            public DataSeries OpenGapless
            {
                get { return openGapless; }
            }
    
            [Browsable(false)]
            [XmlIgnore()]
            public DataSeries HighGapless
            {
                get { return highGapless; }
            }
    
            [Browsable(false)]
            [XmlIgnore()]
            public DataSeries LowGapless
            {
                get { return lowGapless; }
            }
    
            [Browsable(false)]
            [XmlIgnore()]
            public DataSeries CloseGapless
            {
                get { return closeGapless; }
            }
    
            [Browsable(false)]
            [XmlIgnore()]
            public DataSeries GapAdjustment
            {
                get { return gapAdjustment; }
            }
            #endregion
        }
    }

    #2
    Hello GrumpyTrader,

    While this seems to be a logic oriented issue, the prevailing question is what is the issue you are attempting to resolve?

    Are the gaps you are experiencing from the close of a session to the open of the next?


    I think I am understanding that the first indicator simply returns the gap adjustment amount.

    The second indicator then uses this to apply the adjustment to every bar.

    This could be one indicator and does not need to be two.


    My thoughts are that all of the data that day will be adjusted and I don't see anything wrong with the concept.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,
      I broke out the GtGapAdjustment just for code reuse. For example I might want to create a gap adjusted typical price and wouldn't need the adjusted bars at all.

      I guess I was wondering if creating the GtGapAdjustment as an indicator is the best way. I will never use it as an indicator and plot it on a chart. I only use it as a function that will be called in other indicators (same with the GtGaplessBars indicator). Was wondering if there is a better approach to such general use functions.

      Comment


        #4
        Hello,

        I think an indicator would be the best way to go.

        This could be combined into one indicator though.
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Ivantr, Yesterday, 05:24 PM
        1 response
        10 views
        0 likes
        Last Post NinjaTrader_Marco  
        Started by ralett, Yesterday, 06:20 PM
        3 responses
        27 views
        0 likes
        Last Post ralett
        by ralett
         
        Started by trader252, Yesterday, 08:38 PM
        1 response
        5 views
        0 likes
        Last Post NinjaTrader_ChrisSch  
        Started by grizzlytrader, Yesterday, 06:24 AM
        1 response
        24 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by andrewtrades, Yesterday, 01:43 PM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X