• 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

ChartTrader, click on chart, get price at click?

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

    ChartTrader, click on chart, get price at click?

    Hello,

    1) Any ideas how to get (through C# code) to get the price under the cursor on a mouse click?

    2) Is there a way (in code) to get the current ATM in Chartrader (the one in the drop down list)?

    Thx!!

    #2
    I'm still very much a novice when it comes to NinjaScript, however I can try to explain how I do this. Maybe someone will come along and give us a better way of doing it!

    To give you a primary rundown, first we're going to create a custom event handler to capture the mouse click event, then we're going to create a method we can use to get the price value of a Y coordinate. It's all very simple...

    First we need to add the event handler to catch the mouse down event. So we'll start by telling Windows that we want our custom event "OnMouseDownEvent" to receive the mouse down event. We'll do it by overriding the StartUp method and placing the command in there.
    Code:
    protected override void OnStartUp() 
    {
    	this.ChartControl.ChartPanel.MouseDown += new MouseEventHandler(this.OnMouseDownEvent);
    }
    Then we actually need to create the method that will catch the mouse down event for us.
    Code:
    private void OnMouseDownEvent(object sender, MouseEventArgs e)
    {
    	/* Code will go here to handle the mouse down event. */
    }
    Now that we're grabbing the mouse event, let's work on getting the price level from a Y coordinate. So first, we'll create some variables to hold some information about our chart's boundaries. We'll just place these at the top of the module with our other variables.
    Code:
    private	double		myChartMax;
    private	double		myChartMin;
    private	Rectangle	myChartBounds;
    To make sure we're putting the correct information into our variables, we're going to override the Plot method and use it to fill our variables for us.
    Code:
    public override void Plot(Graphics graphics, Rectangle bounds, double min, double max)
    {
    	/* This will ensure that our other plots are still being drawn correctly. */
    	base.Plot(graphics, bounds, min, max);
    			
    	/* Here we grab the information we need and place it into our variables. */
    	myChartMin = min;
    	myChartMax = max;
    	myChartBounds = bounds; 
    }
    Now for the bread and butter! We're going to create a method that will take a Y value and return the calculated price.
    Code:
    private double GetPriceFromY(int y)
    {
    	return (((((myChartBounds.Y + myChartBounds.Height) - y) * ChartControl.MaxMinusMin(myChartMax, myChartMin)) / ((double)myChartBounds.Height)) + myChartMin);
    }
    To test our code, we'll go into the mouse down event handler that we previously created and add some code to show us if we're getting the right price.
    Code:
    private void OnMouseDownEvent(object sender, MouseEventArgs e)
    {
    	Print("Y Coord: " + e.Y + " -- Price: " + GetPriceFromY(e.Y));
    	DrawHorizontalLine("Line" + e.Y, GetPriceFromY(e.Y), Color.Blue);
    }
    Hopefully that works...I didn't actually test it out, so let me know if you have any trouble!

    Comment


      #3
      Not work, the price is not calculated correctly

      Comment


        #4
        Hello KeenEdge,

        I have tested this code and found it to be 100% working correctly.

        Below is a link to a video of the test.
        http://screencast.com/t/ZAXn10nj5X

        Attached is a copy of the script exported.
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi this looks really elegant. I am trying to solve problem. Is there a way to get exact bar index, when i click into chart? I am trying to get bar index if i click on it with mouse.

          thanks for advices

          Paul

          Comment


            #6
            This is awesome. Is it also possible to do it with a rectangle and retrieve the ID of the rectangle?

            Comment


              #7
              Hello kujista,

              Getting the bar time or bar number is not as easy as getting the price from the Y.

              However, there is a forum thread where a community member has shared his approach to this.
              http://ninjatrader.com/support/forum...ad.php?t=24766

              I do want to mention that this is unsupported. The export I provided was just to show that code provided by Coder_Kenny works without issue.

              reach4thelasers,

              I'm not quite sure what you mean. This indicator returns the x and y of the mouse click. Are you wanting to click on an object and have the tag name appear in a text box?
              (edited)
              This might be possible by using the x and y coordinates and looping through all of the drawing objects and seeing if your x and y are within the x and y of the drawing object.

              Or by setting an event listener to a custom drawing object (meaning the event listener or method used would need to be added to a custom coded drawing object).
              However, using a custom event listener would not be supported by NinjaTrader Support. This thread will remain open for any community members that would like to contribute.
              Last edited by NinjaTrader_ChelseaB; 09-28-2016, 01:07 PM.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Yeah what I meant was... Can I bind a handler to some drawing Object like a rectangle so that when I click it I can fire an event. How can I access the drawing object at the point of creation to bind to the event?

                Comment


                  #9
                  Hello reach4thelasers,

                  Binding an event to a drawing object is not supported by NinjaTrader Support to do for NinjaTrader 7, however, this thread will remain open for any community members that would like to assist.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    How can we do this in NinjaTrader 8?

                    Comment


                      #11
                      Hello trader3419,

                      It is not supported for NinjaTrader 8 to add binding to a drawing object (or other non-wpf) objects.

                      Binding can be added for wpf objects though, such as buttons, drop-downs, checkboxes, etc.

                      If you create a custom drawing tool you can add an event to the drawing tool to see when a property has changed, and then subscribe to this event from an indicator.

                      This would be general C# and not NinjaScript supported code, however, NinjaTrader_Matthew was kind enough to put together a rough sample to demonstrate the concept.

                      The attached SamplePropertyChangedTools contains a custom drawing tool 'SamplePropertyChangedDrawingTool' that adds this event and an indicator 'SamplePropertyChangedIndicator' that has an event listener triggered when the drawingStateSpy property changes (when the DrawingState changes).
                      Attached Files
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks for the help. I appreciate it.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by timcjpfx, Today, 03:52 AM
                        0 responses
                        1 view
                        0 likes
                        Last Post timcjpfx  
                        Started by tonynt, Today, 01:42 AM
                        0 responses
                        7 views
                        0 likes
                        Last Post tonynt
                        by tonynt
                         
                        Started by NT-Roland, Yesterday, 04:36 PM
                        0 responses
                        3 views
                        0 likes
                        Last Post NT-Roland  
                        Started by Newtrader101, Yesterday, 03:40 PM
                        2 responses
                        13 views
                        0 likes
                        Last Post Newtrader101  
                        Started by Branpo, Yesterday, 02:58 PM
                        2 responses
                        15 views
                        0 likes
                        Last Post Branpo
                        by Branpo
                         
                        Working...
                        X