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

Draw Line after Mouse Event -- Error

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

    Draw Line after Mouse Event -- Error

    Hello NT8 Support Team,

    The attached example code demonstrates use of Draw.Line() from OnBarUpdate and from the ChartControl_PreviewMouseLeftButtonDown event.

    Calling Draw.Line() from inside the mouse event generates the error "Index was out of range, Must be non-negative and less than the size of the collection." However, Draw.Line() works OK from non-event code like OnBarUpdate. I have tried wrapping the call inside its own ChartControl dispatcher in case there was a threading issue, but the error persists.

    This is identical to a problem reported by another user for the NT8 beta (http://ww.ninjatrader.com/support/fo...d.php?p=453711).

    Is there a solution or work-around? I want to draw a line in response to the user's mouse click on a specified price on the chart. Oddly, this code used to work, but has recently begun generating the above error.

    Thank you.
    Attached Files

    #2
    Originally posted by D Trader View Post

    Is there a solution or work-around? I want to draw a line in response to the user's mouse click on a specified price on the chart. Oddly, this code used to work, but has recently begun generating the above error.
    Have any specific versions in mind of when it used to work?

    Comment


      #3
      Hello,

      Thank you for the post.

      This would have changed in the later beta phase during beta 11: http://ninjatrader.com/support/helpG...s/8_0_0_11.htm

      The Implementation changes related to OnRender.

      This also applies to any non NinjaScript event such as a button event handler or OnRender. You would instead need to use bar Indexes rather than BarsAgo to access the data or do other operations.

      To access properties like the CurrentBar from OnRender or a Button event handler, you would need to make a private variable:

      private int myCurrentBar = 0;

      and assign it from OnBarUpdate:

      protected override void OnBarUpdate()
      {
      myCurrentBar = CurrentBar;
      }

      You could then use myCurrerntBar in the other places in the script where you may need to either get the CurrentBar index, or do some math to know a BarsAgo from the current index.

      You would need to use the methods like GetClose to access Price values http://ninjatrader.com/support/helpG...htsub=GetClose

      In the case of drawing an object, you may also try using a TriggerCustomEvent as that may, in turn, be more simple than converting to an index based system. Indexes would be suggested and preferred but if this worked for you it would be a solution as well: http://ninjatrader.com/support/helpG...ghtsub=trigger

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

      Comment


        #4
        Thanks for your reply, Ninjatrader_Jesse.

        However, I do not understand what you are trying to explain. My sample code does nothing in OnRender except capture the ChartScale object reference, which is needed to determine the chart Y value at the coordinates of the mouse click.

        Also, BarsAgo is defined as a local integer with an assigned value of 10 (to simplify the code). We can substitute a constant for the local BarsAgo variable and Draw.Line() will still generate an error when called after the mouse event -- I have already verified this.

        Can you please have another look at the code, and then address the issue with the call to Draw.Line() generating an error?

        FYI - this error occurs in version 8.0.6.1 64-bit (Multi-Broker). I referenced the earlier user's post dating back to the Beta version only to point out the similarity of the problem -- the error occurs only following a mouse event, but an identical call to Draw.Line() outside a mouse event will work OK. It would appear that the issue was never resolved, so my request was to find a work around.

        If I have misinterpreted your reply, please post a short code snippet to illustrate the solution you are proposing that addresses the Draw.Line() error.

        Many thanks.
        Last edited by D Trader; 05-14-2017, 02:12 PM.

        Comment


          #5
          Sledge, it worked as recently as a week ago using the same version of NT8 -- 8.0.6.1 64-bit (Multi-Broker). Apparently after recompiling other code during normal development, NT8 decided to surface this error for an existing call to Draw.Line() from inside the mouse event.

          FYI, I have tried placing the call inside its own thread using a ChartControl dispatcher. Also tried calling it after a brief delay using a timer that was started by the mouse event. However, in both cases the error still occurred.

          Thanks for asking.

          Comment


            #6
            Hello,

            Thank you for the reply.

            To answer the comment: the error occurs only following a mouse event, but an identical call to Draw.Line() outside a mouse event will work OK.

            Yes this is expected, I noted that this occurs in my post:
            This also applies to any non NinjaScript event such as a button event handler or OnRender.
            To correct this you would need to use one of the items I had posted which would be either using direct indexes or values or in the case of drawing objects to use TriggerCustomEvent:

            In the case of drawing an object, you may also try using a TriggerCustomEvent as that may, in turn, be more simple than converting to an index based system. Indexes would be suggested and preferred but if this worked for you it would be a solution as well: http://ninjatrader.com/support/helpG...ghtsub=trigger
            You could use the sample from the help guide to surround your Draw syntax, I can see this working when I tested it:

            Code:
            TriggerCustomEvent(o =>
            {
                 TestLine2 = Draw.Line( this, "TestLine2", false, BarsAgo, EntryPrice, 0, EntryPrice, Brushes.LightCoral, DashStyleHelper.Solid, 8);
            }, null);
            The BarsAgo concept does not work when you are not in the NinjaScript specific methods like OnBarUpdate. A Buttons event would be one example of where you cannot access NinjaScript properties successfully without instead using either TriggerCustomEvent or by accessing prices or other values using direct indexes. The prior post illustrates one way to transfer data from the methods like OnBarUpdate to other non-ninjascript areas like a button event handler or OnRender if needed.

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

            Comment


              #7
              Thanks for clarifying, NinjaTrader_Jesse.

              I finally got it... wrapped the call to Draw.Line() inside the TriggerCustomEvent, and everything works as expected.

              Appreciate your assistance.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by junkone, 04-21-2024, 07:17 AM
              10 responses
              148 views
              0 likes
              Last Post NinjaTrader_BrandonH  
              Started by tsantospinto, 04-12-2024, 07:04 PM
              6 responses
              100 views
              0 likes
              Last Post tsantospinto  
              Started by rocketman7, Today, 02:12 AM
              5 responses
              26 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by ZenCortexReal, Today, 08:54 AM
              0 responses
              1 view
              0 likes
              Last Post ZenCortexReal  
              Started by ZenCortexReal, Today, 08:52 AM
              0 responses
              0 views
              0 likes
              Last Post ZenCortexReal  
              Working...
              X