Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Get price from clicked bar

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

    Get price from clicked bar

    Hello, I am trying to get price from clicked bar and I am receiving Index out of range error.

    I am confused because I am trying to access Open price and I can see the Open Count is 3. Bar Id clicked is 1. My code is Open[Open.Count - barId - 1] which should be in range.

    Cound you please help to explain or point to some resources?

    My code:

    protected void MouseLeftClicked(object sender, MouseButtonEventArgs e)
    {
    if (!active)
    return;

    try
    {
    var y = ChartingExtensions.ConvertToVerticalPixels(e.GetPo sition(ChartPanel as IInputElement).Y, ChartControl.PresentationSource);
    var x = ChartingExtensions.ConvertToHorizontalPixels(e.Get Position(ChartPanel as IInputElement).X, ChartControl.PresentationSource);

    var priceClicked = chartScale.GetValueByY(y);
    var barId = ChartBars.GetBarIdxByX(ChartControl, x);

    DebugPrint("Price clicked " + priceClicked);
    DebugPrint("Bar Id clicked " + barId);

    DebugPrint("Open Count " + Open.Count);
    DebugPrint("High Count " + High.Count);
    DebugPrint("Low Count " + Low.Count);

    DebugPrint("Open " + Open[Open.Count - barId - 1]);
    DebugPrint("High " + High[High.Count - barId - 1]);
    DebugPrint("Low " + Low[Low.Count - barId - 1]);
    }
    catch (Exception ex)
    {
    DebugPrint("MouseLeftClicked - " + ex.Message + " - " + ex.StackTrace);
    }
    }​

    NinjaScript Output:

    2024-09-21 20:29:00:848 - Price clicked -0.525274406332454
    2024-09-21 20:29:00:848 - Bar Id clicked 1
    2024-09-21 20:29:00:848 - Open Count 3
    2024-09-21 20:29:00:848 - High Count 3
    2024-09-21 20:29:00:848 - Low Count 3
    2024-09-21 20:29:00:872 - MouseLeftClicked - 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.GetOpen(Int32 index)
    at NinjaTrader.Data.Bars.GetOpen(Int32 index)
    at NinjaTrader.NinjaScript.PriceSeries.get_Item(Int32 barsAgo)​

    (DebugPrint is my custom function)

    Thanks

    #2
    Hello 09try,

    Thank you for your post.

    I suggest using GetValueAt() to obtain the value, your mouse click method is asynchronous to the bar update logic. It's not always guaranteed that indexing a series will be accurate.

    Comment


      #3
      Hello Gaby, thank you for your reply. I have wrapped my code inside mouse click in TriggerCustomEvent call. Is that also correct? Or should I use what you suggested GetValueAt() ?

      Comment


        #4
        Hello 09try,

        I recommend taking a look at this example script which demonstrates getting the price from mouse click (MouseXYtoBarTimePriceExample):



        Also, we recommend using CurrentBar - barIndex to access the High at the specified bar.

        I have wrapped my code inside mouse click in TriggerCustomEvent call. Is that also correct? Or should I use what you suggested GetValueAt() ?
        The code in your original post does not suggest this is being called from TriggerCustomEvent. If your code is called after TriggerCustomEvent(), can you please include the code that is being used in the script?

        That said, using Bars.GetHigh() or High.GetValueAt() should be fine even without TriggerCustomEvent().

        Comment


          #5
          Gaby, thanks for detailed answer.

          The code in the original post did not include the TriggerCustomEvent. I have since removed it.

          In the end I want to be able to click in the chart, get price at click point and draw dots connected with lines under or over each bar to the right from the clicked point. Dots should be position under/over each bar based on some logic. Currently I am using the GetValueAt() function - thanks.

          Last point: I tried drawing ellipses with SharpDX.Direct2D1.Ellipse but they are "stuck" in the original x and y point where I draw them. I want them to be scaling when the user zooms in or out. Exactly like dots drawn with Draw.Dot();. Is it possible with SharpDX.Direct2D1.Ellipse?

          I have attached my code - important parts are in MouseLeftClicked

          Thanks!

          Comment


            #6
            Hello 09try,

            Last point: I tried drawing ellipses with SharpDX.Direct2D1.Ellipse but they are "stuck" in the original x and y point where I draw them. I want them to be scaling when the user zooms in or out. Exactly like dots drawn with Draw.Dot();. Is it possible with SharpDX.Direct2D1.Ellipse?
            As this is not related to your original inquiry, we kindly ask that you create a new thread for a new inquiry.

            Please note, any time you reply to an thread, this will go to the person you are working with. Reply instead of sending a new email when you would like to continue investigating an existing inquiry.

            Any time you create a new thread, this will create a new inquiry and a new case ticket, which will be answered by the first person available and may not be the person you are previously working with. Create a new thread instead of a reply when you have a new question that is unrelated to a previous inquiry. This will allow us to properly track each issue, and keeping threads on topic is useful for forum users who may be searching for the same solutions.

            We appreciate your cooperation in this regard.

            Comment


              #7
              Please accept my apologies - I will create a new thread.

              Thanks and have a nice day.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              576 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              334 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
              553 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              551 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X