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

Help converting Arnaud Legoux Moving Average (ALMA) from +NT7 to NT8

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

    Help converting Arnaud Legoux Moving Average (ALMA) from +NT7 to NT8

    Hi,

    I'm trying to convert a NT7 version of the Arnaud Legoux Moving Average to NT8. As a beginner coder, I'm just following guidance in moving the code around and could be getting completely wrong but the compiler is giving me only one error message relating to plotting. Appreciate any help. I've attached the original NT7 code if it helps.


    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class ALMA : Indicator
        {
    
            private int iWindowSize = 9;
            private double iM_Sigma = 6.0;
            private double iP_Sample = 0.5;
            private double[] aALMA;        
    
            protected override void OnStateChange()
            {
    
    
    
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "ALMA";
                    Calculate                                    = Calculate.OnBarClose;
                    IsOverlay                                    = true;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = false;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event. 
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
                    AddPlot(Brushes.Orange, "ALMA_Plot");
                }
                else if (State == State.Configure)
                {
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBar < iWindowSize)
                    return;
    
                int pt = 0;
    
                double agr  = 0;
                double norm = 0;
    
                for (int i = 0; i < iWindowSize; i++) 
                {
                    if (i < iWindowSize - pt) 
                    {
                        agr += aALMA[i] * Close[iWindowSize - pt - 1 - i];
                        norm += aALMA[i];
                    }
                }
    
                // Normalize the result
                if (norm != 0) agr /= norm;
    
                // Set the approrpiate bar.
                //ALMA_Plot.Set(agr);        
            }
    
    
            #region helper
    
            private void ResetWindow() 
            {
    
    
                double m = (int)Math.Floor(iP_Sample * (double)(iWindowSize - 1));
    
    
                double s = iWindowSize;
                s /= iM_Sigma;
    
                for (int i = 0; i < iWindowSize; i++) 
                {
                    aALMA[i] = Math.Exp(-((((double)i)-m)*(((double)i)-m))/(2*s*s));
                }
            }    
    
            #endregion
    
            #region Properties
    
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> ALMA_Plot
            {
                get { return Values[0]; }
            }
    
    
            [Description("Sample point. Where in terms of the window should we take the current value (0-1)")]
            [Category("Parameters")]
            public double SamplePoint
            {
                get { return iP_Sample; }
                set { iP_Sample = Math.Min(Math.Max(0.0, value), 1.0); }
            }
    
            [Description("ALMA period. Must be an odd number.")]
            [Category("Parameters")]
            public int WindowSize
            {
                get { return iWindowSize; }
                set { 
                    iWindowSize = Math.Min(Math.Max(1, value), 50);
                    // Only odd sizes
                    if ((iWindowSize & 1) == 0) 
                    {
                        iWindowSize++;
                    }
                    //ResetWindow();
                }
            }
    
            [Description("Precision / Smoothing")]
            [Category("Parameters")]
            public double Sigma
            {
                get { return iM_Sigma; }
                set { 
                        iM_Sigma = Math.Min(Math.Max(0.01, value), 50.0); //ResetWindow();
                    }
            }
    
    
    
            #endregion
    
        }
    }

    Attached Files

    #2
    Hi, thanks for your question.

    When an indicator fails always check the Log tab of the Control Center for errors.

    The problem with the conversion was not setting up the aALMA array in State.Configure.

    Code:
    else if (State == State.Configure)
                {
                    aALMA = new double[iWindowSize];
                    ResetWindow();
                }
    Also set the plot at the bottom of OnBarUpdate:

    Code:
    ALMA_Plot[0] = agr;
    Please let me know if I can assist any further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Awesome. Thanks Chris. The indicator is now plotting. Follow-up question: how does the code need to be changed so the three parameters (SamplePoint, WindowSize and Sigma) can be configured when I call the indicator from another script. Right now, when I type ALMA into a new script and mouseover, I can't see those inputs.

      Comment


        #4
        Hello mkt_anomalies,

        This is Jim, responding on behalf of Chris who is out of the office at this time.

        You can add user defined inputs to the script following the documentation below. The indicator can then have those inputs configured when adding to a chart, or adding via another script. You can then use the Strategy Builder to generate syntax for how the indicator can be called in another script.



        We look forward to assiting.
        JimNinjaTrader Customer Service

        Comment


          #5
          Thanks. User defined inputs successfully added! Just one thing left I can't solve. The indicator is not recognizing changes to the Data Series in the indicator properties. So say I apply a moving average to the input series, it will continue to use the default chart data series. What am I missing? Here's the script as it stands

          Code:
          namespace NinjaTrader.NinjaScript.Indicators
          {
              public class ALMA : Indicator
              {
          
                  private int iWindowSize = 11;
                  private double iM_Sigma = 6.0;
                  private double iP_Sample = 0.55;
                  private double[] aALMA;        
          
                  protected override void OnStateChange()
                  {
          
          
          
                      if (State == State.SetDefaults)
                      {
                          Description                                    = @"Enter the description for your new custom Indicator here.";
                          Name                                        = "ALMA";
                          Calculate                                    = Calculate.OnBarClose;
                          IsOverlay                                    = true;
                          DisplayInDataBox                            = true;
                          DrawOnPricePanel                            = false;
                          DrawHorizontalGridLines                        = true;
                          DrawVerticalGridLines                        = true;
                          PaintPriceMarkers                            = true;
                          ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                          //Disable this property if your indicator requires custom values that cumulate with each new market data event. 
                          //See Help Guide for additional information.
                          IsSuspendedWhileInactive                    = true;
                          AddPlot(Brushes.Orange, "ALMA_Plot");
                      }
                      else if (State == State.Configure)
                      {
                          aALMA = new double[iWindowSize];
                          ResetWindow();                
                      }
                  }
          
                  protected override void OnBarUpdate()
                  {
                      if (CurrentBar < iWindowSize)
                          return;
          
                      int pt = 0;
          
                      double agr  = 0;
                      double norm = 0;
          
                      for (int i = 0; i < iWindowSize; i++) 
                      {
                          if (i < iWindowSize - pt) 
                          {
                              agr += aALMA[i] * Close[iWindowSize - pt - 1 - i];
                              norm += aALMA[i];
                          }
                      }
          
                      // Normalize the result
                      if (norm != 0) agr /= norm;
          
                      // Set the approrpiate bar.
                      //ALMA_Plot.Set(agr);    
                      ALMA_Plot[0] = agr;
                  }
          
          
                  #region helper
          
                  private void ResetWindow() 
                  {
          
          
                      double m = (int)Math.Floor(iP_Sample * (double)(iWindowSize - 1));
          
          
                      double s = iWindowSize;
                      s /= iM_Sigma;
          
                      for (int i = 0; i < iWindowSize; i++) 
                      {
                          aALMA[i] = Math.Exp(-((((double)i)-m)*(((double)i)-m))/(2*s*s));
                      }
                  }    
          
                  #endregion
          
                  #region Properties
          
                  [Browsable(false)]
                  [XmlIgnore]
                  public Series<double> ALMA_Plot
                  {
                      get { return Values[0]; }
                  }
          
          
          //        [Description("Sample point. Where in terms of the window should we take the current value (0-1)")]
          //        [Category("Parameters")]
                  [Range(0.0001, int.MaxValue), NinjaScriptProperty]
                  [Display(ResourceType = typeof(Custom.Resource), Name = "Sample Point", GroupName = "NinjaScriptParameters", Order = 0)]        
                  public double SamplePoint
                  {
                      get { return iP_Sample; }
                      set { iP_Sample = Math.Min(Math.Max(0.0, value), 1.0); }
                  }
          
          //        [Description("ALMA period. Must be an odd number.")]
          //        [Category("Parameters")]
                  [Range(1, int.MaxValue), NinjaScriptProperty]
                  [Display(ResourceType = typeof(Custom.Resource), Name = "Window Size", GroupName = "NinjaScriptParameters", Order = 1)]        
                  public int WindowSize
                  {
                      get { return iWindowSize; }
                      set { 
                          iWindowSize = Math.Min(Math.Max(1, value), 50);
                          // Only odd sizes
                          if ((iWindowSize & 1) == 0) 
                          {
                              iWindowSize++;
                          }
                          //ResetWindow();
                      }
                  }
          
          //        [Description("Precision / Smoothing")]
          //        [Category("Parameters")]
                  [Range(1, int.MaxValue), NinjaScriptProperty]
                  [Display(ResourceType = typeof(Custom.Resource), Name = "Sigma", GroupName = "NinjaScriptParameters", Order = 2)]
                  public double Sigma
                  {
                      get { return iM_Sigma; }
                      set { 
                              iM_Sigma = Math.Min(Math.Max(0.01, value), 50.0); //ResetWindow();
                          }
                  }
          
          
          
                  #endregion
          
              }
          }

          Comment


            #6
            Hi, thanks for your reply.

            You would need to replay Close with "Inputs". I attached an example to compare against. The default ATR indicator does not support an input series but using the Input series instead of the Close series fixes this.

            Please let me know if I can assist any further.
            Attached Files
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChrisL View Post
              Hi, thanks for your reply.

              You would need to replay Close with "Inputs". I attached an example to compare against. The default ATR indicator does not support an input series but using the Input series instead of the Close series fixes this.

              Please let me know if I can assist any further.
              I am struggling with the same issue and am not sure what needs to be done. Can you please help?

              Comment


                #8
                Attach your latest greatest script.

                I'll take care of the changes and repost it.

                Comment


                  #9
                  Thanks so much! That will be so awesome bltdavid. Here you go. It is not giving any errors, but does not import.
                  Attached Files

                  Comment


                    #10
                    Fixed.

                    Looks like the only thing missing was the 'Using' declarations.

                    Try importing the attached file.
                    Attached Files

                    Comment


                      #11
                      Originally posted by bltdavid View Post
                      Fixed.

                      Looks like the only thing missing was the 'Using' declarations.

                      Try importing the attached file.
                      Thanks so much. It worked!!

                      Comment


                        #12
                        So I am reviving an old thread, but the last update to this indicator had the Window Size, Sigma, and Sample all hard coded in the indicator. I added the code so that those parameters could be adjusted in the indicators properties.
                        Attached Files

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by rhyminkevin, Today, 04:58 PM
                        3 responses
                        47 views
                        0 likes
                        Last Post Anfedport  
                        Started by iceman2018, Today, 05:07 PM
                        0 responses
                        5 views
                        0 likes
                        Last Post iceman2018  
                        Started by lightsun47, Today, 03:51 PM
                        0 responses
                        7 views
                        0 likes
                        Last Post lightsun47  
                        Started by 00nevest, Today, 02:27 PM
                        1 response
                        14 views
                        0 likes
                        Last Post 00nevest  
                        Started by futtrader, 04-21-2024, 01:50 AM
                        4 responses
                        50 views
                        0 likes
                        Last Post futtrader  
                        Working...
                        X