• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using once custom indicator inside other my indicator

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

    Using once custom indicator inside other my indicator

    Hello
    I've got one custom indicator - USCPIMonthly
    The code looks like this:
    Code:
    protected override void OnBarUpdate()
            {
                DateTime dtNow = Time[0];
    			int index = _helper.GetDateIndex(dtNow);
    			if(index != null)
    			{
    				for(int i = 0; i < _helper.Series.Count; ++i)
    				{
    					Values[i].Set( _helper.Series[i].Values[index] );
    				}
    			}
            }
    - so nothing special here - just getting the value for each date and setting to indicator

    Here I'm trying to reference it in another my indicator
    Code:
    protected override void OnBarUpdate()
            {
    			if(USCPIMonthly().Count > 0)
    			{
    				double currCpi = USCPIMonthly()[0];
    				Print("USCPIMonthly()[0] = " + currCpi);
    				double currCpiPrev = USCPIMonthly()[1]; // EXCEPTION HERE!
    				Print("USCPIMonthly()[1] = " + currCpiPrev);
    				
    				Values[0].Set( (currCpi - currCpiPrev)/currCpiPrev * 100 );
    				
    			}
            }
    The problem is that I can only reference the first value - but when I'm trying to get the previous one I'm getting an exception that array is out of range etc.
    My question: why I cannot get the indicator value for the previous bar?

    #2
    Hello Globus000,

    If in the USCPIMonthly indicator there is a plot added with the Add() call in Initialize, this plot will have a value for each bar. If you call a bar that doesn't have a value set, the close price should be returned instead. (.ContainsValue(int barsAgo) is used to check if there was a value set or not)
    http://ninjatrader.com/support/helpG...ries_class.htm


    Was there a plot added to the USCPIMonthly indicator in Initialize that is being set?
    Is there an update being called that would cause that plot to recalculate when called for previous bars?

    (As an example try calling SMA(14)[1]. This script should be able to return the previous bar without issue.)
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Yes, the plot was added on initialize, but how to call the update in order to recalc the indicator and get values for barsAgo?

      Comment


        #4
        Hello Globus000,

        After each bar is set in OnBarUpdate, this does not get unset automatically. The data series will continue to hold the set value for each bar historically. You will not have to update the script to get historical values.

        I am asking if there is an update that might be unsetting these values, so that I may direct you to removing this update which would be causing this issue.

        May I confirm that you have tried calling the SMA(14)[1] and this did not work as expected?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello

          Sorry, for responding with delay.
          Regarding SMA - I'm facing the same error - here is the code:
          protected override void OnBarUpdate()
          {
          if(USCPIMonthly().Count > 0)
          {
          double currCpi = USCPIMonthly()[0];
          Print("USCPIMonthly()[0] = " + currCpi);
          double prevSma = SMA(14)[1];
          Print("SMA(14)[1] = " + prevSma);
          double currCpiPrev = USCPIMonthly()[1];
          Print("USCPIMonthly()[1] = " + currCpiPrev);

          Values[0].Set( (currCpi - currCpiPrev)/currCpiPrev * 100 );

          }
          }
          And here is the output:
          USCPIMonthly()[0] = 237.423
          Error on calling 'OnBarUpdate' method for indicator 'USCPIPercMonthly' on bar 0: 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.

          Comment


            #6
            Hello Globus000,

            To check one bar ago, there must be a bar one bar ago.

            Try:

            if (CurrentBar > 0)
            {
            Print(SMA(14)[1]);
            }

            Does this give you an error?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi Chelsea,

              Yes, you're absolutely right - I missed this. Now it works fine. Thanks!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by chrismcdermut, Yesterday, 01:46 PM
              3 responses
              19 views
              0 likes
              Last Post MojoJojo  
              Started by Bricolico, Yesterday, 09:28 PM
              0 responses
              5 views
              0 likes
              Last Post Bricolico  
              Started by Tradwell385, Yesterday, 08:51 PM
              0 responses
              7 views
              0 likes
              Last Post Tradwell385  
              Started by dvcpro4, Yesterday, 07:08 PM
              0 responses
              21 views
              0 likes
              Last Post dvcpro4
              by dvcpro4
               
              Started by pennywise, Yesterday, 06:40 PM
              0 responses
              8 views
              0 likes
              Last Post pennywise  
              Working...
              X