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

Use PeriodType at Indicators

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

    Use PeriodType at Indicators

    Hi,

    I'm trying to use period type of days in indicator but i'm getting error.
    there is any way that i can use it?

    Igal

    #2
    Hi Igal,

    Thank you for posting.

    What kind of error are you receiving? You can check the Log Tab for any errors

    How many days worth of data are you loading on the chart?

    When you first enable the strategy, What setting is the Minimum Bars Required set to?
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Hi,
      This is the error from the output :

      Error on calling 'OnBarUpdate' method for indicator 'ASI' on bar 2880: Object reference not set to an instance of an object.

      This is my Code :
      protected override void Initialize()
      {
      Add(new Plot(Color.Green, "ASIndex"));
      Add(PeriodType.Day,1);

      }

      /// <summary>
      /// Calculates the indicator value(s) at the current index.
      /// </summary>
      protected override void OnBarUpdate()
      {
      if (CurrentBar != 0)
      return;

      if (CurrentBar < 2880)
      return;

      i reloaded data since 2008.

      Comment


        #4
        Igal,

        Thanks for the code snippet. Unfortunately, the error is from calling an object that is either not valid or has not been setup properly not the amount of bars you are using.

        You would need to post the rest of the code to see where the error might be.
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          //
          // Copyright (C) 2006, 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.Drawing;
          using System.Xml.Serialization;
          using NinjaTrader.Data;
          using NinjaTrader.Gui.Chart;
          #endregion

          // This namespace holds all indicators and is required. Do not change it.
          namespace NinjaTrader.Indicator
          {
          /// <summary>
          /// The ASI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
          /// </summary>
          [Description("The ASI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.")]
          public class ASI : Indicator
          {
          #region Variables
          private int limit = 0;
          private double R,K,smallest,TR,SH,ER,Tpoints = 0;
          private DataSeries dsSIBuffer;
          #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.Green, "ASIndex"));
          Add(PeriodType.Day,1);

          }

          /// <summary>
          /// Calculates the indicator value(s) at the current index.
          /// </summary>
          protected override void OnBarUpdate()
          {
          if (CurrentBar != 0)
          return;

          if (CurrentBar < 2880)
          return;

          double num1 = High[0] - Closes[1][0];
          double num2 = Low[0] - Closes[1][0];
          double num3 = High[0] - Low[0];

          double TR= num1;
          if (TR < num2)
          TR = num2;
          else if(TR < num3)
          TR = num3;






          if (Closes[1][0] >= Low[0] && Closes[1][0] <= High[0])
          ER = 0;
          else
          {
          if (Closes[1][0] > High[0])
          ER = Math.Abs(High[0] - Closes[1][0]);
          if (Closes[1][0] < Low[0])
          ER = Math.Abs(Low[0] - Closes[1][0]);

          }

          K = Math.Max(Math.Abs(Highs[1][0]-Close[0]),Math.Abs(Lows[1][0]-Close[0]));

          SH = Math.Abs(Closes[1][0] - Opens[1][0]);
          R = TR - 0.5*ER+0.25*SH;

          if (R==0)
          dsSIBuffer.Set(0);
          else
          dsSIBuffer.Set(50*(Close[0] - Closes[1][0] + 0.5*(Close[0]-Open[0]) + 0.25*(Closes[1][0]-Opens[1][0])) *(K/30000)/R);

          ASIndex.Set(dsSIBuffer[1] + dsSIBuffer[0] );

          }

          #region Properties
          /// <summary>
          /// </summary>
          [Browsable(false)]
          [XmlIgnore()]
          public DataSeries ASIndex
          {
          get { return Values[0]; }
          }



          #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 ASI[] cacheASI = null;

          private static ASI checkASI = new ASI();

          /// <summary>
          /// The ASI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
          /// </summary>
          /// <returns></returns>
          public ASI ASI()
          {
          return ASI(Input);
          }

          /// <summary>
          /// The ASI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
          /// </summary>
          /// <returns></returns>
          public ASI ASI(Data.IDataSeries input)
          {
          if (cacheASI != null)
          for (int idx = 0; idx < cacheASI.Length; idx++)
          if (cacheASI[idx].EqualsInput(input))
          return cacheASI[idx];

          lock (checkASI)
          {
          if (cacheASI != null)
          for (int idx = 0; idx < cacheASI.Length; idx++)
          if (cacheASI[idx].EqualsInput(input))
          return cacheASI[idx];

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

          ASI[] tmp = new ASI[cacheASI == null ? 1 : cacheASI.Length + 1];
          if (cacheASI != null)
          cacheASI.CopyTo(tmp, 0);
          tmp[tmp.Length - 1] = indicator;
          cacheASI = 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>
          /// The ASI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
          /// </summary>
          /// <returns></returns>
          [Gui.Design.WizardCondition("Indicator")]
          public Indicator.ASI ASI()
          {
          return _indicator.ASI(Input);
          }

          /// <summary>
          /// The ASI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
          /// </summary>
          /// <returns></returns>
          public Indicator.ASI ASI(Data.IDataSeries input)
          {
          return _indicator.ASI(input);
          }
          }
          }

          // This namespace holds all strategies and is required. Do not change it.
          namespace NinjaTrader.Strategy
          {
          public partial class Strategy : StrategyBase
          {
          /// <summary>
          /// The ASI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
          /// </summary>
          /// <returns></returns>
          [Gui.Design.WizardCondition("Indicator")]
          public Indicator.ASI ASI()
          {
          return _indicator.ASI(Input);
          }

          /// <summary>
          /// The ASI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
          /// </summary>
          /// <returns></returns>
          public Indicator.ASI ASI(Data.IDataSeries input)
          {
          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.ASI(input);
          }
          }
          }
          #endregion

          Comment


            #6
            Igal,

            Thanks for posting the code.

            Code:
            private DataSeries dsSIBuffer;
            Although, this is declared in the variables section, this could be the culprit since there is no initialize portion of it. You can test this placing Print statements before and after the first time you Set it in the OnBarUpdate().

            You will need to give a declaration to the dsSIBuffer in the initialize section of the code as you have with the ASI plot.
            Cal H.NinjaTrader Customer Service

            Comment


              #7
              Hi,

              I did as u told me and i am getting this error from the output :

              Error on calling 'OnBarUpdate' method for indicator 'ASI' on bar 10000: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

              Here is my code :

              //
              // Copyright (C) 2006, 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.Drawing;
              using System.Xml.Serialization;
              using NinjaTrader.Data;
              using NinjaTrader.Gui.Chart;
              #endregion

              // This namespace holds all indicators and is required. Do not change it.
              namespace NinjaTrader.Indicator
              {
              /// <summary>
              /// The ASI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
              /// </summary>
              [Description("The ASI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.")]
              public class ASI : Indicator
              {
              #region Variables
              private int limit = 0;
              private double R,K,smallest,TR,SH,ER,Tpoints = 0;
              private DataSeries dsSIBuffer;
              #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.Green, "ASIndex"));
              Add(PeriodType.Day,1);
              dsSIBuffer = new DataSeries(this);
              }

              /// <summary>
              /// Calculates the indicator value(s) at the current index.
              /// </summary>
              protected override void OnBarUpdate()
              {
              if (BarsInProgress != 0)
              return;

              if (CurrentBar < 10000)
              return;

              Print("Time : " + Time[0] + "CurrenBar : " + CurrentBar);

              double num1 = High[0] - Closes[1][0];
              double num2 = Low[0] - Closes[1][0];
              double num3 = High[0] - Low[0];

              double TR= num1;
              if (TR < num2)
              TR = num2;
              else if(TR < num3)
              TR = num3;






              if (Closes[1][0] >= Low[0] && Closes[1][0] <= High[0])
              ER = 0;
              else
              {
              if (Closes[1][0] > High[0])
              ER = Math.Abs(High[0] - Closes[1][0]);
              if (Closes[1][0] < Low[0])
              ER = Math.Abs(Low[0] - Closes[1][0]);

              }

              K = Math.Max(Math.Abs(Highs[1][0]-Close[0]),Math.Abs(Lows[1][0]-Close[0]));

              SH = Math.Abs(Closes[1][0] - Opens[1][0]);
              R = TR - 0.5*ER+0.25*SH;

              if (R==0)
              dsSIBuffer.Set(0);
              else
              dsSIBuffer.Set(50*(Close[0] - Closes[1][0] + 0.5*(Close[0]-Open[0]) + 0.25*(Closes[1][0]-Opens[1][0])) *(K/30000)/R);

              ASIndex.Set(dsSIBuffer[1] + dsSIBuffer[0] );

              }

              #region Properties
              /// <summary>
              /// </summary>
              [Browsable(false)]
              [XmlIgnore()]
              public DataSeries ASIndex
              {
              get { return Values[0]; }
              }



              #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 ASI[] cacheASI = null;

              private static ASI checkASI = new ASI();

              /// <summary>
              /// The ASI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
              /// </summary>
              /// <returns></returns>
              public ASI ASI()
              {
              return ASI(Input);
              }

              /// <summary>
              /// The ASI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
              /// </summary>
              /// <returns></returns>
              public ASI ASI(Data.IDataSeries input)
              {
              if (cacheASI != null)
              for (int idx = 0; idx < cacheASI.Length; idx++)
              if (cacheASI[idx].EqualsInput(input))
              return cacheASI[idx];

              lock (checkASI)
              {
              if (cacheASI != null)
              for (int idx = 0; idx < cacheASI.Length; idx++)
              if (cacheASI[idx].EqualsInput(input))
              return cacheASI[idx];

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

              ASI[] tmp = new ASI[cacheASI == null ? 1 : cacheASI.Length + 1];
              if (cacheASI != null)
              cacheASI.CopyTo(tmp, 0);
              tmp[tmp.Length - 1] = indicator;
              cacheASI = 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>
              /// The ASI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
              /// </summary>
              /// <returns></returns>
              [Gui.Design.WizardCondition("Indicator")]
              public Indicator.ASI ASI()
              {
              return _indicator.ASI(Input);
              }

              /// <summary>
              /// The ASI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
              /// </summary>
              /// <returns></returns>
              public Indicator.ASI ASI(Data.IDataSeries input)
              {
              return _indicator.ASI(input);
              }
              }
              }

              // This namespace holds all strategies and is required. Do not change it.
              namespace NinjaTrader.Strategy
              {
              public partial class Strategy : StrategyBase
              {
              /// <summary>
              /// The ASI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
              /// </summary>
              /// <returns></returns>
              [Gui.Design.WizardCondition("Indicator")]
              public Indicator.ASI ASI()
              {
              return _indicator.ASI(Input);
              }

              /// <summary>
              /// The ASI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
              /// </summary>
              /// <returns></returns>
              public Indicator.ASI ASI(Data.IDataSeries input)
              {
              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.ASI(input);
              }
              }
              }
              #endregion
              Last edited by igalml; 07-11-2013, 12:26 AM.

              Comment


                #8
                Hi Igal,

                Since, you are using a Multi-dataseries script, I would try using
                Code:
                if(CurrentBars[1] < 1)
                This will check all the data series current bar and not just the primary.

                Let me know if that works for you.
                Cal H.NinjaTrader Customer Service

                Comment


                  #9
                  Works.

                  ty

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by DavidHP, Today, 07:56 AM
                  1 response
                  5 views
                  0 likes
                  Last Post NinjaTrader_Erick  
                  Started by kujista, Today, 06:23 AM
                  3 responses
                  6 views
                  0 likes
                  Last Post kujista
                  by kujista
                   
                  Started by Mindset, Yesterday, 02:04 AM
                  2 responses
                  17 views
                  0 likes
                  Last Post NinjaTrader_RyanS  
                  Started by f.saeidi, Today, 08:03 AM
                  1 response
                  5 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Started by samish18, 04-17-2024, 08:57 AM
                  15 responses
                  52 views
                  0 likes
                  Last Post NinjaTrader_BrandonH  
                  Working...
                  X