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

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 jaybedreamin, Today, 05:56 PM
              0 responses
              7 views
              0 likes
              Last Post jaybedreamin  
              Started by DJ888, 04-16-2024, 06:09 PM
              6 responses
              18 views
              0 likes
              Last Post DJ888
              by DJ888
               
              Started by Jon17, Today, 04:33 PM
              0 responses
              4 views
              0 likes
              Last Post Jon17
              by Jon17
               
              Started by Javierw.ok, Today, 04:12 PM
              0 responses
              12 views
              0 likes
              Last Post Javierw.ok  
              Started by timmbbo, Today, 08:59 AM
              2 responses
              13 views
              0 likes
              Last Post bltdavid  
              Working...
              X