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

How to get the time and price of mouse point clicked on chart?

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

    How to get the time and price of mouse point clicked on chart?

    I though it's very easy and many people should've written about it, but after searching in forum, I found nothing useful in NT8. Help me please.

    #2
    It may be that the reason you are unable to find many resources is because these features were not documented in NinjaTrader 7. Fortunately, NinjaTrader 8 has improved support for mouse events and treating charts both as pixel width by pixel height grids, and as time by price grids.

    First, to capture mouse events, you should review this section, and then create code which prints to the New -> NinjaScript Output Window, which lets you know you are successfully capturing mouse events.



    Once you have done so, you can see that one of the objects you have available to you in your OnMouseMove event handler is a ChartPanel object. Documentation for retrieving time, price, x, and y coordinates for ChartPanels can be found here





    We are happy to help with any specific questions that come up.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      I only recently began writing indicators in NT8. I was looking to work with the price, time and bar index of the bar I clicked on with the mouse. Oddly enough, when I started working with the Time[] array, I happened to experience that, on a live chart, if I did a loop to print the last n Time values, it wouldn't go past 15 for the barsAgo parameter. In addition, the values were way off. The code is in a MouseClicked event handler that is added to the chart when the state is historical. Other values returned in the event are fine. There is the output - as of 16 bars ago, I get that the object is not set to a valid reference. Are the charts basic price and time arrays not available in a mouse handler? Thanks.

      Bars ago: 32
      Time test 0: 11/20/2019 6:00:00 PM
      Time test 1: 11/20/2019 4:45:44 PM
      Time test 2: 11/20/2019 4:45:44 PM
      Time test 3: 11/20/2019 4:45:44 PM
      ...<snip>
      Time test 14: 11/20/2019 4:45:44 PM
      Time test 15: 11/20/2019 4:45:44 PM
      Time test didn't work: Object reference not set to an instance of an object.

      The code basically is:

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      <snip>
      }
      else if (State == State.Historical)
      {
      if (ChartControl != null)
      {
      foreach (ChartScale scale in ChartPanel.Scales)
      if (scale.ScaleJustification == ScaleJustification)
      chartScale = scale;

      Print("Adding mouse event");
      ChartControl.MouseLeftButtonDown += MouseClicked;
      }
      }
      else if (State == State.Terminated)
      {
      if (ChartControl != null)
      ChartControl.MouseLeftButtonDown -= MouseClicked;
      }
      }

      and the handler

      protected void MouseClicked(object sender, MouseButtonEventArgs e)
      {
      // convert e.GetPosition for different dpi settings
      clickPoint.X = ChartingExtensions.ConvertToHorizontalPixels(e.Get Position(ChartControl as IInputElement).X, ChartControl.PresentationSource);
      clickPoint.Y = ChartingExtensions.ConvertToVerticalPixels(e.GetPo sition(ChartControl as IInputElement).Y, ChartControl.PresentationSource);

      // -- value is incorrect
      convertedPrice = Instrument.MasterInstrument.RoundToTickSize(chartS cale.GetValueByY((float)clickPoint.Y));

      // -- valoue is correct
      convertedTime = ChartControl.GetTimeBySlotIndex((int)ChartControl. GetSlotIndexByX((int)clickPoint.X));

      int barIdx = 0;
      if (ChartControl != null)
      {
      ChartControl chart = ChartControl;
      barIdx = ChartBars.GetBarIdxByX(chart, (int)clickPoint.X);
      Print("barIdx: " + barIdx.ToString());

      Print("Bars.Count(): " + Bars.Count.ToString());

      if (ChartBars != null)
      {
      Print("From: " + ChartBars.FromIndex.ToString());
      Print("To: " + ChartBars.ToIndex.ToString());
      }

      int barsAgo = (Bars.Count - barIdx - 1);
      Print("Bars ago: " + barsAgo.ToString());

      try
      {
      for (int c = 0; c < Bars.Count; c++)
      {
      Print("Time test " + c.ToString() + ": " + Time[c].ToString());
      }
      }
      catch (Exception e3)
      {
      Print("Time test didn't work: " + e3.Message);
      }


      }

      Thanks,

      Gordon

      Comment


        #4
        Hello grose,

        Below is a link to an example of printing the price and time from mouse click points.


        When accessing series information outside of a data driven method, TriggerCustomEvent() must be called, or the Bars.GetValueAt()/.GetClose()/etc must be used.




        Please include the output from your prints. (Right-click the NinjaScript Output window -> select Save As -> give the text file a name -> click Save -> attach the text file to your next post)
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by michi08, 10-05-2018, 09:31 AM
        2 responses
        737 views
        0 likes
        Last Post Denver_Wayne  
        Started by sightcareclickhere, Today, 01:55 PM
        0 responses
        1 view
        0 likes
        Last Post sightcareclickhere  
        Started by Mindset, 05-06-2023, 09:03 PM
        9 responses
        258 views
        0 likes
        Last Post ender_wiggum  
        Started by Mizzouman1, Today, 07:35 AM
        4 responses
        18 views
        0 likes
        Last Post Mizzouman1  
        Started by philmg, Today, 01:17 PM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_ChristopherJ  
        Working...
        X