Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Mouse click detection and multiple dataseries in same window

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

    Mouse click detection and multiple dataseries in same window

    Hello,

    I have been using the following code on tick charts for some time to detect the click position / time. Today, I overlayed a 5 minute bar series in the same window, and the code now returns the wrong time...

    // convert e.GetPosition for different dpi settings
    clickPoint.X = ChartingExtensions.ConvertToHorizontalPixels(e.Get Position(ChartPanel as IInputElement).X, ChartControl.PresentationSource);
    clickPoint.Y = ChartingExtensions.ConvertToVerticalPixels(e.GetPo sition(ChartPanel as IInputElement).Y, ChartControl.PresentationSource);

    double slotIndex = 0;
    if (clickPoint.Y > 0)
    {
    slotIndex = ChartControl.GetSlotIndexByX((int)clickPoint.X);
    if (Time.IsValidDataPointAt((int)slotIndex) && slotIndex > 0 && slotIndex < Bars.Count) // make sure we have a valid data point
    {
    Print("button clicked on bar " + slotIndex.ToString() + " date is " + Time.GetValueAt((int)slotIndex).ToString());
    GetData(Time.GetValueAt((int)slotIndex), DateTime.Now, slotIndex);
    } else {
    Print("Slotindex invalid? : " + slotIndex.ToString());
    }
    }
    }

    Any hints on how I might make this work with multiple bar series in same window? My times are way too short when the 5 minute series is on there. If I remove it, all works as expected again. Is there a way to force this to a specific data series?

    Thanks.

    #2
    Hello pjsmith,

    Thank you for the post.

    I wanted to make sure I have the whole picture of what you are doing, you have a smaller timeframe ( 1 tick?) and have overlaid a larger timeframe (5minute) in the same panel, is that correct?

    I believe in that use case the chart control/panel will then account for the second series slots however I would need to test that to confirm it. Before going to in depth I wanted to make sure I have the same configuration as you are testing. Can you confirm that for me? .


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

    Comment


      #3
      Yes, that's correct. In this instance, my main data series is a 100 tick chart. I added a 5 minute in the same chart window, and this affects my results. Thank you for your assistance with this.

      Comment


        #4
        Hello pjsmith,

        I took a look over the code and see a couple issues. The first is that you are using the SlotIndex with GetValueAt which is not expecting a Slot but is expecting a Bar index. This works with a single series because the bars happen to line up in the slots, otherwise the slots don't specifically represent the bar data. I believe the following comments from the Slot documentation apply here:

        •A "Slot" is used in Equidistant bar spacing and represents a position on the chart canvas background which may or may not contain a bar. The concept of "Slots" does NOT exist on a TimeBased bar spacing type.

        •If you are looking for information on a bar series, please see ChartBars.GetBarIdxByX()

        •Since the slot index is based on the chart canvas, the value returned by GetSlotIndexByX() can be expected to change as new bars are painted, or as the chart is scrolled backward or forward on the x-axis.

        The way you are getting the value could also be changed to match the above comments which avoids the issue with a secondary series. I used MouseDownPoint as I am not certain how you are currently getting the mouse point, that could also be another area which causes a problem depending on how the X/Y is being retrieved.


        Code:
        Point cursorPoint = chartControl.MouseDownPoint;
        var barIndex = ChartBars.GetBarIdxByX(chartControl, ChartingExtensions.ConvertToHorizontalPixels(cursorPoint.X, chartControl));
        Print("button clicked on bar " + barIndex.ToString() + " date is " + Time.GetValueAt(barIndex).ToString());
        This utilizes the ChartBars object which points to the primary bar series directly.


        I look forward to being of further assistance.


        JesseNinjaTrader Customer Service

        Comment


          #5
          Much appreciated. You nailed it. I made this single code change and all is now good with the second series added

          slotIndex = ChartBars.GetBarIdxByX(ChartControl, ChartingExtensions.ConvertToHorizontalPixels(click Point.X, ChartControl));
          //slotIndex = ChartControl.GetSlotIndexByX((int)clickPoint.X);

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by ksoreaab, Today, 03:47 AM
          0 responses
          2 views
          0 likes
          Last Post ksoreaab  
          Started by soyjesus, Today, 02:22 AM
          1 response
          7 views
          0 likes
          Last Post NinjaTrader_Jason  
          Started by ntdev, Today, 02:37 AM
          0 responses
          4 views
          0 likes
          Last Post ntdev
          by ntdev
           
          Started by DavidWazz, Today, 02:07 AM
          3 responses
          11 views
          0 likes
          Last Post NinjaTrader_Jason  
          Started by NeverDownMoney, Yesterday, 04:21 PM
          1 response
          11 views
          0 likes
          Last Post NinjaTrader_Jason  
          Working...
          X