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

congestion indicator conversion help

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

    congestion indicator conversion help

    HI i am converting simple indicator from pinescript that basically
    Congestion zone include at least 3 candle sticks that the next candle has an opening and closing price within the previous candle
    it gets compiled but i get error in logs
    error on calling onbarupdate method on bar 12, index was outside of the bounds of array
    how do i go about it?
    thank you
    Code:
    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion
    
    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class congestionbox : Indicator
        {
            private double[] closingPrices = new double[0];
    //        private double[] aCZ = new double[0];
            private bool congestionCondtion;
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "congestionbox";
                    Calculate                                    = Calculate.OnBarClose;
                    IsOverlay                                    = true;
                    DisplayInDataBox                            = true;
                    DrawOnPricePanel                            = true;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
                }
                else if (State == State.Configure)
                {
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBar < 10 )
                    return;
                #region congestion
    
                congestionCondtion = (Close[1] >= Low[2] && Close[1] <= High[2] && Open[1] >= Low[2] && Open[1] <= High[2]);
    
                Print("congestionCondtion"+congestionCondtion + Time[0]);
                if(congestionCondtion)
                    {
                        closingPrices.SetValue(Open[1], closingPrices.Length);
                        closingPrices.SetValue(Close[1], closingPrices.Length);
                    }
                    else if (closingPrices.Length < 6)
                    {
                        closingPrices = new double[0];
                    }
                    if (closingPrices.Length >= 6 && !congestionCondtion)
                {
                    double maxCP = closingPrices.Max();
                    double minCP = closingPrices.Min();
    
                    Draw.Rectangle(this, "CongestionZone", closingPrices.Length/2, minCP - TickSize, 1, maxCP + TickSize, Brushes.Blue);
                    closingPrices = new double[0];
                }
                #endregion
            }
        }
    }​
    https://www.tradingview.com/script/E...ngestion-Zone/
    Last edited by tkaboris; 11-03-2023, 05:28 AM.

    #2
    Hello tkaboris,

    You would need to debug the code and find out what specific line is having an error before we can assist. You can use comments or prints to find the line that is having an error.

    The error you are getting is a problem with something that uses a bars ago or indexes so those would be items to look into.
    JesseNinjaTrader Customer Service

    Comment


      #3
      hi i understand that. Evevrything works but issue is with this line
      private double[] closingPrices = new double[0];

      Do i need to make sure currentBar > this new array?
      if so how?

      Comment


        #4
        Hello tkaboris,

        You are making an array with a size of 0 elements which is not valid. You would need to go back and determine what you are trying to do with that code and use the appropriate type or code the array correctly for your use case. While we cannot assist with general C# concepts like arrays you can learn about using arrays in C# by using a search engine like google to search for C# double array examples.
        JesseNinjaTrader Customer Service

        Comment


          #5
          creating an array with 0 elements is valid.
          I basically want to create and initialize empty array, store values there and then make it to 0 again.
          Should i probably use Tseries double for that?

          Comment


            #6
            Hello tkaboris,

            A series stores 1 value per bar, if that is your goal you could use a series to store data. There are samples of how to create and use a series in the help guide.

            If you want to use an array I could only suggest debugging or looking at standard C# samples for arrays if you need further help with that part.

            The platform is letting you know the general C# error here which is " index was outside of the bounds of array". That would mean if one of the lines you found while debugging is the array then you are using that array incorrectly and passing an invalid index at some point. You can use prints to output how your logic is working to get a better idea of what is happening and then adjust your logic as needed.




            JesseNinjaTrader Customer Service

            Comment


              #7
              This is a conversion from small script from pinescript, only 12 lines of code. I posted original link on first post. Can you glance at that code to see why would my script error out? Creating an array with 0 elements is permissible.

              Comment


                #8
                Hello tkaboris,

                While our support cannot debug scripts for you we can advise how you go about doing that. The best way forward here is for you to find the specific line that is causing an error so you know how to address the problem. You can use Prints in the code to find the line or also comment out logic to find the specific line that is throwing the error.

                You can find some details on how to use prints here:




                JesseNinjaTrader Customer Service

                Comment


                  #9

                  Original source indicator assigns open and close to array.


                  congestionCondtion = close >= low[1] and close <= high[1] and open >= low[1] and open <= high[1]
                  if ( congestionCondtion == true )
                  array.push(aCZ, open)
                  array.push(aCZ, close)

                  else if( array.size(aCZ) < 6 )
                  array.clear(aCZ)
                  if( array.size(aCZ) >= 6 and congestionCondtion == false )
                  float maxCZ = array.max(aCZ)
                  float minCZ = array.min(aCZ)
                  box.new(bar_index - (array.size(aCZ) / 2), maxCZ, bar_index - 1, minCZ, bgcolor = ZoneColor, border_color = na)
                  array.clear(aCZ)


                  I was trying to mimick what he did with this code but have problem with bolded text area.

                  private double[] closingPrices = new double[0];
                  private double[] openingPrices = new double[0];​

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

                  region congestion

                  congestionCondtion = (Close[1] >= Low[2] && Close[1] <= High[2] && Open[1] >= Low[2] && Open[1] <= High[2]);
                  Print(closingPrices);
                  Print("congestionCondtion"+congestionCondtion + Time[0]);
                  if(congestionCondtion)
                  {
                  closingPrices[0] = Close[0];
                  openingPrices[0] = Open[0];


                  }

                  else if (closingPrices.Length < 6 && openingPrices.Length < 6)
                  {
                  closingPrices = new double[0];
                  openingPrices = new double[0];
                  }
                  if (closingPrices.Length >= 6 && openingPrices.Length >= 6 && !congestionCondtion)
                  {
                  double maxCP = closingPrices.Max();
                  double minCP = openingPrices.Min();

                  Draw.Rectangle(this, "CongestionZone", 3, minCP - TickSize, 1, maxCP + TickSize, Brushes.Blue);
                  closingPrices = new double[0];
                  openingPrices = new double[0];

                  }
                  #endregion
                  }​​

                  Comment


                    #10
                    Hello tkaboris,

                    Different platforms may use data or types differently, you would need to review how that script operates in the other platform and then re design the new item as needed in NinjaScript. NinjaScript works in a certain way and uses C# language so you may need to make changes to do similar tasks. Most other platforms code cannot be directly converted because they work differently. In most situations you would have to use whatever debugging tools the other platform has or its programming reference to see how each line of code should work. Once you know how the logic should work you can redesign it in NinjaScript to perform a similar action.
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      unfortunately this wasnt helpful.
                      Thanks

                      Comment


                        #12
                        c# isnit as flexible on arrays as PineScript

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by AaronKoRn, Today, 09:49 PM
                        0 responses
                        11 views
                        0 likes
                        Last Post AaronKoRn  
                        Started by carnitron, Today, 08:42 PM
                        0 responses
                        10 views
                        0 likes
                        Last Post carnitron  
                        Started by strategist007, Today, 07:51 PM
                        0 responses
                        11 views
                        0 likes
                        Last Post strategist007  
                        Started by StockTrader88, 03-06-2021, 08:58 AM
                        44 responses
                        3,980 views
                        3 likes
                        Last Post jhudas88  
                        Started by rbeckmann05, Today, 06:48 PM
                        0 responses
                        9 views
                        0 likes
                        Last Post rbeckmann05  
                        Working...
                        X