Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

EMA code

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

    EMA code

    Guys,

    I'm trying to create an EMA of a variable and keep getting error messages. Any help u could provide would be greatly appreciated.

    #2
    please , have picture of the error message and error code or the programming line of the added variable.
    If you are unsure as to what the error is indicating, please post a screenshot of the NinjaScript File column and Error column fully visible (not to be confused with the Code column)
    • To send a screenshot with Windows 7 or newer I would recommend using the Windows Snipping Tool.
    • Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save as a jpeg file and send the file as an attachment.
    Last edited by Emma1; 06-15-2022, 05:44 AM.

    Comment


      #3
      Hello JWeather,

      Thank you for writing in.

      Before providing any further information, I wanted to verify if you were working on a NinjaScript Strategy that you are creating yourself or if you are working with a NinjaScript Strategy that you received from someone else or a 3rd party developer.
      • This post is in the Platform Technical Support section of the Forum vs. the "Strategy Development" or "Indicator Development" section of the forum so I want to make sure

      Comment


        #4
        I copied the LInear Regression code from NT8 which is where the Value is calculating. I was trying to bound the indicator and smooth it out with the EMA. The error message I'm getting is that "Cannot implicity convert EMA to double".


        Value[0] = CurrentBar == 0 ? input0 : (intercept + slope * (myPeriod - 1));
        }
        double Value0 = Value[0];
        double Value1 = Value[1];
        if (Value0 > Value1)
        {double LRR = 1.0;
        }
        else
        {double LRR = -1.0;
        }
        {
        double JLRR = EMA(LRR,14);
        }}

        Comment


          #5
          Hi JWeather, thanks for posting. The EMA is not indexed. To get the latest value, do this:
          Code:
          double JLRR = EMA(LRR,14)[0];

          Comment


            #6
            Thanks for the help. I added the index but can't get the indicator to plot? Any thoughts?
            Thanks,
            JWeather

            Comment


              #7
              HI JWeather, I would need you to post the rest of your script for me to know whats going on. Use Print(); in your script to see what the value of everything is e.g.
              Print(Value[0]);
              Print(JLRR );
              Print(Time[0]);

              Also check the Log tab of the Control Center for any errors.

              Comment


                #8
                LRR needs to be a Series<double>, not a Double variable.
                Then:
                if (Value0 > Value1)
                {LRR[0] = 1.0;
                }
                else
                {LRR[0] = -1.0;
                }
                {
                double JLRR = EMA(LRR,14);
                Last edited by eDanny; 06-16-2022, 11:12 AM.
                eDanny
                NinjaTrader Ecosystem Vendor - Integrity Traders

                Comment


                  #9

                  Chris,

                  I'm getting an error message of:
                  Indicator 'JWLRR': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

                  I've copied the script as you suggested. I'm obviously a novice at this NT code - any help you could provide would be greatly appreciated.

                  /// </summary>
                  public class JWLRR : Indicator
                  {

                  private Series<double> LRR;

                  private double avg;
                  private double divisor;
                  private double intercept;
                  private double myPeriod;
                  private double priorSumXY;
                  private double priorSumY;
                  private double slope;
                  private double sumX2;
                  private double sumX;
                  private double sumXY;
                  private double sumY;
                  private SUM sum;
                  private double JLRR;



                  protected override void OnStateChange()
                  {
                  if (State == State.SetDefaults)
                  {//Description = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDe scriptionNameJWLRR;
                  Name = "JWLRR";
                  Calculate = Calculate.OnBarClose;
                  //DisplayInDataBox = true;
                  //DrawonPricePanel = false;
                  IsOverlay = false;
                  IsSuspendedWhileInactive = true;
                  Period = 14;

                  AddPlot(Brushes.Goldenrod, NinjaTrader.Custom.Resource.NinjaScriptIndicatorNa meLinReg);
                  AddPlot(Brushes.DodgerBlue, "MyPlot");
                  }
                  else if (State == State.Configure)
                  {
                  avg = divisor = intercept = myPeriod = priorSumXY
                  = priorSumY = slope = sumX = sumX2 = sumY = sumXY = 0;
                  }
                  else if (State == State.DataLoaded)
                  {
                  sum = SUM(Inputs[0], Period);

                  }
                  }

                  protected override void OnBarUpdate()
                  {
                  if (BarsArray[0].BarsType.IsRemoveLastBarSupported)
                  {
                  double sumX = (double)Period * (Period - 1) * 0.5;
                  double divisor = sumX * sumX - (double)Period * Period * (Period - 1) * (2 * Period - 1) / 6;
                  double sumXY = 0;

                  for (int count = 0; count < Period && CurrentBar - count >= 0; count++)
                  sumXY += count * Input[count];

                  double slope = ((double)Period * sumXY - sumX * SUM(Inputs[0], Period)[0]) / divisor;
                  double intercept = (SUM(Inputs[0], Period)[0] - slope * sumX) / Period;

                  Value[0] = intercept + slope * (Period - 1);
                  }
                  else
                  {
                  if (IsFirstTickOfBar)
                  {
                  priorSumY = sumY;
                  priorSumXY = sumXY;
                  myPeriod = Math.Min(CurrentBar + 1, Period);
                  sumX = myPeriod * (myPeriod - 1) * 0.5;
                  sumX2 = myPeriod * (myPeriod + 1) * 0.5;
                  divisor = myPeriod * (myPeriod + 1) * (2 * myPeriod + 1) / 6 - sumX2 * sumX2 / myPeriod;
                  }

                  double input0 = Input[0];
                  sumXY = priorSumXY - (CurrentBar >= Period ? priorSumY : 0) + myPeriod * input0;
                  sumY = priorSumY + input0 - (CurrentBar >= Period ? Input[Period] : 0);
                  avg = sumY / myPeriod;
                  slope = (sumXY - sumX2 * avg) / divisor;
                  intercept = (sum[0] - slope * sumX) / myPeriod;
                  Value[0] = CurrentBar == 0 ? input0 : (intercept + slope * (myPeriod - 1));
                  }
                  double Value0 = Value[0];
                  double Value1 = Value[1];
                  if (Value0 > Value1)
                  {LRR[0] = 1.0;
                  }
                  else
                  {LRR[0] = -1.0;
                  }
                  {
                  double JLRR = EMA(LRR,14)[0];

                  }
                  { MyPlot[1] = JLRR;

                  }
                  Print(Value[0]);
                  Print(Value1);
                  Print(LRR);
                  }

                  #region Properties
                  [Range(2, int.MaxValue), NinjaScriptProperty]
                  [Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)]
                  public int Period
                  { get; set; }
                  [Browsable(false)]
                  [XmlIgnore]
                  public Series<double> MyPlot
                  {
                  get { return Values[1]; }
                  }

                  Comment


                    #10
                    Hi JWeather, thanks for posting that. The First thing I notice is the use of if (IsFirstTickOfBar) in a strategy that runs OnBarUpdate. That value will only vary if the strategy runs OnEachTick or OnPriceChange. The indexing error can be found using Visual Studio debugging. You can attach Visual Studio to the NinjaTrader process, run the code, and Visual Studio will break at the line that caused the exception.


                    Kind regards,
                    -ChrisL

                    Comment


                      #11
                      Try this:
                      else if (State == State.DataLoaded)
                      {
                      LRR = new Series<double>(this);
                      }

                      protected override void OnBarUpdate()
                      {
                      if(CurrentBar < Period)
                      return;
                      eDanny
                      NinjaTrader Ecosystem Vendor - Integrity Traders

                      Comment


                        #12
                        I've been able to get the code to calculate an EMA of a Value by assigning a double Average to the EMA;
                        double Average = EMA(Value,14)[0]
                        but I need to index the EMA from the prior bar. As I understand it, you can only index Series <double> so how would I do that?
                        Thanks,
                        JWeather

                        Comment


                          #13
                          Hi JWeather, We have a guide on how to use any price series in a script here:


                          The previous EMA value can be accessed by referencing 1 index back e.g. EMA(Value,14)[1]

                          Comment


                            #14
                            Chris,

                            I've been able to plot a diamond on the ema cross, but the diamonds don't stay on the chart - they keep moving. Is there a command that would paint every cross encountered?
                            thanks,
                            JWeather

                            Comment


                              #15
                              Hi JWeather, Use a unique Tag string to keep them placed on the chart. You can use this for the Tag:

                              ("DOTTAG" + CurrentBar)

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              668 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              378 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              111 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              575 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              580 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X