Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Object reference not set to an instance of an object just when optimizing.

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

    Object reference not set to an instance of an object just when optimizing.

    Hi,

    I have a strategy that runs OK in the chart, but it gets "Object reference" error when trying to optimize it.

    The error occurs here:
    protected override void OnBarUpdate()
    {

    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe barsType = Bars.BarsSeries.BarsType as
    NinjaTrader.NinjaScript.BarsTypes.VolumetricBars Type;

    Print("Step 1");
    long vVolumen = barsType.Volumes[CurrentBar].TotalVolume;

    Print("Step 2");
    double vDeltaPorcentual = barsType.Volumes[CurrentBar].GetDeltaPercent();

    Print("Step 3");
    long vDelta = barsType.Volumes[CurrentBar].BarDelta;

    }

    When running the strategy, it prints "1", "2" and "3" in the NinjaScript Output window for each candle in the chart, but when running the optimizer it prints the following in the NinjaScript Output window:


    Step 1
    Strategy 'PatronesOF': Error on calling 'OnBarUpdate' method on bar 1: Object reference not set to an instance of an object.


    Any suggestion?

    Thanks

    #2
    Hello javier.filgueira,

    The only object shown here is the barsType variable, are you using a primary series that is Volumetric?

    The other observation I can see is that you are misisng the conditions that are suggested in the help guide for working with volumetric data:

    Code:
    if (Bars == null)
    return;
    
    NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe barsType = Bars.BarsSeries.BarsType as
    NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe;
    
    if (barsType == null)
    return;

    https://ninjatrader.com/support/help...sub=volumetric



    I look forward to being of further assistance.

    Comment


      #3
      Hello Jesse, thank you for your post.

      The chart has just one volumetric bars added. The only way I found to access level 2 data is adding the barsType variable as shown in the example.

      I added the sentences in your example as shown:
      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;

      if (CurrentBars[0] < 1)
      return;

      if (Bars == null)
      return;

      double vTickSize = Bars.Instrument.MasterInstrument.TickSize;


      // Obtención de datos de la vela

      NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe barsType = Bars.BarsSeries.BarsType as
      NinjaTrader.NinjaScript.BarsTypes.VolumetricBarsTy pe;

      if (barsType == null)
      return;
      Print("1");
      long vVolumen = barsType.Volumes[CurrentBar].TotalVolume;
      Print("2");
      double vDeltaPorcentual = barsType.Volumes[CurrentBar].GetDeltaPercent();
      Print("3");
      long vDelta = barsType.Volumes[CurrentBar].BarDelta;

      When adding the strategy to the chart, it works OK and shows "1", "2" and "3" in the Output window, but when running an optimization it shows: "Error on calling 'OnBarUpdate' method on bar 0: Object reference not set to an instance of an object."

      Thanks in advance for any suggestion.

      Comment


        #4
        Hello javier.filgueira,

        Yes the error means that an object you used was null. Have you tried to copy the sample from the help guide exactly as its shown? I tried that and see it working in the analyzer.

        I look forward to being of further assistance.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        23 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        120 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        63 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        41 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        45 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X