Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 01-16-2010, 01:27 AM   #1
koganam
Senior Member
 
Join Date: Feb 2008
Location: http://omegasigmaindicators.com
Posts: 4,719
Thanks: 31
Thanked 1,951 times in 1,595 posts
Send a message via Skype™ to koganam
Default 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.
koganam is offline  
Reply With Quote
Old 01-16-2010, 12:59 PM   #2
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 92 times in 82 posts
Default

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".
NinjaTrader_Austin is offline  
Reply With Quote
Old 02-22-2012, 05:00 AM   #3
fpa75
Member
 
Join Date: Sep 2011
Location: Paris (France)
Posts: 48
Thanks: 4
Thanked 0 times in 0 posts
Default

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.
fpa75 is offline  
Reply With Quote
Old 02-22-2012, 09:27 AM   #4
NinjaTrader_Brett
NinjaTrader Customer Service
 
NinjaTrader_Brett's Avatar
 
Join Date: Dec 2009
Location: Denver, CO, USA
Posts: 6,568
Thanks: 111
Thanked 314 times in 299 posts
Default

Hello,

Thanks for the feedback I will forward it too development.
NinjaTrader_Brett is offline  
Reply With Quote
Old 02-28-2012, 08:26 AM   #5
sprad001
Junior Member
 
Join Date: Jul 2011
Location: Sunshine Coast, QLD, Australia
Posts: 4
Thanks: 1
Thanked 2 times in 2 posts
Default 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 at 01:17 AM.
sprad001 is offline  
Reply With Quote
The following user says thank you to sprad001 for this post:
Old 03-08-2012, 05:39 PM   #6
mate41
Senior Member
 
Join Date: Aug 2011
Location: France
Posts: 277
Thanks: 27
Thanked 15 times in 15 posts
Default

Sprad001,
when compiling, MouseEventArgs directive or reference is missing ?
mate41 is offline  
Reply With Quote
Old 03-09-2012, 12:45 AM   #7
sprad001
Junior Member
 
Join Date: Jul 2011
Location: Sunshine Coast, QLD, Australia
Posts: 4
Thanks: 1
Thanked 2 times in 2 posts
Default

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 at 12:54 AM.
sprad001 is offline  
Reply With Quote
The following user says thank you to sprad001 for this post:
Old 03-09-2012, 09:25 AM   #8
mate41
Senior Member
 
Join Date: Aug 2011
Location: France
Posts: 277
Thanks: 27
Thanked 15 times in 15 posts
Default

Thanks,
it works great now
mate41 is offline  
Reply With Quote
Old 03-09-2012, 04:16 PM   #9
rabcnesbit
Member
 
Join Date: Aug 2011
Posts: 45
Thanks: 11
Thanked 0 times in 0 posts
Default

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
rabcnesbit is offline  
Reply With Quote
Old 03-10-2012, 03:21 PM   #10
sprad001
Junior Member
 
Join Date: Jul 2011
Location: Sunshine Coast, QLD, Australia
Posts: 4
Thanks: 1
Thanked 2 times in 2 posts
Default 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 at 06:31 PM.
sprad001 is offline  
Reply With Quote
Old 03-10-2012, 05:53 PM   #11
rabcnesbit
Member
 
Join Date: Aug 2011
Posts: 45
Thanks: 11
Thanked 0 times in 0 posts
Default

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.
rabcnesbit is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Req: Access to number of historical bars processing tazatek Suggestions And Feedback 2 11-17-2009 02:34 AM
Relative Bar Number WolliWilli Version 7 Beta General Questions & Bug Reports 3 11-09-2009 09:21 AM
number of orders remaining in a bar... Fritztx Charting 4 04-28-2009 04:56 AM
bar number of viewable bars? foxthorn Indicator Development 3 09-25-2008 04:22 PM
Bar index from the session or plot bar number on an intraday chart eneratom General Programming 1 02-27-2008 07:48 AM


All times are GMT -6. The time now is 02:23 AM.

FULL RISK DISCLOSURE: Futures trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.