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

onbarupdate issues

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

    onbarupdate issues

    I know this is a common problem and has come up a lot on these forums but I'm
    still not sure what to put for currentbar count.

    The error msg in log is:

    You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.


    i have added the daily data series to state.configure

    AddDataSeries(BarsPeriodType.Day, 1);


    and declared 2 doubles which are the previous days high and low:

    double pdh = Highs[1][1];
    double pdl = Lows[1][1];

    if(CurrentBars[0] < 5)
    return;

    '//trigger

    if(Close[0] > pdh)
    { pdh breakout alert}

    for the main data series will be using values under 1 day (usually 1m or 5m). Knowing this how to i need to configure the:

    if(CurrentBars[0] < 5)

    so that this indicator will function as intended. Please assist.
    return;
    Last edited by gordongekko; 12-07-2017, 05:32 PM.

    #2
    try this

    Code:
              
               double pdh ;
                double pdl ;
         OnBarUpdate:   
                if(CurrentBars[0] < 5)
                return;
                pdh = Highs[1][1];
                pdl = Lows[1][1];
                
    '//trigger
    
                if(Close[0] > pdh)
                { pdh breakout alert}

    Comment


      #3
      Hello gordongekko,

      Thanks for your post.

      You are adding a data series however you are not accounting for this in your CurrentBars check. CurrentBars[0] points to the charts bars, CurrentBars[1] points to the added dataseries.

      You are trying to access the previous day high/low with Highs[1][1] and Lows[1][1] after the 5 bars loaded on the chart bars which are 1m or 5m, this is not enough bars for the daily bars to load

      Try changing your CurrentBars check to something like:

      if (CurrentBars[0] < 5 || CurrentBars[1] < 1)
      return;


      The statement would need to be the first one in the OnBarUpdate() as you don't want to process anything below that line until you have the correct number of bars loaded.

      Reference: https://ninjatrader.com/support/help...urrentbars.htm
      Paul H.NinjaTrader Customer Service

      Comment


        #4
        Thanks. This seems so obvious now. For some reason I was overlooking the [] which should have clued me into the fact that it was referencing that bar series from the array. I was thinking the 0 was the current bar of the data series and it was loading both.

        Is there a way to have this send out an alert only when the price closes above or below this line and not every bar that is above or below the previous days high without using a timer.

        if(Close[0] > pdh)
        { pdh breakout alert}

        When i use this the alert will be sent out at the close of every bar that is above the previous daily high but I only want the alert to go out on the first bar to close above the previous days high on the main data series. Can cross above/below be used for price like it can for an indicator? What is the best way to accomplish this?
        Last edited by gordongekko; 12-08-2017, 09:21 AM.

        Comment


          #5
          Hello gordongekko,

          Thanks for your reply.

          Yes, you can create a boolean variable that is true or false only and can test for that as well as set to what you need.

          You can use CrossAbove() or CrossBelow() as needed.

          For example:

          if (CrossAbove(Close, pdh, 1) && FirstCrossAbove)
          {
          // pdh breakout alert
          FirstCrossAbove = false; // set bool to false to prevent re-entry
          }


          You would also need to reset the bool to true at some point in time to again enable the condition.
          Paul H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by maybeimnotrader, Yesterday, 05:46 PM
          2 responses
          20 views
          0 likes
          Last Post maybeimnotrader  
          Started by adeelshahzad, Today, 03:54 AM
          5 responses
          32 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by stafe, 04-15-2024, 08:34 PM
          7 responses
          32 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by merzo, 06-25-2023, 02:19 AM
          10 responses
          823 views
          1 like
          Last Post NinjaTrader_ChristopherJ  
          Started by frankthearm, Today, 09:08 AM
          5 responses
          22 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Working...
          X