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

MouseMove event

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

    MouseMove event

    Greetings,

    I am doing an indicator for displaying the mouse coordinates in MouseMove event.
    The indicator calls the method "chart_MouseMove" when the mouse is moved but the text isn't updated.
    Is there any way for calling to Plot method ?

    Now the indicator works fine only when clickmouse or Alt+Tab windows or key press.


    ( I know that this doubt is beyond the scope of what you provide support for but I would be very grateful if you could help me ).

    Thanks very much.


    protectedoverridevoid OnBarUpdate()
    {
    if ((ChartControl != null) && (!_initMouseEvents))
    {
    this.ChartControl.ChartPanel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.chart_MouseMove);
    _initMouseEvents = true;
    }
    }

    publicoverridevoid Plot(Graphics graphics, Rectangle bounds, double min, double max)
    {
    _boundsChart = bounds;
    _minChartPrice = min;
    _maxChartPrice = max;

    graphics.DrawString( "Y: " + _mouseOffset, fontTexto, brushTexto, 10, 280 );
    }


    privatevoid chart_MouseMove(object sender, MouseEventArgs e)
    {
    if (ChartControl != null)
    {
    double price1 = ConvertYtoPrice ( e.Y );
    _mouseOffset = price1 ;
    }

    }


    privatedouble ConvertYtoPrice (int i)
    {
    int _tickLength=0;

    if (TickSize < 1)
    _tickLength = TickSize.ToString ().Length-2;

    double chartscale = Math.Abs (_maxChartPrice - _minChartPrice);
    double boundAreaScale = _boundsChart.Bottom - _boundsChart.Y ;

    double ratio = (double)(chartscale)/ boundAreaScale ;
    double chartPrice = Math.Round ( (_minChartPrice ) + ((_boundsChart.Bottom -i) * ratio), _tickLength);

    return chartPrice;
    }

    #2
    Sorry, but as you already figured this clearly is beyond the scope what we provide support for, since coding at that level easily could screw up NT if you don't know exactly what you are doing.

    As last resort you could contact a certified NinjaScript consultant: http://www.ninjatrader.com/webnew/pa...injaScript.htm

    Comment


      #3
      Any progress on this?

      Hi cls71

      Did you manage to make any progress with your code. I am also trying to capture the mouse/crosshair location and use this within an indicator.

      Basically my indicator is a drawing tool that can be used to measure the ticks between 2 points. The idea is to alt-click once at one point on the chart, then alt-click again on the 2nd point. A line should then appear between the two points as well as the distance between the two points.

      What I am missing is the X and Y coordinates of the two alt-mouse clicks.

      I have attached my code for you to have a look at and any suggestions/help would be much appreciated.

      The sample pic shows what I am trying to achieve. Currently my code has the X and Y coordinates fillled in, but I would like to use what you have done, if it works, and use chart clicked coordinates in my code.
      Attached Files

      Comment


        #4
        Originally posted by traderT View Post
        Hi cls71

        Did you manage to make any progress with your code. I am also trying to capture the mouse/crosshair location and use this within an indicator.

        Basically my indicator is a drawing tool that can be used to measure the ticks between 2 points. The idea is to alt-click once at one point on the chart, then alt-click again on the 2nd point. A line should then appear between the two points as well as the distance between the two points.

        What I am missing is the X and Y coordinates of the two alt-mouse clicks.

        I have attached my code for you to have a look at and any suggestions/help would be much appreciated.

        The sample pic shows what I am trying to achieve. Currently my code has the X and Y coordinates fillled in, but I would like to use what you have done, if it works, and use chart clicked coordinates in my code.
        havent look at the code, but basically you got to figure out the bar counts (of the two points). mouse position will only get you the pixel point. you have to calculate the bar count from those pixel points. once you figure out the bar count then its piece of cake.

        Comment


          #5
          ypos?

          Hi bukkan,

          Thanks for the info, I see that converting the xpos to barcount is fairly straight forward, however I am a little stumped on how to do the ypos - how can I determine the ypos and then convert it to price?

          As this is not always from a high to a low, I need to calc and convert the ypos too.

          Comment


            #6
            the said indi does not calculates the price scale as it is redundant for its need.

            anyway, the bars itself contains the price value. isnt it.

            like Close[barcount] will give you the closing price of that bar. now you got to calculate a range and then some more calculation beween the pixel pts (where the mouse was clicked) and you will find what you need.

            Comment


              #7
              Range it is then

              Thx again bukkan, will try and figure out how to do the range vertically. Have only just started programming this year, so this will be a challenge.

              I see that cls71 has some code that converts Y to price so will try and modify that to suit my needs, if I can figure it out.
              Last edited by traderT; 12-28-2009, 07:56 AM.

              Comment


                #8
                NinjaTrader is broken wrt/ asynchronous events like this - it does not leave the Bars collection in a valid state outside the OnBarUpdate method, so you cannot access the historical values of the indicator or its Input.

                however, here's what I have for bar & price:

                PHP Code:
                #region Using declarations
                using System;
                using System.Diagnostics;
                using System.Drawing;
                using NinjaTrader.Gui.Chart;
                #endregion

                // This namespace holds all indicators and is required. Do not change it.
                namespace NinjaTrader.Indicator
                {
                    
                /// <summary>
                    /// Enter the description of your new custom indicator here
                    /// </summary>
                    
                public class XYIndicator Indicator
                    
                {
                        protected 
                override void Initialize ()
                        {
                        }

                        
                void ChartControl_MouseMove (object senderSystem.Windows.Forms.MouseEventArgs e)
                        {
                            
                double right _chartBounds.Right this.ChartControl.BarMarginRight this.ChartControl.BarWidth this.ChartControl.BarSpace 2;
                            
                double bottom _chartBounds.Bottom;

                            
                double x right e.X;
                            
                double y bottom e.Y;

                            
                int iBar this.LastVisibleBar + (int) Math.Round (this.ChartControl.BarSpace);
                            
                double price * (_max _min) / _chartBounds.Height;

                            
                /*
                            if (iBar < 0 || iBar >= this.Count)
                                return;

                            double barPrice = this [iBar];
                            Debug.WriteLine (x + "," + y + ", " + barPrice);
                            */

                            
                Debug.WriteLine (iBar "," price);

                            
                //((IndicatorBase) this).DrawDot ("foo" + iBar, iBar, price, Color.Red);
                        
                }

                        public 
                override void Plot (Graphics graphicsRectangle boundsdouble mindouble max)
                        {
                            
                base.Plot (graphicsboundsminmax);

                            
                _chartBounds bounds;
                            
                _min min;
                            
                _max max;
                        }

                        public 
                override void Dispose ()
                        {
                            
                base.Dispose ();

                            if (
                _chartControl != null)
                                
                _chartControl.ChartPanel.MouseMove -= new System.Windows.Forms.MouseEventHandler (ChartControl_MouseMove);
                        }

                        
                ChartControl _chartControl;
                        
                Rectangle _chartBounds;
                        
                double _min_max;

                        
                /// <summary>
                        /// Called on each bar update event (incoming tick)
                        /// </summary>
                        
                protected override void OnBarUpdate ()
                        {
                            if (
                this.ChartControl != _chartControl)
                            {
                                if (
                _chartControl != null)
                                    
                _chartControl.ChartPanel.MouseMove -= new System.Windows.Forms.MouseEventHandler (ChartControl_MouseMove);

                                
                _chartControl this.ChartControl;

                                if (
                _chartControl != null)
                                    
                _chartControl.ChartPanel.MouseMove += new System.Windows.Forms.MouseEventHandler (ChartControl_MouseMove);
                            }
                        }
                    }
                }

                #region NinjaScript generated code. Neither change nor remove.
                // This namespace holds all indicators and is required. Do not change it.
                namespace NinjaTrader.Indicator
                {
                    public 
                partial class Indicator IndicatorBase
                    
                {
                        private 
                XYIndicator [] cacheXYIndicator null;

                        private static 
                XYIndicator checkXYIndicator = new XYIndicator ();

                        
                /// <summary>
                        /// Enter the description of your new custom indicator here
                        /// </summary>
                        /// <returns></returns>
                        
                public XYIndicator XYIndicator ()
                        {
                            return 
                XYIndicator (Input);
                        }

                        
                /// <summary>
                        /// Enter the description of your new custom indicator here
                        /// </summary>
                        /// <returns></returns>
                        
                public XYIndicator XYIndicator (Data.IDataSeries input)
                        {
                            if (
                cacheXYIndicator != null)
                                for (
                int idx 0idx cacheXYIndicator.Lengthidx++)
                                    if (
                cacheXYIndicator [idx].EqualsInput (input))
                                        return 
                cacheXYIndicator [idx];

                            
                XYIndicator indicator = new XYIndicator ();
                            
                indicator.BarsRequired BarsRequired;
                            
                indicator.CalculateOnBarClose CalculateOnBarClose;
                            
                indicator.Input input;
                            
                indicator.SetUp ();

                            
                XYIndicator [] tmp = new XYIndicator [cacheXYIndicator == null cacheXYIndicator.Length 1];
                            if (
                cacheXYIndicator != null)
                                
                cacheXYIndicator.CopyTo (tmp0);
                            
                tmp [tmp.Length 1] = indicator;
                            
                cacheXYIndicator tmp;
                            
                Indicators.Add (indicator);

                            return 
                indicator;
                        }

                    }
                }

                // This namespace holds all market analyzer column definitions and is required. Do not change it.
                namespace NinjaTrader.MarketAnalyzer
                {
                    public 
                partial class Column ColumnBase
                    
                {
                        
                /// <summary>
                        /// Enter the description of your new custom indicator here
                        /// </summary>
                        /// <returns></returns>
                        
                [Gui.Design.WizardCondition ("Indicator")]
                        public 
                Indicator.XYIndicator XYIndicator ()
                        {
                            return 
                _indicator.XYIndicator (Input);
                        }

                        
                /// <summary>
                        /// Enter the description of your new custom indicator here
                        /// </summary>
                        /// <returns></returns>
                        
                public Indicator.XYIndicator XYIndicator (Data.IDataSeries input)
                        {
                            return 
                _indicator.XYIndicator (input);
                        }

                    }
                }

                // This namespace holds all strategies and is required. Do not change it.
                namespace NinjaTrader.Strategy
                {
                    public 
                partial class Strategy StrategyBase
                    
                {
                        
                /// <summary>
                        /// Enter the description of your new custom indicator here
                        /// </summary>
                        /// <returns></returns>
                        
                [Gui.Design.WizardCondition ("Indicator")]
                        public 
                Indicator.XYIndicator XYIndicator ()
                        {
                            return 
                _indicator.XYIndicator (Input);
                        }

                        
                /// <summary>
                        /// Enter the description of your new custom indicator here
                        /// </summary>
                        /// <returns></returns>
                        
                public Indicator.XYIndicator XYIndicator (Data.IDataSeries input)
                        {
                            if (
                InInitialize && input == null)
                                throw new 
                ArgumentException ("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

                            return 
                _indicator.XYIndicator (input);
                        }

                    }
                }
                #endregion 

                Comment


                  #9
                  Price isn't correct though

                  Hi Piersh, thanks for the info. One thing that I am concerned about is the fact that the "double price" is not correct when I print it to the Output window. Is this working a designed or is the maths wrong?

                  Comment


                    #10
                    Originally posted by traderT View Post
                    Hi Piersh, thanks for the info. One thing that I am concerned about is the fact that the "double price" is not correct when I print it to the Output window. Is this working a designed or is the maths wrong?
                    sorry, that line should be
                    PHP Code:
                                double price _min * (_max _min) / _chartBounds.Height

                    Comment


                      #11
                      I did some of my own maths :-)

                      I just added price to _min and it come out right as well, but your way is better - thanks again.

                      Comment


                        #12
                        MouseUp only?

                        Is there a way to do the calculations only on a mouse up, rather than using mousemove? Or should I continue to use mousemove and just grab the x and y coordinates on a mouseup?

                        Comment


                          #13
                          Originally posted by traderT View Post
                          Is there a way to do the calculations only on a mouse up, rather than using mousemove? Or should I continue to use mousemove and just grab the x and y coordinates on a mouseup?
                          sure, the math is the same. just listen on the MouseUp event instead...

                          Comment


                            #14
                            bukkan et al

                            I modified your great code Bukkan just so that it switches on and off effectively.
                            Unfortunately this code breaks in NT7 and I can't work out why - I have the same problem with another indicator- it works on a single chart but if you add another chart on the same panel the barclick returns the wrong number !!
                            Edit - the text still appears btw but it is shifted approx 2 bars to the left
                            If someone could help me with this I would be immensely grateful as it's unsupported.
                            Attached Files
                            Last edited by Mindset; 12-29-2009, 03:50 AM.

                            Comment


                              #15
                              Originally posted by Mindset View Post
                              I modified your great code Bukkan just so that it switches on and off effectively.
                              Unfortunately this code breaks in NT7 and I can't work out why - I have the same problem with another indicator- it works on a single chart but if you add another chart on the same panel the barclick returns the wrong number !!
                              Edit - the text still appears btw but it is shifted approx 2 bars to the left
                              If someone could help me with this I would be immensely grateful as it's unsupported.
                              thanks mindset.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by WHICKED, Today, 12:45 PM
                              2 responses
                              16 views
                              0 likes
                              Last Post WHICKED
                              by WHICKED
                               
                              Started by GussJ, 03-04-2020, 03:11 PM
                              15 responses
                              3,272 views
                              0 likes
                              Last Post xiinteractive  
                              Started by Tim-c, Today, 02:10 PM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by Taddypole, Today, 02:47 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post Taddypole  
                              Started by chbruno, 04-24-2024, 04:10 PM
                              4 responses
                              51 views
                              0 likes
                              Last Post chbruno
                              by chbruno
                               
                              Working...
                              X