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

How to find the max value

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

    How to find the max value

    This is my first NinjaScript and I am trying to build an indicator that simply finds the max value of the current Open and Close. I have tried the following:

    Plot0.Set(max(Open[0],Close[0]));

    and it give me an error. Can someone please let me know how to do this.

    Thanks, Steven

    #2
    Hi Steven,

    Welcome to the NinjaTrader forums. To get the highest of two values, you can use C#s Math.Max()

    Plot0.Set(Math.Max(Open[0],Close[0]));
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Thanks...It now works!

      I now have the following formula. It takes the average length of the wicks per bar:

      Plot0.Set(((High[0]-Math.Max(Open[0],Close[0]))+(Math.Min(Open[0],Close[0])-Low[0]))/2);

      How do I have a SMA of the above formula.

      Thanks, Steven

      Comment


        #4
        In order to pass that value into an SMA indicator, you need to create a custom data series. The .Set portion of the data series is identical to the Plot0 .Set.

        For the rest of the structure, this post can help you use data series in NinjaScript:
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          If I understand correctly, I need to define the new custom variable first then I will be able to apply an average function to it.

          I am having problems understanding how to do define the variable first even after reading through the help guide. Do I define what type of variable it is or do I define the exact formula as below. Lets say I want to define a variable 'Wicklength'. How do I do this?

          Thanks, Steven

          Comment


            #6
            If you take a look at the sample, you'll see 3 parts needed to DataSeries.

            1) Declare in Variables region:
            private DataSeries myDataSeries;

            2) Instantiate in Initialize():
            myDataSeries = new DataSeries (this);

            3) Set in OnBarUpdate()
            myDataSeries.Set(Close[0]); //use your own formula here.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              I can not open your sample file. I have downloaded and unzipped it but when I double click on it it will not open. Can you please explain how to open it.

              Thanks, Steven

              Comment


                #8
                Sure. Download the zip file to your computer. You do not need to work with the contents of it manually.

                To import:

                1. From the Control Center window select the menu File > Utilities > Import NinjaScript to open the Import NinjaScript dialog window
                2. Select the file you want to import
                3. Press the "Open" button

                To view/edit the code: Click Tools menu > Edit NinjaScript > Indicator > Select the indicator and then click Open.
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Thanks I will give it a try.

                  Is there a course or webinar that NinjaTrader offers for beginners to start programming in NinjaScript?

                  Thanks, Steven

                  Comment


                    #10
                    We have introductory tutorials in our help guide. There are a good start for breaking down the different parts of a NinjaScript file.

                    We don't have a dedicated NinjaScript webinar, but our automated strategy one uses the strategy wizard to create an automated strategy. This can be helpful for building NinjaScript expressions in a point and click interface.
                    Ryan M.NinjaTrader Customer Service

                    Comment


                      #11
                      I was able to open you sample file and simply changed your calculation in line 57 with mine and it does not work as I am getting an error code of CS0201. I do not understand as your unmodified indicator worked fine and my change was s simple one.

                      Can you please tell me what I did wrong.

                      Here is the code below:

                      //
                      // Copyright (C) 2008, NinjaTrader LLC <www.ninjatrader.com>.
                      // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
                      //

                      #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>
                      /// Reference sample demonstrating how to use DataSeries objects to store self calculations.
                      /// </summary>
                      [Description("Reference sample demonstrating how to use DataSeries objects to store self calculations.")]
                      public class SampleCustomDataSeries : Indicator
                      {
                      #region Variables
                      private int period = 5;

                      // Defines the DataSeries object
                      private DataSeries myDataSeries;
                      #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()
                      {
                      // Adds a plot to our NinjaScript Indicator
                      Add(new Plot(Color.Orange, PlotStyle.Bar, "Average Range"));

                      // Create a new DataSeries object and assign it to the variable myDataSeries declared in the ‘Variables’ region above
                      myDataSeries = new DataSeries(this);

                      CalculateOnBarClose = true;
                      Overlay = false;
                      }

                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      {
                      /* To set values to our DataSeries we use the Set() method. Here we are setting the DataSeries
                      object for the current bar to take on the absolute value of the difference between the current bar's
                      open and close. */
                      myDataSeries.Set(((High[0]-Math.Max(Open[0],Close[0]))+(Math.Min(Open[0],Close[0])-Low[0]))/2);

                      /* Take note that the method for setting the value to be plotted is the same as for setting a value
                      to a DataSeries object. The difference here is that the custom DataSeries object is not plotted while
                      this "AvgR" is plotted.

                      In this case we are plotting the Simple Moving Average of the intermediate calculation step stored in our
                      DataSeries object. */
                      AvgR.Set(SMA(myDataSeries, Period)[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 AvgR
                      {
                      get { return Values[0]; }
                      }

                      [Description("Numbers of bars used for calculations")]
                      [Category("Parameters")]
                      public int Period
                      {
                      get { return period; }
                      set { period = Math.Max(1, value); }
                      }
                      #endregion
                      }
                      }

                      #region NinjaScript generated code. Neither change nor remove.
                      // This namespace holds all indicators and is required. Do not change it.
                      namespace NinjaTrader.Indicator
                      {
                      public partial class Indicator : IndicatorBase
                      {
                      private SampleCustomDataSeries[] cacheSampleCustomDataSeries = null;

                      private static SampleCustomDataSeries checkSampleCustomDataSeries = new SampleCustomDataSeries();

                      /// <summary>
                      /// Reference sample demonstrating how to use DataSeries objects to store self calculations.
                      /// </summary>
                      /// <returns></returns>
                      public SampleCustomDataSeries SampleCustomDataSeries(int period)
                      {
                      return SampleCustomDataSeries(Input, period);
                      }

                      /// <summary>
                      /// Reference sample demonstrating how to use DataSeries objects to store self calculations.
                      /// </summary>
                      /// <returns></returns>
                      public SampleCustomDataSeries SampleCustomDataSeries(Data.IDataSeries input, int period)
                      {
                      if (cacheSampleCustomDataSeries != null)
                      for (int idx = 0; idx < cacheSampleCustomDataSeries.Length; idx++)
                      if (cacheSampleCustomDataSeries[idx].Period == period && cacheSampleCustomDataSeries[idx].EqualsInput(input))
                      return cacheSampleCustomDataSeries[idx];

                      lock (checkSampleCustomDataSeries)
                      {
                      checkSampleCustomDataSeries.Period = period;
                      period = checkSampleCustomDataSeries.Period;

                      if (cacheSampleCustomDataSeries != null)
                      for (int idx = 0; idx < cacheSampleCustomDataSeries.Length; idx++)
                      if (cacheSampleCustomDataSeries[idx].Period == period && cacheSampleCustomDataSeries[idx].EqualsInput(input))
                      return cacheSampleCustomDataSeries[idx];

                      SampleCustomDataSeries indicator = new SampleCustomDataSeries();
                      indicator.BarsRequired = BarsRequired;
                      indicator.CalculateOnBarClose = CalculateOnBarClose;
                      #if NT7
                      indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                      indicator.MaximumBarsLookBack = MaximumBarsLookBack;
                      #endif
                      indicator.Input = input;
                      indicator.Period = period;
                      Indicators.Add(indicator);
                      indicator.SetUp();

                      SampleCustomDataSeries[] tmp = new SampleCustomDataSeries[cacheSampleCustomDataSeries == null ? 1 : cacheSampleCustomDataSeries.Length + 1];
                      if (cacheSampleCustomDataSeries != null)
                      cacheSampleCustomDataSeries.CopyTo(tmp, 0);
                      tmp[tmp.Length - 1] = indicator;
                      cacheSampleCustomDataSeries = tmp;
                      return indicator;
                      }
                      }
                      }
                      }

                      // This namespace holds all market analyzer column definitions and is required. Do not change it.
                      namespace NinjaTrader.MarketAnalyzer
                      {
                      public partial class Column : ColumnBase
                      {
                      /// <summary>
                      /// Reference sample demonstrating how to use DataSeries objects to store self calculations.
                      /// </summary>
                      /// <returns></returns>
                      [Gui.Design.WizardCondition("Indicator")]
                      public Indicator.SampleCustomDataSeries SampleCustomDataSeries(int period)
                      {
                      return _indicator.SampleCustomDataSeries(Input, period);
                      }

                      /// <summary>
                      /// Reference sample demonstrating how to use DataSeries objects to store self calculations.
                      /// </summary>
                      /// <returns></returns>
                      public Indicator.SampleCustomDataSeries SampleCustomDataSeries(Data.IDataSeries input, int period)
                      {
                      return _indicator.SampleCustomDataSeries(input, period);
                      }
                      }
                      }

                      // This namespace holds all strategies and is required. Do not change it.
                      namespace NinjaTrader.Strategy
                      {
                      public partial class Strategy : StrategyBase
                      {
                      /// <summary>
                      /// Reference sample demonstrating how to use DataSeries objects to store self calculations.
                      /// </summary>
                      /// <returns></returns>
                      [Gui.Design.WizardCondition("Indicator")]
                      public Indicator.SampleCustomDataSeries SampleCustomDataSeries(int period)
                      {
                      return _indicator.SampleCustomDataSeries(Input, period);
                      }

                      /// <summary>
                      /// Reference sample demonstrating how to use DataSeries objects to store self calculations.
                      /// </summary>
                      /// <returns></returns>
                      public Indicator.SampleCustomDataSeries SampleCustomDataSeries(Data.IDataSeries input, int period)
                      {
                      if (InInitialize && input == null)
                      throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

                      return _indicator.SampleCustomDataSeries(input, period);
                      }
                      }
                      }
                      #endregion

                      Comment


                        #12
                        What is the descriptive error message you're getting? Expand the "Error" column to see this.

                        What lines of code(the actual code not line numbers) generate the message?
                        Ryan M.NinjaTrader Customer Service

                        Comment


                          #13
                          I have included a screen shot for you.

                          I also noticed that there might be more lines of code than the 87 since it jumps to 205, and this maybe the problem.

                          Thanks, Steven
                          Attached Files

                          Comment


                            #14
                            Originally posted by StevenV View Post
                            I have included a screen shot for you.

                            I also noticed that there might be more lines of code than the 87 since it jumps to 205, and this maybe the problem.

                            Thanks, Steven
                            It would seem that you have some extraneous non-printable character(s) on Line 48.

                            Comment


                              #15
                              Are you sure the errors you're getting pertain to this file? I see no problems with the code, and the line # indicated in the error (line 48) doesn't use SMA anywhere. The far-left column there indicates the exact files causing the programming errors.
                              Ryan M.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by wzgy0920, Yesterday, 09:53 PM
                              1 response
                              13 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by Rapine Heihei, Yesterday, 07:51 PM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by kaywai, Today, 06:26 AM
                              1 response
                              6 views
                              0 likes
                              Last Post kaywai
                              by kaywai
                               
                              Started by ct, 05-07-2023, 12:31 PM
                              6 responses
                              206 views
                              0 likes
                              Last Post wisconsinpat  
                              Started by kevinenergy, 02-17-2023, 12:42 PM
                              118 responses
                              2,780 views
                              1 like
                              Last Post kevinenergy  
                              Working...
                              X