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

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 PaulMohn, Today, 05:00 AM
          0 responses
          8 views
          0 likes
          Last Post PaulMohn  
          Started by ZenCortexAuCost, Today, 04:24 AM
          0 responses
          6 views
          0 likes
          Last Post ZenCortexAuCost  
          Started by ZenCortexAuCost, Today, 04:22 AM
          0 responses
          3 views
          0 likes
          Last Post ZenCortexAuCost  
          Started by SantoshXX, Today, 03:09 AM
          0 responses
          16 views
          0 likes
          Last Post SantoshXX  
          Started by DanielTynera, Today, 01:14 AM
          0 responses
          5 views
          0 likes
          Last Post DanielTynera  
          Working...
          X