Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Index outside the bounds using volumetric bars in an indicator

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

    Index outside the bounds using volumetric bars in an indicator

    Hello,
    I am trying to work on an indicator using volumetric bars and I get "Indicator 'IndicatorName': Error on calling 'OnBarUpdate' method on bar 1: Index was outside the bounds of the array.

    The chart in which I will add the indicator is configured with volumetric bars, but I didn't find how to get information from these bars so I added another volumetric bars to the indicator code as it is indicated in the user guide, using the following code in OnBarUpdate():

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

    Added the bars, I was able to get the information that I need (using GetBidVolumeForPrice and GetAskVolumeForPrice).

    All the logic is inside OnBarUpdate(). The problem is when I assing the result of the indicator to Value[0]. When adding the indicator to the chart, it doesn't work. If I do not include the assignment of the result to Value[0], the indicator looks fine in the chart, but I understand that the indicator is not returning any result, so I won't be able to use the indicator in a strategy.

    I'd need to know how to pass the result to Value[0] so I can use the indicator in a strategy.

    The indicator code is the following:
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Description here";
    Name = "Name here";
    Calculate = Calculate.OnBarClose;
    IsOverlay = true;
    IsSuspendedWhileInactive = true;
    ...
    ...
    ...

    }
    }

    protected override void OnBarUpdate()
    {

    (Variables assignments here...)

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

    // Init

    (some logic here)

    for (double i = Low[0]; i < High[0]; i = i + vTickSize)
    {

    vBidVol = barsType.Volumes[CurrentBar].GetBidVolumeForPrice(i);
    vAskVol = barsType.Volumes[CurrentBar].GetAskVolumeForPrice(i + vTickSize);

    (som logic and calculation here)
    }

    // Show the indicator in the chart

    if (P_Show_Result)
    {
    if ( vAlcista == true
    && vAskImbalances != 0)
    {
    Draw.Text(this, "Imbalances" + CurrentBar, false, "Imb: " + Convert.ToString(vAskImbalances), 0, Low[0], -50,
    Brushes.Green, TextFont, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0);
    }

    if ( vAlcista == false
    && vBidImbalances != 0)
    {
    Draw.Text(this, "Imbalances" + CurrentBar, false, "Imb: " + Convert.ToString(vBidImbalances), 0, High[0], +50,
    Brushes.Red, TextFont, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0);
    }
    }

    // Return value

    Value[0] = vAlcista ? vAskImbalances : vBidImbalances * (-1);

    }

    #region Properties

    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public Series<double> vResultado
    {
    get { return Values[0]; }
    }

    (Parameters here)



    Thanks!

    #2
    Hello javier.filgueira, thanks for your question.

    You should run the script with Visual Studio attached to find where exactly the script fails:

    https://ninjatrader.com/support/help..._debugging.htm

    If you attach Visual Studio to the NinaTrader process and run the script, Visual Studio will break at the line the exceptions happens on. I can not tell what is wrong from the snippet, unfortunately.

    I look forward to hearing of your results.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris,

      Thank you for your response.

      The error is exactly when it tries to execute:
      Value[0] = vAlcista ? vAskImbalances : vBidImbalances * (-1);

      If I put Print("something") just before and after this line of code, it prints the first string but not the second. If I comment this line of code, the indicator works right showing the correct output in the chart.

      This is the complete code:
      namespace NinjaTrader.NinjaScript.Indicators
      {
      /// <summary>
      /// Imbalances muestra la cantidad de imbalances en la vela actual.
      /// </summary>
      ///
      public class Imbalances : Indicator
      {
      private Brush downBrush = Brushes.DimGray;
      private Brush upBrush = Brushes.DimGray;
      private TextPosition textBoxPosition = TextPosition.BottomRight;
      private Brush textBrush = Brushes.DimGray;
      private int vBidImbalances = 0;
      private int vAskImbalances = 0;

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = "Muestra la cantidad de Imbalancess que presenta la vela";
      Name = "Imbalances";
      Calculate = Calculate.OnBarClose;
      IsOverlay = true;
      IsSuspendedWhileInactive = true;
      P_Imbalances_Ratio = 3;
      P_Min_Delta_Imbalances = 50;
      P_Min_Imb_Qty = 3;
      P_Show_Imb_Count = true;
      TextFont = new Gui.Tools.SimpleFont() { Size = 12, Bold = true };

      }
      }

      protected override void OnBarUpdate()
      {
      if (CurrentBar < 1)
      return;

      bool vAlcista = true;
      double vTickSize = Bars.Instrument.MasterInstrument.TickSize;
      long vBidVol = 0;
      long vAskVol = 0;

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

      // Inicialización

      if (Open[0] > Close[0])
      {
      vAlcista = false;
      }

      vBidImbalances = 0;
      vAskImbalances = 0;


      // Calculo cantidad de imbalances

      for (double i = Low[0]; i < High[0]; i = i + vTickSize)
      {

      vBidVol = barsType.Volumes[CurrentBar].GetBidVolumeForPrice(i);
      vAskVol = barsType.Volumes[CurrentBar].GetAskVolumeForPrice(i + vTickSize);

      if (vBidVol - vAskVol >= P_Min_Delta_Imbalances)
      {
      if (vAskVol != 0)
      {
      if (vBidVol / vAskVol >= P_Imbalances_Ratio)
      {
      vBidImbalances = vBidImbalances +1;
      }
      }
      else
      {
      vBidImbalances = vBidImbalances +1;
      }
      }

      if (vAskVol - vBidVol >= P_Min_Delta_Imbalances)
      {
      if (vBidVol != 0)
      {
      if (vAskVol / vBidVol >= P_Imbalances_Ratio)
      {
      vAskImbalances = vAskImbalances +1;
      }
      }
      else
      {
      vAskImbalances = vAskImbalances +1;
      }
      }

      }

      if (vAskImbalances < P_Min_Imb_Qty)
      {
      vAskImbalances = 0;
      }

      if (vBidImbalances < P_Min_Imb_Qty)
      {
      vBidImbalances = 0;
      }

      // Muestro el resultado en el chart

      if (P_Show_Imb_Count)
      {
      if ( vAlcista == true
      && vAskImbalances != 0)
      {
      Draw.Text(this, "Imbalances" + CurrentBar, false, "Imb: " + Convert.ToString(vAskImbalances), 0, Low[0], -50,
      Brushes.Green, TextFont, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0);
      }

      if ( vAlcista == false
      && vBidImbalances != 0)
      {
      Draw.Text(this, "Imbalances" + CurrentBar, false, "Imb: " + Convert.ToString(vBidImbalances), 0, High[0], +50,
      Brushes.Red, TextFont, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 0);
      }
      }

      // Print("1");
      // Value[0] = vAlcista ? vAskImbalances : vBidImbalances * (-1);
      // Print("2");

      // if(vResultado[0] != 0) { Print("CurrentBar: " + CurrentBar + " Time: " + Bars.GetTime(CurrentBar) + " vResultado: " + vResultado[0]); }


      }

      #region Properties

      [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
      [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
      public Series<double> vResultado
      {
      get { return Values[0]; }
      }

      [Range(0, int.MaxValue), NinjaScriptProperty]
      [Display(ResourceType = typeof(Custom.Resource), Name = "Imbalances ratio", GroupName = "NinjaScriptParameters", Order = 0)]
      public int P_Imbalances_Ratio
      { get; set; }

      [Range(0, int.MaxValue), NinjaScriptProperty]
      [Display(ResourceType = typeof(Custom.Resource), Name = "Minimum delta for imbalance", GroupName = "NinjaScriptParameters", Order = 1)]
      public int P_Min_Delta_Imbalances
      { get; set; }

      [Range(1, int.MaxValue), NinjaScriptProperty]
      [Display(ResourceType = typeof(Custom.Resource), Name = "Minimum imbalances quantity", GroupName = "NinjaScriptParameters", Order = 2)]
      public int P_Min_Imb_Qty
      { get; set; }

      [Display(ResourceType = typeof(Custom.Resource), Name = "Show Imbalancess count", Description = "Set true to display on chart the count of imbalances", GroupName = "NinjaScriptGeneral", Order = 3)]
      public bool P_Show_Imb_Count
      { get; set; }

      [Display(ResourceType = typeof(Custom.Resource), Name = "TextFont", Description = "select font, style, size to display on chart", GroupName = "NinjaScriptGeneral", Order = 4)]
      public Gui.Tools.SimpleFont TextFont
      { get; set; }


      #endregion
      }
      }


      Thanks,


      Comment


        #4
        Uncommenting the following Print sentences:
        Print("1");
        Value[0] = vAlcista ? vAskImbalances : vBidImbalances * (-1);
        Print("2");

        This is what happens (in Ninjacript Output):
        1
        Indicator 'Imbalances': Error on calling 'OnBarUpdate' method on bar 1: Index was outside the bounds of the array.

        Thanks.

        Comment


          #5
          Hello javier.filgueira,

          Value is being indexed, but it does not look like you are adding a plot, so there would not be a plot Value to assign unless a plot is is added with AddPlot.

          AddPlot - https://ninjatrader.com/support/help...8/?addplot.htm

          We look forward to assisting.
          JimNinjaTrader Customer Service

          Comment


            #6
            Hi Jim.

            It worked. THANKS!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Stephen Trading, Yesterday, 10:55 AM
            1 response
            26 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by Coopero, Yesterday, 12:25 PM
            2 responses
            21 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by Hyper, Yesterday, 09:56 AM
            3 responses
            20 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by bootize, Yesterday, 08:37 AM
            3 responses
            25 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by Newtrader101, 06-17-2020, 11:01 AM
            11 responses
            53 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Working...
            X