• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Get bar from MouseButtonEventArgs.Position

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

    Get bar from MouseButtonEventArgs.Position

    Hi,
    In OnStateChange I have:
    Code:
    switch(State)
     {
        // ...irrelevant usual crud removed.
         case State.DataLoaded:
              ChartControl.MouseUp += ChartControl_OnMouseUp;
     
              break;
         case State.Terminated:
             ChartControl.MouseUp -= ChartControl_OnMouseUp;
             break;
     }
    
    In my event handler:
    Code:
     private void ChartControl_OnMouseUp(object sender, MouseButtonEventArgs e)
     {
             if(Keyboard.IsKeyDown(Key.LeftCtrl) && 
                      Keyboard.IsKeyDown(Key.LeftShift))
                     MessageBox.Show(ChartControl.GetTimeByX(
                          (int)e.GetPosition(ChartPanel as IInputElement).X).ToString());
    The result is not very accurate as I am converting the .X value which is a double to an int. This is because GetTimeByX wants an int. The result is often off by multiple seconds.

    Soooo, what I am trying to do is just get the accurate chart time from the mouse position at the time of mouse up so I can do some calcs and display a MessageBox with the calc result.

    Is there a better way than what I am doing? Actually, what I would really love is an Bar.OnClick event that I can handle and know what bar is displayed in the x coordinate of the mouse. In checking the docs this doesn't appear to exist. I'm kind of surprised. This would seem to be something that people would regularly what to do ... namely, catch mouse events on bars. Is there a way to do this without a lot of gymnastics?

    What I'd really love even more is a way to add info to the little box that comes up when you mousewheel click on a bar. That's probably asking too much but ...

    Thanks ... Ed

    #2
    Hello,

    Thank you for the question on this.

    Currently you are utilizing the only conversion method I know that is exposed. I will raise this question to development to see if there is a more correct approach as the return from a Points X and Y is a Double and the conversion method is Int type.

    Once I have further information on this I will reply back.

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

    Comment


      #3
      Hello,

      I wanted to reply on this to check if the following item is the resolution for the question.

      I see there is an extension method to aid in converting a Double to a Int regarding pixels: http://ninjatrader.com/support/helpG...ntalpixels.htm

      Code:
      private void myDown(object sender, MouseButtonEventArgs e)
      {
      Print("myDown: " + e.GetPosition(ChartControl).X.ConvertToHorizontalPixels(ChartControl.PresentationSource));
      //GetTimeByX(e.GetPosition(ChartControl).X.ConvertToHorizontalPixels(ChartControl.PresentationSource)) 
      }
      Could you please test using this method in your logic to confirm if this does solve the problem?

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

      Comment


        #4
        I look today and get back to you.
        Thanks ... Ed

        Comment


          #5
          Your code and mine produce identical results. Is one technically better than the other?
          Thanks ... Ed

          Comment


            #6
            Hello,

            Thank you for trying that.

            Aside from this being a supported method, I would be unsure of what actual logic is used in the internal method to really answer if one is better.

            Could you provide a simple example that includes the mouse handler and click logic you are currently using in script form for me to provide to development for further review?

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

            Comment


              #7
              This should get you what you need.
              Just pasted this into vstudio and hit ctrl k-d and it'll put the formatting back in.
              Just throw it on a chart and hit the left crtl - left shift - left mouse button and hold the mouse button down.
              ***** I modified the code slightly since originally posting. Use this if you already got it.*****

              Thanks ... Ed

              Code:
               NinjaTrader.Gui.Tools.SimpleFont Arial = null;
               protected override void OnStateChange()
               {
               switch (State)
               {
               case State.SetDefaults:
               Description = @"Test1.";
               Name = "Test1";
               Calculate = Calculate.OnBarClose;
               IsOverlay = true;
               DisplayInDataBox = true;
               DrawOnPricePanel = true;
               DrawHorizontalGridLines = true;
               DrawVerticalGridLines = true;
               PaintPriceMarkers = true;
               ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
               //Disable this property if your indicator requires custom values that cumulate with each new market data event. 
              //See Help Guide for additional information.
              IsSuspendedWhileInactive = true;
               break;
               case State.Configure:
               break;
               case State.DataLoaded:
               if (ChartControl != null)
               {
               ChartControl.PreviewMouseLeftButtonDown += ChartControl_PreviewMouseLeftButtonDown;
               ChartControl.PreviewMouseLeftButtonUp += ChartControl_PreviewMouseLeftButtonUp;
               }
               Arial = new NinjaTrader.Gui.Tools.SimpleFont("Arial", 14) { Size = 14, Bold = true };
               break;
               case State.Terminated:
               if (ChartControl != null)
               {
               ChartControl.PreviewMouseLeftButtonDown -= ChartControl_PreviewMouseLeftButtonDown;
               ChartControl.PreviewMouseLeftButtonUp -= ChartControl_PreviewMouseLeftButtonUp;
               Arial = null;
               }
               break;
               }
               }
               private void ChartControl_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
               {
               
               string sResult = "";
               if (Keyboard.IsKeyDown(Key.LeftCtrl) && Keyboard.IsKeyDown(Key.LeftShift))
               {
               
               
               Point pt = new Point();
               
               //my way
              sResult = GetString(e.GetPosition(ChartPanel as IInputElement).X);
               pt.X = ChartingExtensions.ConvertToHorizontalPixels(e.GetPosition(ChartPanel as IInputElement).X, ChartControl.PresentationSource);
               pt.Y = ChartingExtensions.ConvertToVerticalPixels(e.GetPosition(ChartPanel as IInputElement).Y, ChartControl.PresentationSource);
               Draw.TextFixed(this, "TFDefs", sResult, TextPosition.BottomRight, ChartControl.Properties.ChartText, Arial, Brushes.Blue, Brushes.Transparent, 0);
               
               // nt way
              sResult = GetString2(e.GetPosition(ChartControl).X.ConvertToHorizontalPixels(ChartControl.PresentationSource));
               pt.X = e.GetPosition(ChartControl).X.ConvertToHorizontalPixels(ChartControl.PresentationSource);
               pt.Y = e.GetPosition(ChartControl).Y.ConvertToVerticalPixels(ChartControl.PresentationSource);
               Draw.TextFixed(this, "TFDefs1", sResult, TextPosition.BottomLeft, ChartControl.Properties.ChartText, Arial, Brushes.Blue, Brushes.Transparent, 0);
               ForceRefresh();
               }
               }
               private void ChartControl_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
               {
               RemoveDrawObject("TFDefs");
               RemoveDrawObject("TFDefs1");
               }
               private string GetString2(int xPos)
               {
               DateTime tm = ChartControl.GetTimeByX(xPos);
               return "The time on the chart at the mouse is " + tm.ToString() + "\r\n";
               }
               private string GetString(double xPos)
               {
               DateTime tm = ChartControl.GetTimeByX((int) xPos);
               return "The time on the chart at the mouse is " + tm.ToString() + "\r\n";
               }
              Last edited by edstaffin; 06-07-2016, 12:12 PM.

              Comment


                #8
                Hello,

                Thank you for providing that.

                I wanted to confirm, are you trying to get the time of the bar under the mouse for the given X position, or the exact time under the mouse X?

                By what was provided, you would be getting the exact time under the mouse X position, so it would not directly be the same time you see on the bars Close time or crosshair.

                If you are trying to get the Bar under the X position, I have a extended sample showing how to get the specific bar values which I have attached. In the case you are trying to get the exact time under the mouse, you are already getting that time but again it would not exactly match the bar close time or times listed on the crosshair as examples.

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

                Comment


                  #9
                  Hi,
                  In the end it turns out I was only looking for the time at the mouse click. However, I would be interested in seeing your sample. Always good to learn something new!
                  Thanks ... Ed

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by timcjpfx, Today, 06:22 AM
                  3 responses
                  17 views
                  0 likes
                  Last Post NinjaTrader_Kate  
                  Started by welson, Today, 08:26 AM
                  2 responses
                  12 views
                  0 likes
                  Last Post NinjaTrader_PatrickG  
                  Started by brucerobinson, Today, 08:27 AM
                  3 responses
                  10 views
                  0 likes
                  Last Post NinjaTrader_Kate  
                  Started by Leeroy_Jenkins, Today, 07:45 AM
                  1 response
                  7 views
                  0 likes
                  Last Post NinjaTrader_PaulH  
                  Started by 1001111, 02-22-2020, 09:10 AM
                  5 responses
                  41 views
                  0 likes
                  Last Post NinjaTrader_Jesse  
                  Working...
                  X