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

Error on Calling OnBarUpdate

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

    Error on Calling OnBarUpdate

    Hi,

    I'm receiving an error on calling OnBarUpdate. I've the primary data series and a secondary data series as Volumetric bars. I believe it's the red highlighted portion which is causing the error. Could someone assist with a recommendation?

    Thanks!

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class Name : Strategy
    {

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "Name";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 3;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    UserDefinedVolume = 2000;
    }
    else if (State == State.Configure)
    {
    if (BarsPeriod.BarsPeriodType!=BarsPeriodType.Volumet ric)
    AddVolumetric(Instrument.FullName, BarsPeriodType.Volume, UserDefinedVolume, VolumetricDeltaType.BidAsk, 1);
    AddDataSeries(Data.BarsPeriodType.Tick, 1);
    }
    else if (State == State.DataLoaded)
    {
    // Indicators
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 60 || CurrentBars[1] < 1)

    return;

    if (Bars == null)
    return;


    for (int x=0; x<BarsArray.Length; x++)
    if (CurrentBars[x] < 5)
    return;

    int idx = BarsArray.Length - 1;
    NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe barsType1 = BarsArray[1].BarsType as NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe;

    if (barsType1 == null || BarsInProgress != idx)
    return;


    var deltaSL = Math.Abs(barsType1.Volumes[CurrentBar].DeltaSl);
    var deltaSH = Math.Abs(barsType1.Volumes[CurrentBar].DeltaSh);​

    #2
    Hello AgriTrdr,

    Thanks for your post.

    What exactly is the compile error message reporting so I may accurately assist?

    Ultimately, debugging prints should be used to determine how a strategy is processing logic if the strategy is throwing errors or not behaving as expected. Have you added debugging prints to your script to determine the source of the error?

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121

    I look forward to assisting further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Here's the error message that I'm receiving:

      Error on calling 'OnBarUpdate' method on bar 24: Index was outside the bounds of the array.

      Comment


        #4
        Sorry it should say bar 60, not bar 24.

        Error on calling 'OnBarUpdate' method on bar 60: Index was outside the bounds of the array.

        Comment


          #5
          Hello AgriTrdr,

          Thanks for your note.

          This error indicates that an invalid index is being used in your script.

          For example, with the error: "Error on calling 'OnBarUpdate' method on bar 0: 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". This message indicates an invalid index was used.

          As one example, calling Close[1] when CurrentBar is 0 (the very first bar) will cause this error. The reason for this is because there will not be a bar ago; essentially there is not enough data at bar 0 look back one bar.​ In this specific example, it would be needed to wait for the second bar to be built (from all series if there are multiple series added), to ensure that you have enough data to call a index 1 bar ago.

          This is especially important when multiple series are added to a single script as the data for all series may not start at the same time.

          Below is a link to the help guide on this specific example of an indexing error.
          https://ninjatrader.com/support/help...nough_bars.htm

          Also, below are links to the help guide on CurrentBar and CurrentBars.
          http://ninjatrader.com/support/helpG...currentbar.htm
          https://ninjatrader.com/support/help...urrentbars.htm

          Indexing errors can also occur when using an invalid index with arrays or collections. Below are public links to 3rd party educational sites on arrays and index out-of-range errors.
          https://www.dotnetperls.com/array

          Debugging prints should be added to the script to determine exactly where the invalid index is being used in your script. Below is a link to a forum post that demonstrates how to use prints to understand behavior.
          https://ninjatrader.com/support/foru...121#post791121

          Let me know if I may assist further.
          Brandon H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Human#102, Yesterday, 09:54 AM
          2 responses
          7 views
          0 likes
          Last Post Human#102  
          Started by Patlpp, 08-16-2021, 03:10 PM
          10 responses
          498 views
          0 likes
          Last Post Joerg
          by Joerg
           
          Started by AdamDJ8, 05-07-2024, 09:18 PM
          2 responses
          19 views
          0 likes
          Last Post -=Edge=-  
          Started by StockTrader88, 03-06-2021, 08:58 AM
          46 responses
          4,103 views
          3 likes
          Last Post tradgrad  
          Started by usasugardefender, Today, 01:42 AM
          0 responses
          1 view
          0 likes
          Last Post usasugardefender  
          Working...
          X