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 do I access the bar number under the cursor?

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

    How do I access the bar number under the cursor?

    The DataBox evidently dynamically accesses the bar on which the cursor is hovering. That is how it can update the values in the box.

    I need to access and update text based on the bar number on which the cursor is resting or hovering over.

    Does anyone know how to bet that information? I thought that it might be in the Bars property, but intellisense displays nothing that looks like it might be it.

    #2
    koganam, doing what you seek will require accessing and utilizing unsupported NinjaScript methods. You will also need to work with mouse events. I'm pretty sure this topic has been discussed before right here on the forums. I suggest you do a search for something like "mouse events" or even just "mouse".
    AustinNinjaTrader Customer Service

    Comment


      #3
      Hello,
      The next release of Multicharts will allow to get data of selected bar within an indicator or strategy (I have checked it, it works - see MouseClickBarNumber at http://www.multicharts.com/traders-blog/?p=540). I have seen some posts about similar requests in NT forums. At the moment the only possible solution is a manual calculation of the (x,y) of the mouse on the chart. Not really easy... I think it would be nice to have this feature in NinjaScript.
      Best regards.

      Comment


        #4
        Hello,

        Thanks for the feedback I will forward it too development.
        BrettNinjaTrader Product Management

        Comment


          #5
          mouse event, bar index, bar number

          koganam,

          I also needed to find the bar index based on the x coordinate and the solutions I found and tried before did not work accurately. I wrote this one today and it works well. It is accurate even with BarWidth 1 at BarSpace 3, if you have steady enough hand with the mouse at that level
          This one requires to click on the bar.

          Under using declarations add:
          Code:
          using System.Windows.Forms;
          declare variables:
          Code:
          private bool	click 				= false;
          private bool	dataToOutputEnabled	= true;
          Code:
                 private void myMouseEvents(object sender, MouseEventArgs e)
          	{
          		TriggerCustomEvent(new CustomEvent(myCustomEvent),e );
          	}
          		
          		
          	private int ConvertXcoordinateToBarIdx(int x)
                  {
                      if (ChartControl == null)
                          return 0;
          			
          	    int idxSelectedBar		= 0;
                      int idxFirstBar 		= ChartControl.FirstBarPainted;
                      int idxLastBar 		= ChartControl.LastBarPainted;
                      int totalNoOfBars	 	= Bars.Count - 1;
                      int firstBarX 			= ChartControl.GetXByBarIdx(Bars,idxFirstBar);
          	    int lastBarX 			= ChartControl.GetXByBarIdx(Bars,idxLastBar);
          	    int pixelRangeOfBars 	= lastBarX - firstBarX + 1;
          	    int selectedBarNoScreen= (int)(Math.Round(((double)(x - firstBarX)) / (ChartControl.BarSpace), 0));
          	    int SelectedBarNumber 	= idxFirstBar + selectedBarNoScreen;
          			
          		if (x <= firstBarX)
          			idxSelectedBar = totalNoOfBars - idxFirstBar;
          		else if (x >= lastBarX)
          			idxSelectedBar = totalNoOfBars - idxLastBar;
          		else
          			idxSelectedBar = totalNoOfBars - SelectedBarNumber;
          			
          		if (dataToOutputEnabled) // Display info in Output window if enabled 
          		{
          			Print("------------------------------");
                                  Print("Instrument: " + Instrument.FullName + ",    Chart: " +  BarsPeriod.ToString()); // To identify which chart the data was coming from									
          			Print("Mouse Coordinate X: " + x.ToString());
          			Print("Total Bars On Chart: " + totalNoOfBars.ToString());
          			Print("BarWidth: " + ChartControl.BarWidth.ToString());
          			Print("BarSpace: " + ChartControl.BarSpace.ToString());
          			Print("Firs Bar Idx On Screen: " + idxFirstBar.ToString());
          			Print("Last Bar Idx On Screen: " + idxLastBar.ToString());
          			Print("No Of Bars On Screen: " + (idxLastBar - idxFirstBar).ToString());
          			Print("First (Left) On Screen Bar X Coordinate: " + firstBarX.ToString());
          			Print("Last (Right) On Screen Bar X Coordinate: " + lastBarX.ToString());
          			Print("Pixel Range Of Visible Bars: " + pixelRangeOfBars.ToString());
          			Print("Bar Position On Screen From Left: " + selectedBarNoScreen.ToString());
          			Print("Selected Bar Number: " + SelectedBarNumber.ToString());
          			Print("Selected Bar Index: " + idxSelectedBar.ToString());
          		}
          			
          		return idxSelectedBar;
                  }
          
                  private void myCustomEvent(object state)
          	{
          		MouseEventArgs m 	= (MouseEventArgs)state;		
          		int xPos 			= m.X;  // mouse X coordinate
          		int yPos			= m.Y; // mouse Y coordinate but we do not use it here
                          int barPosition           = ConvertXcoordinateToBarIdx(xPos);
                          Print(Close[barPosition].ToString()); // prints Close price of the clicked bar into the output window
                                  // write your own code here
                   }
          
                 protected override void OnBarUpdate()
                  {			
          		if (!click)
          		{
          			click = true;
          			this.ChartControl.ChartPanel.MouseUp += new MouseEventHandler(myMouseEvents);	
          		}		
                  }
          I hope this helps
          Last edited by sprad001; 03-11-2012, 01:17 AM.

          Comment


            #6
            Sprad001,
            when compiling, MouseEventArgs directive or reference is missing ?

            Comment


              #7
              mate41,

              Sorry about that, my bad. I forgot to show that you need to declare this at the start of your code. I fixed this up in the previous post, now it is all correct there.
              Code:
              using System.Windows.Forms;
              try to compile it now, it should work.
              Last edited by sprad001; 03-09-2012, 12:54 AM.

              Comment


                #8
                Thanks,
                it works great now

                Comment


                  #9
                  Hello sprad001,

                  I am basically looking for a way to be able to assign the price value I click on on a chart to a variable in an indicator. For example, if I have a variable in my custom indicator named entry price, then anytime I click on a price on the chart, this variable is assigned that price value. I am a novice when it comes to NTScripts and C#, and while searching for a solution on the forum to my problem, I was directed to this thread with your code. I have copied and compiled you code and added it to a chart to see how it works or what it is supposed to do, so as to see whether it is close to what I want and how I could possibly amend it to do what I want, but alas I do not seem to understand how it works or what it's meant to do. Would you mind explaining what your code is meant to do or how to be used please?

                  Many thanks
                  Dan

                  Comment


                    #10
                    price from Y mouse coordinate

                    Hi rabcnesbit,

                    The code in the previous post is basically demonstrates how you can get, calculate a bar index by clicking with the mouse on a bar or targeting the vertical line of the Cross Hair on the bar you want. To determine the bar number we only evaluate the X axis pixel data. I put in the code the Y axis pixel data for the mouse as well to demonstrate that too but the value is not used in this code as it was not relevant to this exercise.
                    Once you know the bar index then you can get a multitude of information related to that bar.

                    As for how to get price from the Y mouse data I did not look into it yet as I did not need it but I would start with something like getting a price read from the chart and get its Y pixel value, coordinate using the GetYByValue
                    Code:
                    int referenceY1 = ChartControl.GetYByValue(BarsArray[0], Close[0]);
                    as an example. Then take 1 TickSize value away from this Close[0] price and get the Y value for this second price,
                    Code:
                    int referenceY2 = ChartControl.GetYByValue(BarsArray[0], Close[0] - TickSize);
                    The difference of the two would show you how many pixels 1 tick is on the Y axis.
                    Code:
                    int tickSizeInPixels = referenceY1 - referenceY2;
                    This way you get your reference point with the Close[0] price and from there on with the known pixel size per tick you can calculate the prices from the mouse position. All this assuming that you have the Close[0] on screen which you need to check for. If you stretch the vertical axis with the mouse the Close[0] can get off the screen.
                    I would strongly suggest to read this thread and read David Lean's excellent, detailed explanation about plots, coordinates. You need to have a clear understanding of this before you can write functional code for your problem.
                    This is just my line of thinking and I have not tried this yet.
                    I suggest when testing your code write the results to the Output Window with the
                    Code:
                    Print(tickSizeInPixels.ToString());
                    as an example, to see what happens.

                    I hope this points you to the right direction.
                    Last edited by sprad001; 03-10-2012, 06:31 PM.

                    Comment


                      #11
                      sprad001,

                      Many thanks for your reply. Looks like this is more difficult than I thought, and looks a bit too advance for me at this stage. Will just have to survive without it for now.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by focus333, Today, 05:34 PM
                      1 response
                      25 views
                      0 likes
                      Last Post bltdavid  
                      Started by RaddiFX, Today, 07:48 PM
                      0 responses
                      8 views
                      0 likes
                      Last Post RaddiFX
                      by RaddiFX
                       
                      Started by smcllr, Today, 05:41 PM
                      0 responses
                      23 views
                      0 likes
                      Last Post smcllr
                      by smcllr
                       
                      Started by trader3000a, Today, 02:28 PM
                      1 response
                      24 views
                      0 likes
                      Last Post NinjaTrader_Kate  
                      Started by mcosta72, Today, 02:25 PM
                      0 responses
                      19 views
                      0 likes
                      Last Post mcosta72  
                      Working...
                      X