Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Get Bar Value from Mouse Click Event on Indicator

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

    Get Bar Value from Mouse Click Event on Indicator

    Hi,

    I want to get the Bar value of where mouse is clicked and have to draw some line using Bars High and Low from my Indicator. I have written below to code to access the Hing and Low. But It is not working and throwing some exception. I need your help to fix this.

    CODE :
    Code:
    protected override void OnStateChange()
    {[INDENT]if (State == State.Configure)
    {
    }else if (State == State.Historical)
    {
    
    }else if (State == State.DataLoaded)
    {
    ChartControl.ChartPanels[0].MouseDown += MouseClicked;
    }
    else if (State == State.Terminated)
    {
    ChartControl.ChartPanels[0].MouseDown -= MouseClicked;
    }[/INDENT]
     }
    protected void MouseClicked(object sender, MouseButtonEventArgs e)
    {
    try{[INDENT]DateTime slotTimeX = ChartControl.GetTimeByX((int)ChartControl.MouseDow nPoint.X);
    int barsAgo = CurrentBar - Bars.GetBar(slotTimeX);
    Print(barsAgo);
    Print (High[barsAgo]);
    Print (Low[barsAgo]);[/INDENT]
     }catch(Exception exc)
    {[INDENT]Print(exc);[/INDENT]
     }
    //Print((e.MiddleButton == MouseButtonState.Pressed).ToString());
    }

    Exception :

    System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
    at System.ThrowHelper.ThrowArgumentOutOfRangeExceptio n(ExceptionArgument argument, ExceptionResource resource)
    at NinjaTrader.Data.BarsSeries.GetClose(Int32 index)
    at NinjaTrader.Data.Bars.GetClose(Int32 index)
    at NinjaTrader.NinjaScript.PriceSeries.get_Item(Int32 barsAgo)
    at NinjaTrader.NinjaScript.Indicators.SolzzSnRIndicat or.MouseClicked(Object sender, MouseButtonEventArgs e)



    Attached Files

    #2
    Hello nandhumca,

    Thanks for your post.

    I see that you are printing High[barsAgo] and Low[barsAgo] in your script outside of a bars processing method, such as OnBarUpdate().

    To use data inside a button/WPF event you would need to surround that code in a TriggerCustomEvent so that internal NinjaScript indexes and pointers are correctly set prior to processing user code triggered by your custom event. For example, the code may look something like this.

    Code:
    try
    {
      DateTime slotTimeX = ChartControl.GetTimeByX((int)ChartControl.MouseDow nPoint.X); 
      int barsAgo = CurrentBar - Bars.GetBar(slotTimeX);
    
      TriggerCustomEvent(o =>
      {
        Print("High barsAgo: " + High[barsAgo]);  
        Print("Low barsAgo: " + Low[barsAgo]);
      }, null);
    
    }
    See this help guide documentation for more information: https://ninjatrader.com/support/help...ustomevent.htm

    Let us know if we may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Thanks a lot. it is working .

      Comment


        #4
        Hi Brandon,

        I linked below method for MouseDown event and trying to draw a small line in chart. it is working , but it is taking very long time to print the line on chart. Kindly share your input on this. I want the line to be drawn immediately as and when the mouse clicked.

        Code:
        protected void MouseClicked(object sender, MouseButtonEventArgs e)
        {
        try{
        if(e.MiddleButton == MouseButtonState.Pressed){
        DateTime slotTimeX = ChartControl.GetTimeByX((int)ChartControl.MouseDow nPoint.X);
        int barsAgo = CurrentBar - Bars.GetBar(slotTimeX);
        Print(barsAgo);
        if(barsAgo>=0){
        double low = 0;
        double high = 0;
        TriggerCustomEvent(o =>
        {
        // Print("Time : " + slotTimeX.ToString());
        // Print("High barsAgo: " + High[barsAgo]);
        // Print("Low barsAgo: " + Low[barsAgo]);
        high = High[barsAgo];
        low = Low[barsAgo];
        Draw.Line(this, "myLine", true, barsAgo, high, barsAgo-1, high, Brushes.Pink, DashStyleHelper.Solid, 3);
        }, null);
        }
        }
        }catch(Exception exc)
        {
        Print(exc);
        }
        }

        Comment


          #5
          Hello nandhumca,

          I would suggest calling ForceRefresh() after the Draw method is called.

          ForceRefresh() - https://ninjatrader.com/support/help...rcerefresh.htm

          Comment


            #6
            Thanks Jim. It is working. But as per documentation, it will cause performance issue. is that recommended to go with this approach ?

            Comment


              #7
              Hello nandhumca,

              It would be performance demanding if it is over-used, but calling it once when a mouse button is clicked is not a big deal.

              Comment


                #8
                Hi Jim,

                I am using below code to get the time value of mouse clicked. But the value is not consistent, If I do mouse click on right part of the bar, it is giving the time value of next bar. How to get the bar index of the mouse clicked ?

                Code:
                DateTime slotTimeX = ChartControl.GetTimeByX((int)ChartControl.MouseDow nPoint.X);
                
                int barsAgo = CurrentBar - Bars.GetBar(slotTimeX);

                Comment


                  #9
                  Hello nandhumca,

                  Thanks for your note.

                  You would need to get the position of the mouse click, use ChartingExtensions to convert the position for different dpi settings, and get the slot and the time with the slot with GetTimeBySlotIndex and GetSlotIndexByX.

                  See the example script created by my colleague Chelsea, MouseXYToBarTimePriceExample_NT8, in the forum link below which demonstrates how to capture a mouse click and adjust for DPI settings properly.



                  Also, see this help guide page for more information: https://ninjatrader.com/support/help...oordinates.htm

                  Let us know if we may assist further.
                  <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  563 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  329 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  101 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  547 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  548 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X