• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How is this array out of range???

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

    How is this array out of range???

    I am having no problems with this in ninja 7, but ninja 8 refuses to work with this. I am triggering draw objects via the OnRender method. I have the following code:
    Code:
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
            {
                if(Time.Count < Count - 1) { Print("Data array not initialized"); return; }
    
    int bpnt = CurrentBar-Bars.GetBar(bTime);
                            int cpnt = CurrentBar-Bars.GetBar(cTime);
    int apnt = CurrentBar-Bars.GetBar(aTime);
    Print("apnt: "+apnt.ToString()+"   bpnt: "+bpnt.ToString()+"   cpnt: "+cpnt.ToString()+"   element: "+(bpnt+(int)Math.Round((apnt-bpnt)/2.0)).ToString()+"   T: "+Time.Count.ToString());
                                DateTime bpt1=Time[bpnt+(int)Math.Round((apnt-bpnt)/2.0)];
    }
    When it attempts to capture this DateTime, I receive the following message:
    apnt: 162 bpnt: 147 cpnt: 132 element: 155 T: 6784
    Indicator 'EnigmaV009': Error on calling 'OnRender' method on bar 6783: 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 tested to make sure the Time array had enough elements. I checked to see how many elements it had. I checked to make sure it wasn't attempting to index beyond that range. Why is this telling me I am trying to access an index out of range?

    #2
    Hello,

    Thank you for the post.

    Yes, in this case, this would be expected. When you are working outside of the event-driven NinjaScript overrides you would need to use the Index of a bar instead of a BarsAgo. This also requires using the Get methods instead of using [0] or a BarsAgo.

    There is an example on the following page showing how to access the Time of a bar from OnRender: https://ninjatrader.com/support/help...ghtsub=gettime

    When using OnRender it is important to remember that you are no longer working from bar 0 to the current bar, you are only working for the visible bars on the chart. The sample in the help guide page from above shows how to correctly reference the span of bars being rendered currently, please see the "for" loop in the sample.

    I would also likely advise against using DrawingTools in OnRender, generally, you should replace DrawingObjects with rendering using the RenderTarget to take advantage of the efficient rendering. You can see the SampleCustomRender for example of drawing Text or other objects from OnRender.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      NinjaTrader_Jesse , would this also apply to Ninja 7, as I am doing the same thing in Ninja 7...even though there have been no problems?

      Comment


        #4
        Hello Antny,

        No this is a different process in NT8.

        NT8 is now multi-threaded and also uses SharpDX for rendering so quite a lot has changed in regard to rendering. NT7's Plot override worked more or less like the other overrides in the script. In NT8, when you change over to OnRender it does work differently in respect to BarsAgo and how to access data. You can see the SampleCustomRender for examples of general rendering from OnRender. You can also see the Pivots indicator for a realworld use that involves plots and data being used. In this case, you will have two separate syntax's used between 7 and 8.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Thank you for the clarification!

          Comment


            #6
            NinjaTrader_Jesse , About the SharpDX use...would you still recommend using the SharpDX rendering when the objects need to remain locked to the chart's price and time coordinates? Just want to make sure this is the best route to pursue before I invest the time in learning it.

            Comment


              #7
              Hello,

              Thank you for the reply.

              If the intention is to draw a large number of items for every bar then using OnRender and SharpDX would be more efficient. OnRender simply renders visuals where you want, so if you wanted something to be locked to bar and price you could certainly do that from OnRender by converting a Time and Price to X and Y coordinates.

              Really it comes down to, are you doing this for efficiency? If yes, OnRender should be used, if not you should likely use OnBarUpdate with Drawing tools as that is more simple. The other consideration would be if you are programmatically accessing drawing objects to get values. OnRender is just showing data in a visual way so there are no tags or anything else associated with the drawing which provides a performance boost, is this going to affect your plans? If not OnRender could be used to visualize the data you have in an efficient way, you would just need to modify your existing code to be OnRender code if that is the case.

              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                NinjaTrader_Jesse , I guess I need to be a bit more specific. As it is now, I have been using DrawLine, DrawTriangle, and DrawText to render the objects. I am capturing the coordinates of the mouse, and updating the objects as the mouse moves. When I arrive at the coords I desire, I capture the mouse left button click which is where I lock in that anchor of the object. Once the entire object is plotted on the chart, usually involving several mouse clicks, then a new object can be started.

                So...what I am unsure of is, would there be a problem if the chart is scrolled to the left or right, which would move the object off the chart? For instance....let's say I have a triangle draw on the chart, which spans 150 bars. If I scroll the chart so that 60 of those bars are obscured...would I still be able to use the sharpDX library to render the object on the chart correctly...since one of the points is technically no longer on the chart?

                Comment


                  #9
                  Hello,

                  Thank you for the reply.

                  Yes, this helps in explaining your question.

                  In this case, you can either combine using the mouse and DrawingTools or Renderings from OnRender, that would really be up to how you want to approach this. In regard to scrolling, if you do choose to use OnRender for drawing you would need to store the data for what should be rendered and then reference that data based on what bars are currently visible. The HelpGuide sample I previously linked shows the for loop for the span of bars that are currently visible. As I noted previously there is no tagging or anything else associated with renderings (see SampleCustomRender indicator) and you are simply drawing the raw data. If you want to take advantage of the using OnRender rendering, you would need to track what should be drawn for the currently visible bars (see pivots for real-world example with plot use).

                  Drawing objects do this for you because they are an object and have a tag, anchor points etc and use OnRender in their code. If you aren't seeing a performance impact by what you are doing now, I would likely suggest sticking with using DrawingTools to keep it simple. Also if you are only using OnRender for mouse purposes, that would likely be another reason to use DrawingObjects to simplify your code. If you want more control or even custom drawings you could use the rendering from OnRender instead of drawing objects but that would require more overall logic.

                  Please let me know if I may be of additional assistance.
                  JesseNinjaTrader Customer Service

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by kitwit, Yesterday, 11:59 AM
                  1 response
                  6 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by otislauwaert, Today, 02:02 PM
                  1 response
                  5 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by rafleischman, 10-18-2018, 12:11 PM
                  2 responses
                  22 views
                  0 likes
                  Last Post Alfred
                  by Alfred
                   
                  Started by dadarara, Yesterday, 02:42 AM
                  1 response
                  9 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by Dan_D_Trader, Today, 04:46 PM
                  0 responses
                  2 views
                  0 likes
                  Last Post Dan_D_Trader  
                  Working...
                  X