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

Focus control, Incorrect Pixel Values & Snap controls

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

    Focus control, Incorrect Pixel Values & Snap controls

    Hello there,

    I'm trying to delete manually drawn objects from an indicator. (I'm aware that is unsupported etc.). My goal is to remove the currently selected draw object by simulating a delete key input when pressing a button (it's for when a keyboard is not present). There is only one thing I'm struggling with. When the button is pressed, the focus is on the button in the chart trader area, thus the delete key doesn't remove the draw object. Is there some way to set the focus back to the selected draw object? For the time being I thought of simulating a mouse click onto the draw object, but only found that the pixel coordinates seem to be off. My current method is:

    Code:
                            var point = draw.GetSelectionPoints(ChartControl, myChartScale)[0];
                            int x = ChartingExtensions.ConvertToHorizontalPixels(point.X, ChartControl.PresentationSource);
                            int y = ChartingExtensions.ConvertToVerticalPixels(point.Y, ChartControl.PresentationSource);
                            
                            MouseSimulator.SetMousePosition(x,y);
                            MouseSimulator.LeftMouseClick(x, y);
                            
                            Keyboard.FocusedElement.RaiseEvent(new KeyEventArgs(Keyboard.PrimaryDevice,
                                PresentationSource.FromVisual(ChartControl.OwnerChart), 0, Key.Delete) { RoutedEvent = Keyboard.PreviewKeyDownEvent } );​
    The x coordinate seems to be correct, but the y value is well below the selection point.


    Also, I'd like to know if there's a way to identify the currently selected snap mode and change it? I can simulate hotkey events for changing it if that's not possible through code, but I still need some way to identify it.

    #2
    Hello Human#102,

    This would be unsupported, but the RolloverIndications indicator has some code to focus the chart you may find useful.
    Shows a button on the chart to roll the expiry to the next available contract month. Option to hide button until a number of days before or on roll date. Another option to confirm by opening the instrument overlay selector or automatically rollover without confirmation. (Update June 27th, 2019 – Added an option to show […]
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you for the quick feedback. I have looked at the example previously and while I can focus the chart, this unfortunately is not enough for the delete function to work on the drawing object. If the focusing is not possible, there is still the solution with the mouseclick. There the issue's as mentioned that my cursor ends up well below the the SelectionPoint. I can say that the ChartingExtensions functions definitely provide more accurate coordinate than the chart based pixel values, obviously, but it is still off. Is that a known issue, or is there an example which on your end works?

      I've currently no time to provide a sample script for testing, perhaps in the next days, but perhaps it's enough to share these parts:

      The mouse smulator:
      Code:
      public class MouseSimulator
          {
              // Import the user32.dll for native methods
              [DllImport("user32.dll")]
              private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
          
              [DllImport("user32.dll")]
              private static extern bool SetCursorPos(int X, int Y);
          
              // Constants for mouse events
              private const int MOUSEEVENTF_LEFTDOWN = 0x02;
              private const int MOUSEEVENTF_LEFTUP = 0x04;
              private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
              private const int MOUSEEVENTF_RIGHTUP = 0x10;
          
              // Method to set the cursor position
              public static void SetMousePosition(int x, int y)
              {
                  SetCursorPos(x, y);
              }
          
              // Method to simulate a left mouse click
              public static void LeftMouseClick(int x, int y)
              {
                  // Simulate a left mouse button down
                  mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
          
                  // Simulate a left mouse button up
                  mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
              }
          }​
      My Click Method:
      Code:
      private void RemoveDrawClick(object sender, RoutedEventArgs e)
              {
                  TriggerCustomEvent(o => {
                      if (myChartScale == null) return;
                      
                      foreach (DrawingTool draw in DrawObjects.ToList())
                      {
                          if (draw.IsSelected)
                          {
                              var point = draw.GetSelectionPoints(ChartControl, myChartScale)[0];
                              int x = ChartingExtensions.ConvertToHorizontalPixels(point.X, ChartControl.PresentationSource);
                              int y = ChartingExtensions.ConvertToVerticalPixels(point.Y, ChartControl.PresentationSource);
                              
                              MouseSimulator.SetMousePosition(x,y);
                              MouseSimulator.LeftMouseClick(x, y);
                              
                              Keyboard.FocusedElement.RaiseEvent(new KeyEventArgs(Keyboard.PrimaryDevice,
                                  PresentationSource.FromVisual(ChartControl.OwnerChart), 0, Key.Delete) { RoutedEvent = Keyboard.PreviewKeyDownEvent } );
                          }
                      }
                  }, null);
              }​

      Comment


        #4
        Hello Human#102,

        Unfortunately, I wouldn't be able to provide any unsupported code (or assistance) with simulating the mouse clicks.

        Try removing the drawing objects with RemoveDrawObject() and supplying the tag of the object.

        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          The issue with RemoveDrawObject() is that I want to remove manually drawn objects, which is not possible through these methods. I understand that you don't support simulating mouse clicks, but my question is targeted at these methods:

          Code:
          var point = draw.GetSelectionPoints(ChartControl, myChartScale)[0];
          int x = ChartingExtensions.ConvertToHorizontalPixels(point.X, ChartControl.PresentationSource);
          int y = ChartingExtensions.ConvertToVerticalPixels(point.Y, ChartControl.PresentationSource);​
          As far as I'm aware these are documented and supported functions, but don't provide the correct values in my case. So my question is, to put all the unsupported context aside, how can I get the exact display pixel coordinates of a drawing object. Be it Anchors selection points or something else. As I've written before, the x value seems to be correct, but the y value does not.

          Comment


            #6
            Hello Human#102,

            The drawing objects Anchors have the price / time. (Not selection anchors)

            For example a Line object has a StartAnchor and EndAnchor.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I have tried it with both, anchors and selection points, and have received the same coordinates.

              Comment


                #8
                Hello Human#102,

                These would be the coordinates of the anchors. Unfortunately, I wouldn't be able to provide any information on triggering a click on them.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Perhaps my questions got a bit unclear. My current question is about getting the correct pixels of anchor points and translate them into correct device pixels. This is something which is documented and thus I presume also supported as documented here: https://ninjatrader.com/support/help...oordinates.htm
                  and demonstrated here: https://ninjatrader.com/support/help...ntalpixels.htm

                  These methods don't produce the correct pixel coordinates for me currently, to be precise, the y value is too "low" on the screen. I can't rule out 100% that my code should absolutely produce the correct values, but as of now, I don't know if it is an issue with my code, with my use and understanding of these methods or perhaps even a bug within the methods. Is there a working example to get the correct pixel coordinates of e.g. some drawing object or similar? Perhaps one which places the mouse cursor to these coordinates as well?

                  Comment


                    #10
                    Hello Human#102,

                    The drawing objects wouldn't need to be converted to device pixels that I am aware.

                    The x and y of the anchors price and slotindex would be converted with ChartScale.GetYByValue() and ChartControl.GetXByBarIndex() / ChartControl.GetXByTime().
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by ccrone2024, Today, 05:13 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post ccrone2024  
                    Started by ttodua, Today, 03:40 AM
                    0 responses
                    4 views
                    0 likes
                    Last Post ttodua
                    by ttodua
                     
                    Started by ttodua, Today, 03:18 AM
                    0 responses
                    4 views
                    0 likes
                    Last Post ttodua
                    by ttodua
                     
                    Started by TraderKnG, Today, 01:30 AM
                    0 responses
                    11 views
                    0 likes
                    Last Post TraderKnG  
                    Started by morrnel, 05-12-2024, 06:07 PM
                    8 responses
                    154 views
                    1 like
                    Last Post nobchi
                    by nobchi
                     
                    Working...
                    X