• 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

NwMA -- IFTA 2012 -- Mov Avg 3.0

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

    NwMA -- IFTA 2012 -- Mov Avg 3.0

    Hi

    can anyone check the issue in this coding?

    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>
        /// IFTA2012 Moving Averages 3.0 by Manfred G. Dürschner
        /// </summary>
        [Description("IFTA2012 Moving Averages 3.0 by Manfred G. Dürschner")]
        public class nwMAMovAvg30alpha : Indicator
        {
            #region NWMA UTIl 
            
            public IDataSeries NWMA(IDataSeries input, int cycle_period_n1, int cycle_period_n2,double alpha)
            {
                NMA =new DataSeries(this);
                MA1 =new DataSeries(this);
                MA2 =new DataSeries(this);
                //PRICE =new DataSeries(this);//MA2[MA1/ n2]
                
                /*error codes */
                
                //PRICE.Set(input[0]);
                MA1.Set(WMA(input, cycle_period_n1)[0]);
                MA2.Set(WMA(MA1,cycle_period_n2)[0]);
                NMA.Set((1 +alpha)* MA1[0] -( alpha * MA2[0]));
                return NMA ;
            }
            #endregion // NWMA Calculations
            
            
            #region Variables
            // copyright stuff
            private const string        _versionText        = "V0.1Beta";
            private    const string         _copyrightText    = "All rights reserved to nTuple And KKS.";
            // Wizard generated variables
                private int cycle_Period_n1 = 21; // Default setting for Cycle_Period_n1
                private int cycle_Period_n2 = 5; // Default setting for Cycle_Period_n2
            private DataSeries NMA;
            private DataSeries MA1;
            private DataSeries MA2;
            private DataSeries PRICE;
            // 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, "NwMA"));
                Overlay                = true;
                //Initializeparam();
            }
    
            /// <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.
                
                            /* α := L1/L2 = (n1 – 1)/(n2 – 1).
    In this expression denominator 2 for the SMA and EMA as
    well as denominator 3 for the WMA are missing. α is therefore
    valid for all three MAs. Using the Nyquist Criterion one gets for
    α the following result:
    (2) α = λ* (n1 – 1)/(n1 – λ).
    α put in (1) and C replaced by the approximation term NMA,
    the notation for the new MA, one gets:
    NMA = (1 +α) MA1 – α MA2.
    In detail, equation (2) reads as follows:
    (3) NMA[price/ n1, n2] = (1 + α) MA1[price/ n1 ] – α
    MA2[MA1/ n2],
    (4) α = λ* (n1 – 1)/(n1 – λ), with λ ≥ 2.
    (3) and (4) are equations for a group of MAs (notation: Moving
    Averages 3.0). They are independent of the choice of an MA. As
    the WMA shows the smallest lag (see introduction), it should
    generally be the first choice for the NMA.
    n1 = n2 results in the value 1 for α and λ, respectively. Then
    equation (3) passes into Ehlers´ formula. Thus Ehlers´ formula
    is included in the NMA formula as limiting value. It follows from
    a short calculation that the lag for NMA results in a theoretical
    value zero
                */
                
                PRICE =new DataSeries(this);
                PRICE.Set((High[0]+Low[0])/2);
                double lamda=(cycle_Period_n1/cycle_Period_n2);
                if(lamda>=2)
                {
                double alpha = (lamda * (cycle_Period_n1 - 1))/(cycle_Period_n1 - lamda);        
                //NMA = (1 +alpha)* SMA((Close/ period_n1),period_n2 ) - alpha * SMA(SMA/ n2);
                //PRICEc.Set((Open[0] + High[0] + Low[0] + Close[0]) / 4);
                NwMA.Set(NWMA(PRICE,cycle_Period_n1,cycle_Period_n2,alpha)[0]);
                }
                else if (cycle_Period_n1==cycle_Period_n2)
                {
                    NwMA.Set(NWMA(PRICE,cycle_Period_n1,cycle_Period_n2,1)[0]);
                }
                    
                else    
                
                {Print("Please Some Other Values");}
                //NwMA.Set(Close[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 NwMA
            {
                get { return Values[0]; }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public int Cycle_Period_n1
            {
                get { return cycle_Period_n1; }
                set { cycle_Period_n1 = Math.Max(1, value); }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public int Cycle_Period_n2
            {
                get { return cycle_Period_n2; }
                set { cycle_Period_n2 = Math.Max(1, value); }
            }
            #endregion
            
            #region Version Control
            [Description("Indicator version")]
            [Category(" Copyright (c)")]
            [Gui.Design.DisplayNameAttribute("Version")]
            public string VersionText
            {
                get { return _versionText; }
            }
            [Description(_copyrightText)]
            [Category(" Copyright (c)")]
            [Gui.Design.DisplayNameAttribute("Copyright 2015,2016,2017")]
            public string CopyrightText
            {
                get { return _copyrightText; }
            }
            #endregion    
        }

    #2
    Nyquist Criterion
    In signal processing theory, the application of a MA to itself
    can be seen as a Sampling procedure. The sampled signal is the
    MA (referred to as MA1) and the sampling signal is the MA as
    well (referred to as MA2). If additional periodic cycles which are
    not included in the price series are to be avoided sampling must
    obey the Nyquist Criterion [1, 4].
    With the cycle period as parameter, the usual one in
    Technical Analysis, the Nyquist Criterion reads as follows:
    n1 = λ*n2 , with λ ≥ 2.
    n1 is the cycle period of the sampled signal to which a sampling
    signal with cycle period n2 is applied. n1 must at least be twice as
    large as n2. In Mulloy´s and Ehlers´ approaches (referred to as
    Moving Averages 2.0) both cycle periods are equal.
    Moving Averages 3.0
    Using the Nyquist Criterion there is a relation by which the
    application of a MA to itself can be described more precisely. In
    figure 2 a price series C (black line), one MA (MA1, red line) with
    lag L1 to the price series and another MA with lag L2 to MA1 (MA2,
    blue line) are illustrated. Based on the approximation and the
    relations described in figure 2 the following equation holds:
    (1) D1/D2 = (C – MA1)/(MA1 – MA2) = L1/L2
    According to the lag formulas in the introduction L1/L2 can be
    written as follows:


    α := L1/L2 = (n1 – 1)/(n2 – 1).
    In this expression denominator 2 for the SMA and EMA as
    well as denominator 3 for the WMA are missing. α is therefore
    valid for all three MAs. Using the Nyquist Criterion one gets for
    α the following result:
    (2) α = λ* (n1 – 1)/(n1 – λ).
    α put in (1) and C replaced by the approximation term NMA,
    the notation for the new MA, one gets:
    NMA = (1 +α) MA1 – α MA2.
    In detail, equation (2) reads as follows:
    (3) NMA[price/ n1, n2] = (1 + α) MA1[price/ n1 ] – α
    MA2[MA1/ n2],
    (4) α = λ* (n1 – 1)/(n1 – λ), with λ ≥ 2.

    (3) and (4) are equations for a group of MAs (notation: Moving
    Averages 3.0). They are independent of the choice of an MA. As
    the WMA shows the smallest lag (see introduction), it should
    generally be the first choice for the NMA.
    n1 = n2 results in the value 1 for α and λ, respectively. Then
    equation (3) passes into Ehlers´ formula. Thus Ehlers´ formula
    is included in the NMA formula as limiting value. It follows from
    a short calculation that the lag for NMA results in a theoretical
    value zero.

    Comment


      #3
      We had discussed the Nyquist moving average when it was published back in 2011 in Big Mike's forum.

      https://www.bigmiketrading.com/germa...tml#post128971

      Is that the same average?
      Attached Files

      Comment


        #4
        Originally posted by Harry View Post
        We had discussed the Nyquist moving average when it was published back in 2011 in Big Mike's forum.

        https://www.bigmiketrading.com/germa...tml#post128971

        Is that the same average?

        yes.what have i missed? can u plz point out?

        Comment


          #5
          Sumana.m

          I see nothing standing out with the code.

          Are you receiving any errors?
          Cal H.NinjaTrader Customer Service

          Comment


            #6
            Hi Cal

            thanks for ur help;
            there is no error but the output should be different from the scrrenshot
            Attached Files
            Last edited by sumana.m; 04-02-2015, 07:02 AM.

            Comment


              #7
              Sumana.m,

              I would suggest adding Print()'s through out your code and print out values of your calculations and see what you are getting for them.
              http://www.ninjatrader.com/support/h...html?print.htm
              Cal H.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_Cal View Post
                Sumana.m,

                I would suggest adding Print()'s through out your code and print out values of your calculations and see what you are getting for them.
                http://www.ninjatrader.com/support/h...html?print.htm
                Thanks for the suggestion cal.
                i got something like in added attachment.i am afraid of WMA functions return.
                Attached Files
                Last edited by sumana.m; 04-02-2015, 07:54 AM.

                Comment


                  #9
                  Originally posted by sumana.m View Post
                  yes.what have i missed? can u plz point out?
                  What about simplifying the code?

                  Code:
                   #region Variables
                          private int                     fastPeriod                      = 21;
                          private int                     slowPeriod                     = 89;
                          private double              lambda                           = 0.0;
                          private double              alpha                              = 0.0;
                          private WMA                average1;
                          private WMA                average2;
                  #endregion
                  
                  protected override void OnStartUp()
                  {
                      if(slowPeriod < 2* fastPeriod)
                              slowPeriod = 2*fastPeriod; // this is needed to avoid problems
                     lambda = (double)slowPeriod/(double)fastPeriod;
                     alpha = lambda * (slowPeriod - 1.0)/(slowPeriod - lambda);
                     average1 = WMA(Input, slowPeriod);
                     average2 = WMA(average1, fastPeriod);
                  }
                  
                  protected override void OnBarUpdate()
                  {
                     Value.Set( (1+alpha)* average1[0] - alpha*average2[0]);
                  }
                  Last edited by Harry; 04-02-2015, 09:21 AM.

                  Comment


                    #10
                    Hi

                    Thanks for ur reply.
                    Originally posted by Harry View Post
                    What about simplifying the code?

                    Code:
                     #region Variables
                            private int                     fastPeriod                      = 21;
                            private int                     slowPeriod                     = 89;
                            private double              lambda                           = 0.0;
                            private double              alpha                              = 0.0;
                            private WMA                average1;
                            private WMA                average2;
                    #endregion
                    
                    protected override void OnStartUp()
                    {
                        if(slowPeriod < 2* fastPeriod)
                                slowPeriod = 2*fastPeriod; // this is needed to avoid problems
                       lambda = (double)slowPeriod/(double)fastPeriod;
                       alpha = lambda * (slowPeriod - 1.0)/(slowPeriod - lambda);
                       average1 = WMA(Input, slowPeriod);
                       average2 = WMA(average1, fastPeriod)
                    }
                    
                    protected override void OnBarUpdate()
                    {
                       Value.Set( (1+alpha)* average1[0] - alpha*average2[0]);
                    }

                    onstartup method is requred to call other indicator function ?but the output.txt suggesting that there is a issue for wma return.and code can not be exported.

                    2nd issue is overshoot of rice for shorter period.
                    Last edited by sumana.m; 04-02-2015, 11:20 AM.

                    Comment


                      #11
                      hi

                      getting error for attached code.what am i missing in such cases?
                      Attached Files
                      Last edited by sumana.m; 04-03-2015, 05:15 AM.

                      Comment


                        #12
                        Hello,

                        Thank you for the question.

                        Please see this document on how to create dataseries: http://www.ninjatrader.com/support/h...sub=dataseries

                        You have some invalid syntax in OnBarUpdate.

                        If you ignore the popup message you get and look in the Tools -> Output Window you will see you are getting an Object not set error.

                        This is because you have this statement:

                        Code:
                        PRICE =new DataSeries(this);
                        In OnBarUpdate instead of Initialize where it needs to be. Remove the above line from OnBarUpdate and place it in your Initialize and recompile and you should no longer have the error.

                        I look forward to being of further assistance.
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          @jesse and @cal
                          Thank you for your update.
                          why outputs differs posted #8 from orginal WMA() ?

                          plz explain if i can learn from the mistake in future ...

                          Comment


                            #14
                            Hello,

                            Can you provide the details on what you had done for the test on post #8?

                            When I am running the most recent script provided, I am getting totally different output so it looks like this may be a different script.

                            If so please use the most recent script and compose an example where you can see different output. After this please tell me if you had changed anything in the settings or what you had done so I could see what is happening.


                            I look forward to being of further assistance.
                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              Hi Jesse

                              thanks for ur update.

                              That testcode is done on the scrip on #1 after the suggestion of cal i made post #8;

                              2ndly i need to throw the exception ie. (try throw and catch syntax in ninja) for the cases when lamda less than 2 as this code do not provide solution for such cases.
                              Last edited by sumana.m; 04-03-2015, 09:13 AM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Zenersev, 10-19-2019, 02:11 PM
                              7 responses
                              49 views
                              0 likes
                              Last Post NinjaTrader_PatrickG  
                              Started by Lancer, Today, 10:39 AM
                              4 responses
                              24 views
                              0 likes
                              Last Post Lancer
                              by Lancer
                               
                              Started by koganam, Today, 11:50 AM
                              2 responses
                              12 views
                              0 likes
                              Last Post koganam
                              by koganam
                               
                              Started by koganam, Today, 10:33 AM
                              4 responses
                              14 views
                              0 likes
                              Last Post koganam
                              by koganam
                               
                              Started by FREEN, Today, 10:17 AM
                              4 responses
                              11 views
                              0 likes
                              Last Post FREEN
                              by FREEN
                               
                              Working...
                              X