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

Sample Code but not plotting please advice

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

    Sample Code but not plotting please advice

    I want to plot the diff between the current bar close and the previous bar close as an indicator and I have written the following, please advice if correct

    protected override void Initialize()
    {
    Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
    CalculateOnBarClose = true;
    Overlay = false;
    PriceTypeSupported = 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.
    Plot0.Set(Close[0]-Close[1]);
    }

    #2
    You cannot access an index of [1] without first ensuring you have enough bars loaded. Please see this tip: http://www.ninjatrader-support2.com/...ead.php?t=3170
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks, I could manage to get it. One more question if I need to sum the diff of the current bar close and previous bar close and then plot how will i do that eg

      if (CurrentBar < 1)
      return;
      else
      Plot0.Set sum((Close[0]-Close[1],10);
      }

      Iam trying to plot the sum of diff of 10 bars

      Originally posted by NinjaTrader_Josh View Post
      You cannot access an index of [1] without first ensuring you have enough bars loaded. Please see this tip: http://www.ninjatrader-support2.com/...ead.php?t=3170

      Comment


        #4
        You need to create a custom DataSeries to store your Close[0] - Close[1] calculation first. Then you can run the SUM() on your DataSeries and then plot that value.

        Please see the Help Guide for how to make your own DataSeries.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          I tried doing it but it shows error, please advice

          {
          // 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 < 1)
          return;
          else
          {
          // Calculate the range of the current bar and set the value
          myDataSeries.Set(Close[0] - Close[1]);
          }

          //double value1=SUM(myDataSeries,10);
          //Plot0.Set ((Close[0]-Close[1]));
          Plot0.Set(SUM(myDataSeries,10));
          }

          Originally posted by NinjaTrader_Josh View Post
          You need to create a custom DataSeries to store your Close[0] - Close[1] calculation first. Then you can run the SUM() on your DataSeries and then plot that value.

          Please see the Help Guide for how to make your own DataSeries.

          Comment


            #6
            Plot0.Set(SUM(myDataSeries,10));

            You cannot do that. You need to include the [] index value. Try:

            Plot0.Set(SUM(myDataSeries, 10)[0]);
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Thanks, I tried but it is not plotting anything

              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 < 1)
              return;
              else
              {
              // Calculate the range of the current bar and set the value
              myDataSeries.Set(Close[0] - Close[1]);
              }

              //double value1=SUM(myDataSeries,10);
              //Plot0.Set ((Close[0]-Close[1]));
              Plot0.Set(SUM(myDataSeries,10)[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 Plot0
              {
              get { return Values[0]; }
              }

              [Description("")]
              [Category("Parameters")]
              public int MyInput0
              {
              get { return myInput0; }
              set { myInput0 = Math.Max(1, value); }
              }
              #endregion
              }
              }

              Originally posted by NinjaTrader_Josh View Post
              Plot0.Set(SUM(myDataSeries,10));

              You cannot do that. You need to include the [] index value. Try:

              Plot0.Set(SUM(myDataSeries, 10)[0]);

              Comment


                #8
                Please check your Control Center logs for errors.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  the log shows
                  Error on calling the 'OnBarUpdate' method for indicator 'v' on bar 1: Object reference not set to an instance of an object.


                  Originally posted by NinjaTrader_Josh View Post
                  Please check your Control Center logs for errors.

                  Comment


                    #10
                    Please post the full code including your Initialize().
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Following is the 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>
                      /// Enter the description of your new custom indicator here
                      /// </summary>
                      [Description("Enter the description of your new custom indicator here")]
                      public class v : Indicator
                      {
                      #region Variables
                      // Wizard generated variables
                      private int myInput0 = 1; // Default setting for MyInput0
                      private DataSeries myDataSeries;
                      // 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, "Plot0"));
                      CalculateOnBarClose = true;
                      Overlay = false;
                      PriceTypeSupported = 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 < 1)
                      return;
                      else


                      {
                      // Calculate the range of the current bar and set the value
                      myDataSeries.Set(Close[0] - Close[1]);
                      }

                      //double value1=SUM(myDataSeries,10);
                      //Plot0.Set ((Close[0]-Close[1]));
                      Plot0.Set(SUM(myDataSeries,10)[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 Plot0
                      {
                      get { return Values[0]; }
                      }

                      [Description("")]
                      [Category("Parameters")]
                      public int MyInput0
                      {
                      get { return myInput0; }
                      set { myInput0 = 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 vhf[] cachevhf = null;

                      private static vhf checkvhf = new vhf();

                      /// <summary>
                      /// Enter the description of your new custom indicator here
                      /// </summary>
                      /// <returns></returns>
                      public vhf vhf(int myInput0)
                      {
                      return vhf(Input, myInput0);
                      }

                      /// <summary>
                      /// Enter the description of your new custom indicator here
                      /// </summary>
                      /// <returns></returns>
                      public vhf vhf(Data.IDataSeries input, int myInput0)
                      {
                      checkvhf.MyInput0 = myInput0;
                      myInput0 = checkvhf.MyInput0;

                      if (cachevhf != null)
                      for (int idx = 0; idx < cachevhf.Length; idx++)
                      if (cachevhf[idx].MyInput0 == myInput0 && cachevhf[idx].EqualsInput(input))
                      return cachevhf[idx];

                      vhf indicator = new vhf();
                      indicator.BarsRequired = BarsRequired;
                      indicator.CalculateOnBarClose = CalculateOnBarClose;
                      indicator.Input = input;
                      indicator.MyInput0 = myInput0;
                      indicator.SetUp();

                      vhf[] tmp = new vhf[cachevhf == null ? 1 : cachevhf.Length + 1];
                      if (cachevhf != null)
                      cachevhf.CopyTo(tmp, 0);
                      tmp[tmp.Length - 1] = indicator;
                      cachevhf = tmp;
                      Indicators.Add(indicator);

                      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>
                      /// Enter the description of your new custom indicator here
                      /// </summary>
                      /// <returns></returns>
                      [Gui.Design.WizardCondition("Indicator")]
                      public Indicator.vhf vhf(int myInput0)
                      {
                      return _indicator.vhf(Input, myInput0);
                      }

                      /// <summary>
                      /// Enter the description of your new custom indicator here
                      /// </summary>
                      /// <returns></returns>
                      public Indicator.vhf vhf(Data.IDataSeries input, int myInput0)
                      {
                      return _indicator.vhf(input, myInput0);
                      }

                      }
                      }

                      // This namespace holds all strategies and is required. Do not change it.
                      namespace NinjaTrader.Strategy
                      {
                      public partial class Strategy : StrategyBase
                      {
                      /// <summary>
                      /// Enter the description of your new custom indicator here
                      /// </summary>
                      /// <returns></returns>
                      [Gui.Design.WizardCondition("Indicator")]
                      public Indicator.vhf vhf(int myInput0)
                      {
                      return _indicator.vhf(Input, myInput0);
                      }

                      /// <summary>
                      /// Enter the description of your new custom indicator here
                      /// </summary>
                      /// <returns></returns>
                      public Indicator.vhf vhf(Data.IDataSeries input, int myInput0)
                      {
                      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.vhf(input, myInput0);
                      }

                      }
                      }
                      #endregion
                      Originally posted by NinjaTrader_Bertrand View Post
                      Please post the full code including your Initialize().

                      Comment


                        #12
                        You would need to assign the dataseries variable to the object, too in the Initialize() -

                        Code:
                        myDataSeries = new DataSeries(this);
                        BertrandNinjaTrader Customer Service

                        Comment


                          #13
                          Thanks I was able to do it but 2 more problems, please advice:

                          1. I made a strategy that uses this indicator. The indicator values are in the range of .0002 and when I run the optimisation on euro the indicator gets plotted by default. Since the price chart has to accommodate both the price which has values in the range of 1.5 and indicator has values in the range of .0002-.0004 the chart is nor properly displayed.

                          2. When I was developing the indicator I developed two data series Dataseria A and dataseries b. Final indicator comes by division of these data series. How can I plot the final indicator without plotting the dataseries'a' and dataseries'b'.

                          Originally posted by NinjaTrader_Bertrand View Post
                          You would need to assign the dataseries variable to the object, too in the Initialize() -

                          Code:
                          myDataSeries = new DataSeries(this);
                          http://www.ninjatrader-support.com/H...iesObject.html

                          Comment


                            #14
                            1. When you add the indicator to the strategy, add it to a new panel. Please see here: http://www.ninjatrader-support2.com/...ead.php?t=3228

                            2. Only actual indicator plots are plotted. If you created DataSeries objects like myDataSeries those already are not plotted.
                            Josh P.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Stanfillirenfro, Yesterday, 09:19 AM
                            7 responses
                            51 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Started by TraderCro, 04-12-2024, 11:36 AM
                            4 responses
                            69 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by Mindset, Yesterday, 02:04 AM
                            1 response
                            15 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by agclub, 04-21-2024, 08:57 PM
                            4 responses
                            18 views
                            0 likes
                            Last Post NinjaTrader_Gaby  
                            Started by Irukandji, Today, 04:58 AM
                            0 responses
                            6 views
                            0 likes
                            Last Post Irukandji  
                            Working...
                            X