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:
    [COLOR=#0000ff][COLOR=#0000ff]switch[/COLOR]([COLOR=#080808]State[/COLOR])
     {
        // ...irrelevant usual crud removed.
     [COLOR=#0000ff]    case[/COLOR] [COLOR=#080808]State[/COLOR].[COLOR=#080808]DataLoaded[/COLOR]:
     [COLOR=#080808]         ChartControl[/COLOR].[COLOR=#080808]MouseUp[/COLOR] += [COLOR=#080808]ChartControl_OnMouseUp[/COLOR];
     
     [COLOR=#0000ff]         break[/COLOR];
     [COLOR=#0000ff]    case[/COLOR] [COLOR=#080808]State[/COLOR].[COLOR=#080808]Terminated[/COLOR]:
     [COLOR=#080808]        ChartControl[/COLOR].[COLOR=#080808]MouseUp[/COLOR] -= [COLOR=#080808]ChartControl_OnMouseUp[/COLOR];
     [COLOR=#0000ff]        break[/COLOR];
     }
    [/COLOR]
    In my event handler:
    Code:
     [COLOR=#0000ff]private[/COLOR] [COLOR=#0000ff]void[/COLOR] [COLOR=#080808]ChartControl_OnMouseUp[/COLOR]([COLOR=#0000ff]object[/COLOR] [COLOR=#080808]sender[/COLOR], [COLOR=#080808]MouseButtonEventArgs[/COLOR] [COLOR=#080808]e[/COLOR])
     {
             [COLOR=#0000ff]if[/COLOR]([COLOR=#080808]Keyboard[/COLOR].[COLOR=#080808]IsKeyDown[/COLOR]([COLOR=#080808]Key[/COLOR].[COLOR=#080808]LeftCtrl[/COLOR]) && 
     [COLOR=#080808]                 Keyboard[/COLOR].[COLOR=#080808]IsKeyDown[/COLOR]([COLOR=#080808]Key[/COLOR].[COLOR=#080808]LeftShift[/COLOR]))
     [COLOR=#080808]                MessageBox[/COLOR].[COLOR=#080808]Show[/COLOR]([COLOR=#080808]ChartControl[/COLOR].[COLOR=#080808]GetTimeByX[/COLOR](
                          ([COLOR=#0000ff]int[/COLOR])[COLOR=#080808]e[/COLOR].[COLOR=#080808]GetPosition[/COLOR]([COLOR=#080808]ChartPanel[/COLOR] [COLOR=#0000ff]as [/COLOR][COLOR=#080808]IInputElement[/COLOR]).[COLOR=#080808]X[/COLOR]).[COLOR=#080808]ToString[/COLOR]());
    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:
               [COLOR=#080808]NinjaTrader[/COLOR].[COLOR=#080808]Gui[/COLOR].[COLOR=#080808]Tools[/COLOR].[COLOR=#080808]SimpleFont[/COLOR] [COLOR=#080808]Arial[/COLOR] = [COLOR=#0000ff]null[/COLOR];
               [COLOR=#0000ff]protected[/COLOR] [COLOR=#0000ff]override[/COLOR] [COLOR=#0000ff]void[/COLOR] [COLOR=#080808]OnStateChange[/COLOR]()
               {
               [COLOR=#0000ff]switch[/COLOR] ([COLOR=#080808]State[/COLOR])
               {
               [COLOR=#0000ff]case[/COLOR] [COLOR=#080808]State[/COLOR].[COLOR=#080808]SetDefaults[/COLOR]:
               [COLOR=#080808]Description[/COLOR] = [COLOR=#b22222]@"Test1."[/COLOR];
               [COLOR=#080808]Name[/COLOR] = [COLOR=#b22222]"Test1"[/COLOR];
               [COLOR=#080808]Calculate[/COLOR] = [COLOR=#080808]Calculate[/COLOR].[COLOR=#080808]OnBarClose[/COLOR];
               [COLOR=#080808]IsOverlay[/COLOR] = [COLOR=#0000ff]true[/COLOR];
               [COLOR=#080808]DisplayInDataBox[/COLOR] = [COLOR=#0000ff]true[/COLOR];
               [COLOR=#080808]DrawOnPricePanel[/COLOR] = [COLOR=#0000ff]true[/COLOR];
               [COLOR=#080808]DrawHorizontalGridLines[/COLOR] = [COLOR=#0000ff]true[/COLOR];
               [COLOR=#080808]DrawVerticalGridLines[/COLOR] = [COLOR=#0000ff]true[/COLOR];
               [COLOR=#080808]PaintPriceMarkers[/COLOR] = [COLOR=#0000ff]true[/COLOR];
               [COLOR=#080808]ScaleJustification[/COLOR] = [COLOR=#080808]NinjaTrader[/COLOR].[COLOR=#080808]Gui[/COLOR].[COLOR=#080808]Chart[/COLOR].[COLOR=#080808]ScaleJustification[/COLOR].[COLOR=#080808]Right[/COLOR];
               [COLOR=#006400]//Disable this property if your indicator requires custom values that cumulate with each new market data event. 
              [/COLOR][COLOR=#006400]//See Help Guide for additional information.
              [/COLOR][COLOR=#080808]IsSuspendedWhileInactive[/COLOR] = [COLOR=#0000ff]true[/COLOR];
               [COLOR=#0000ff]break[/COLOR];
               [COLOR=#0000ff]case[/COLOR] [COLOR=#080808]State[/COLOR].[COLOR=#080808]Configure[/COLOR]:
               [COLOR=#0000ff]break[/COLOR];
               [COLOR=#0000ff]case[/COLOR] [COLOR=#080808]State[/COLOR].[COLOR=#080808]DataLoaded[/COLOR]:
               [COLOR=#0000ff]if[/COLOR] ([COLOR=#080808]ChartControl[/COLOR] != [COLOR=#0000ff]null[/COLOR])
               {
               [COLOR=#080808]ChartControl[/COLOR].[COLOR=#080808]PreviewMouseLeftButtonDown[/COLOR] += [COLOR=#080808]ChartControl_PreviewMouseLeftButtonDown[/COLOR];
               [COLOR=#080808]ChartControl[/COLOR].[COLOR=#080808]PreviewMouseLeftButtonUp[/COLOR] += [COLOR=#080808]ChartControl_PreviewMouseLeftButtonUp[/COLOR];
               }
               [COLOR=#080808]Arial[/COLOR] = [COLOR=#0000ff]new[/COLOR] [COLOR=#080808]NinjaTrader[/COLOR].[COLOR=#080808]Gui[/COLOR].[COLOR=#080808]Tools[/COLOR].[COLOR=#080808]SimpleFont[/COLOR]([COLOR=#b22222]"Arial"[/COLOR], [COLOR=#ff8c00]14[/COLOR]) { [COLOR=#080808]Size[/COLOR] = [COLOR=#ff8c00]14[/COLOR], [COLOR=#080808]Bold[/COLOR] = [COLOR=#0000ff]true[/COLOR] };
               [COLOR=#0000ff]break[/COLOR];
               [COLOR=#0000ff]case[/COLOR] [COLOR=#080808]State[/COLOR].[COLOR=#080808]Terminated[/COLOR]:
               [COLOR=#0000ff]if[/COLOR] ([COLOR=#080808]ChartControl[/COLOR] != [COLOR=#0000ff]null[/COLOR])
               {
               [COLOR=#080808]ChartControl[/COLOR].[COLOR=#080808]PreviewMouseLeftButtonDown[/COLOR] -= [COLOR=#080808]ChartControl_PreviewMouseLeftButtonDown[/COLOR];
               [COLOR=#080808]ChartControl[/COLOR].[COLOR=#080808]PreviewMouseLeftButtonUp[/COLOR] -= [COLOR=#080808]ChartControl_PreviewMouseLeftButtonUp[/COLOR];
               [COLOR=#080808]Arial[/COLOR] = [COLOR=#0000ff]null[/COLOR];
               }
               [COLOR=#0000ff]break[/COLOR];
               }
               }
               [COLOR=#0000ff]private[/COLOR] [COLOR=#0000ff]void[/COLOR] [COLOR=#080808]ChartControl_PreviewMouseLeftButtonDown[/COLOR]([COLOR=#0000ff]object[/COLOR] [COLOR=#080808]sender[/COLOR], [COLOR=#080808]MouseButtonEventArgs[/COLOR] [COLOR=#080808]e[/COLOR])
               {
               
               [COLOR=#0000ff]string[/COLOR] [COLOR=#080808]sResult[/COLOR] = [COLOR=#b22222]""[/COLOR];
               [COLOR=#0000ff]if[/COLOR] ([COLOR=#080808]Keyboard[/COLOR].[COLOR=#080808]IsKeyDown[/COLOR]([COLOR=#080808]Key[/COLOR].[COLOR=#080808]LeftCtrl[/COLOR]) && [COLOR=#080808]Keyboard[/COLOR].[COLOR=#080808]IsKeyDown[/COLOR]([COLOR=#080808]Key[/COLOR].[COLOR=#080808]LeftShift[/COLOR]))
               {
               
               
               [COLOR=#080808]Point[/COLOR] [COLOR=#080808]pt[/COLOR] = [COLOR=#0000ff]new[/COLOR] [COLOR=#080808]Point[/COLOR]();
               
               [COLOR=#006400]//my way
              [/COLOR][COLOR=#080808]sResult[/COLOR] = [COLOR=#080808]GetString[/COLOR]([COLOR=#080808]e[/COLOR].[COLOR=#080808]GetPosition[/COLOR]([COLOR=#080808]ChartPanel[/COLOR] [COLOR=#0000ff]as[/COLOR] [COLOR=#080808]IInputElement[/COLOR]).[COLOR=#080808]X[/COLOR]);
               [COLOR=#080808]pt[/COLOR].[COLOR=#080808]X[/COLOR] = [COLOR=#080808]ChartingExtensions[/COLOR].[COLOR=#080808]ConvertToHorizontalPixels[/COLOR]([COLOR=#080808]e[/COLOR].[COLOR=#080808]GetPosition[/COLOR]([COLOR=#080808]ChartPanel[/COLOR] [COLOR=#0000ff]as[/COLOR] [COLOR=#080808]IInputElement[/COLOR]).[COLOR=#080808]X[/COLOR], [COLOR=#080808]ChartControl[/COLOR].[COLOR=#080808]PresentationSource[/COLOR]);
               [COLOR=#080808]pt[/COLOR].[COLOR=#080808]Y[/COLOR] = [COLOR=#080808]ChartingExtensions[/COLOR].[COLOR=#080808]ConvertToVerticalPixels[/COLOR]([COLOR=#080808]e[/COLOR].[COLOR=#080808]GetPosition[/COLOR]([COLOR=#080808]ChartPanel[/COLOR] [COLOR=#0000ff]as[/COLOR] [COLOR=#080808]IInputElement[/COLOR]).[COLOR=#080808]Y[/COLOR], [COLOR=#080808]ChartControl[/COLOR].[COLOR=#080808]PresentationSource[/COLOR]);
               [COLOR=#080808]Draw[/COLOR].[COLOR=#080808]TextFixed[/COLOR]([COLOR=#0000ff]this[/COLOR], [COLOR=#b22222]"TFDefs"[/COLOR], [COLOR=#080808]sResult[/COLOR], [COLOR=#080808]TextPosition[/COLOR].[COLOR=#080808]BottomRight[/COLOR], [COLOR=#080808]ChartControl[/COLOR].[COLOR=#080808]Properties[/COLOR].[COLOR=#080808]ChartText[/COLOR], [COLOR=#080808]Arial[/COLOR], [COLOR=#080808]Brushes[/COLOR].[COLOR=#080808]Blue[/COLOR], [COLOR=#080808]Brushes[/COLOR].[COLOR=#080808]Transparent[/COLOR], [COLOR=#ff8c00]0[/COLOR]);
               
               [COLOR=#006400]// nt way
              [/COLOR][COLOR=#080808]sResult[/COLOR] = [COLOR=#080808]GetString2[/COLOR]([COLOR=#080808]e[/COLOR].[COLOR=#080808]GetPosition[/COLOR]([COLOR=#080808]ChartControl[/COLOR]).[COLOR=#080808]X[/COLOR].[COLOR=#080808]ConvertToHorizontalPixels[/COLOR]([COLOR=#080808]ChartControl[/COLOR].[COLOR=#080808]PresentationSource[/COLOR]));
               [COLOR=#080808]pt[/COLOR].[COLOR=#080808]X[/COLOR] = [COLOR=#080808]e[/COLOR].[COLOR=#080808]GetPosition[/COLOR]([COLOR=#080808]ChartControl[/COLOR]).[COLOR=#080808]X[/COLOR].[COLOR=#080808]ConvertToHorizontalPixels[/COLOR]([COLOR=#080808]ChartControl[/COLOR].[COLOR=#080808]PresentationSource[/COLOR]);
               [COLOR=#080808]pt[/COLOR].[COLOR=#080808]Y[/COLOR] = [COLOR=#080808]e[/COLOR].[COLOR=#080808]GetPosition[/COLOR]([COLOR=#080808]ChartControl[/COLOR]).[COLOR=#080808]Y[/COLOR].[COLOR=#080808]ConvertToVerticalPixels[/COLOR]([COLOR=#080808]ChartControl[/COLOR].[COLOR=#080808]PresentationSource[/COLOR]);
               [COLOR=#080808]Draw[/COLOR].[COLOR=#080808]TextFixed[/COLOR]([COLOR=#0000ff]this[/COLOR], [COLOR=#b22222]"TFDefs1"[/COLOR], [COLOR=#080808]sResult[/COLOR], [COLOR=#080808]TextPosition[/COLOR].[COLOR=#080808]BottomLeft[/COLOR], [COLOR=#080808]ChartControl[/COLOR].[COLOR=#080808]Properties[/COLOR].[COLOR=#080808]ChartText[/COLOR], [COLOR=#080808]Arial[/COLOR], [COLOR=#080808]Brushes[/COLOR].[COLOR=#080808]Blue[/COLOR], [COLOR=#080808]Brushes[/COLOR].[COLOR=#080808]Transparent[/COLOR], [COLOR=#ff8c00]0[/COLOR]);
               [COLOR=#080808]ForceRefresh[/COLOR]();
               }
               }
               [COLOR=#0000ff]private[/COLOR] [COLOR=#0000ff]void[/COLOR] [COLOR=#080808]ChartControl_PreviewMouseLeftButtonUp[/COLOR]([COLOR=#0000ff]object[/COLOR] [COLOR=#080808]sender[/COLOR], [COLOR=#080808]MouseButtonEventArgs[/COLOR] [COLOR=#080808]e[/COLOR])
               {
               [COLOR=#080808]RemoveDrawObject[/COLOR]([COLOR=#b22222]"TFDefs"[/COLOR]);
               [COLOR=#080808]RemoveDrawObject[/COLOR]([COLOR=#b22222]"TFDefs1"[/COLOR]);
               }
               [COLOR=#0000ff]private[/COLOR] [COLOR=#0000ff]string[/COLOR] [COLOR=#080808]GetString2[/COLOR]([COLOR=#0000ff]int[/COLOR] [COLOR=#080808]xPos[/COLOR])
               {
               [COLOR=#080808]DateTime[/COLOR] [COLOR=#080808]tm[/COLOR] = [COLOR=#080808]ChartControl[/COLOR].[COLOR=#080808]GetTimeByX[/COLOR]([COLOR=#080808]xPos[/COLOR]);
               [COLOR=#0000ff]return[/COLOR] [COLOR=#b22222]"The time on the chart at the mouse is "[/COLOR] + [COLOR=#080808]tm[/COLOR].[COLOR=#080808]ToString[/COLOR]() + [COLOR=#b22222]"\r\n"[/COLOR];
               }
               [COLOR=#0000ff]private[/COLOR] [COLOR=#0000ff]string[/COLOR] [COLOR=#080808]GetString[/COLOR]([COLOR=#0000ff]double[/COLOR] [COLOR=#080808]xPos[/COLOR])
               {
               [COLOR=#080808]DateTime[/COLOR] [COLOR=#080808]tm[/COLOR] = [COLOR=#080808]ChartControl[/COLOR].[COLOR=#080808]GetTimeByX[/COLOR](([COLOR=#0000ff]int[/COLOR]) [COLOR=#080808]xPos[/COLOR]);
               [COLOR=#0000ff]return[/COLOR] [COLOR=#b22222]"The time on the chart at the mouse is "[/COLOR] + [COLOR=#080808]tm[/COLOR].[COLOR=#080808]ToString[/COLOR]() + [COLOR=#b22222]"\r\n"[/COLOR];
               }
              Last edited by edstaffin; 06-07-2016, 01: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 GLFX005, Today, 03:23 AM
                  0 responses
                  1 view
                  0 likes
                  Last Post GLFX005
                  by GLFX005
                   
                  Started by XXtrader, Yesterday, 11:30 PM
                  2 responses
                  11 views
                  0 likes
                  Last Post XXtrader  
                  Started by Waxavi, Today, 02:10 AM
                  0 responses
                  6 views
                  0 likes
                  Last Post Waxavi
                  by Waxavi
                   
                  Started by TradeForge, Today, 02:09 AM
                  0 responses
                  14 views
                  0 likes
                  Last Post TradeForge  
                  Started by Waxavi, Today, 02:00 AM
                  0 responses
                  3 views
                  0 likes
                  Last Post Waxavi
                  by Waxavi
                   
                  Working...
                  X