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

Trouble Coding Multi-Series & Multi-Time Frame Indicator

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

    Trouble Coding Multi-Series & Multi-Time Frame Indicator

    I'm coding my first multi-series and multi-time frame indicator. The indicator works for some conditions and fails for others, and I'm trying to understand why it fails and what to do about it. The cs file for my indicator is attached as aaaWIP.cs.

    The indicator adds a secondary series with a 1-minute period in Initialize(). As currently coded, the indicator works when it's added to a 1-minute chart or a 2-minute chart, but it fails when it's added to a 3-minute or longer period chart. When the indicator fails, the error message reads "Error on calling 'OnBarUpdate' method for indicator 'aaaWIP' 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."

    I'd like to understand the following things.

    1. Why does the indicator work for a 2-minute chart, but fail for a 3-minute chart?
    2. What code do I need to add to my script to eliminate the error when the indicator is added to 3-minute or longer period charts?

    Thanks in advance for help you might provide.
    Attached Files

    #2
    Change:

    Code:
    	if (BarsInProgress == 1)
    			{
    				Print( Time[0] + " " + Open[0] + " " + High[0] + " " + Low[0] + " " + Close[0]);
    				Value.Set( Close[0] );
    			}
    to:

    Code:
    	if (BarsInProgress == 1)
    			{
    				Print( Times[1][0] + " " + Opens[1][0] + " " + Highs[1][0] + " " + Lows[1][0] + " " + Closes[1][0]);
    				Value.Set( Closes[1][0] );
    			}
    And re-read the documentation on this....

    It appears to be a freak accident you hit this... But in data processing you probably his the 2nd series first before the first series... all bets are off if you do other variations (3/4/5/6/7 minutes).

    Well that's my guess and experience without testing this...

    Comment


      #3
      Hello NtFan,

      Thanks for your post and welcome to the forums!

      In looking at your code the issue is that you are trying to process data that is not yet available. Keep in mind that when added to a chart the indicator will begin processing data from the very first bar loaded x days ago. As these bars are historical the processing will be the same as CalculateOnbarClose = true meaning that the OBU will not trigger until the end of the bar. You will want to ensure that enough bars have passed through the indicator before trying to access what you need. For example, add this line as the first line in OnBarUpdate() method:

      if (CurrentBars[0] < BarsRequired || CurrentBars[1] < BarsRequired) return; // wait until enough bars loaded in each dataseries to process.

      CurrentBars[] hold the bar number of the dataseries, 0 = chart, 1 = 1st added dataseries.

      Here are the helpguide references:


      http://ninjatrader.com/support/helpG...rsrequired.htm NOTE: You do not need to use BarsRequired, you could use integer values of your choosing, I just used it for convenience.

      If you haven't already, please do give this section a complete review: http://ninjatrader.com/support/helpG...nstruments.htm
      Paul H.NinjaTrader Customer Service

      Comment


        #4
        Thank you sledge & NinjaTrader_Paul for your replies, and for the warm welcome. Sure enough, adding the CurrentBars check at the top of OBU eliminated the trouble. In my case this code reads as shown below.

        if ( (CurrentBars[0] == 0) || (CurrentBars[1] == 0) ){
        return;
        }

        Thanks again!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by jaybedreamin, Today, 05:56 PM
        0 responses
        2 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
        1 view
        0 likes
        Last Post Jon17
        by Jon17
         
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        6 views
        0 likes
        Last Post Javierw.ok  
        Started by timmbbo, Today, 08:59 AM
        2 responses
        10 views
        0 likes
        Last Post bltdavid  
        Working...
        X