![]() |
This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com
|
|||||||
| Indicator Development Support for the development of custom indicators using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Senior Member
|
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 |
|
NinjaTrader Customer Service
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
|
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".
Austin
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Junior Member
Join Date: Sep 2011
Posts: 17
Thanks: 0
Thanked 0 times in 0 posts
|
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. |
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: Dec 2009
Location: Denver, CO, USA
Posts: 6,498
Thanks: 109
Thanked 291 times in 280 posts
|
Hello,
Thanks for the feedback I will forward it too development.
Brett
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Junior Member
Join Date: Jul 2011
Location: Sunshine Coast, QLD, Australia
Posts: 4
Thanks: 0
Thanked 2 times in 2 posts
|
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; 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);
}
}
Last edited by sprad001; 03-11-2012 at 12:17 AM.
|
|
|
|
|
The following user says thank you to sprad001 for this post: |
|
|
|
#6 |
|
Senior Member
Join Date: Aug 2011
Location: France
Posts: 268
Thanks: 24
Thanked 15 times in 15 posts
|
Sprad001,
when compiling, MouseEventArgs directive or reference is missing ? |
|
|
|
|
|
#7 |
|
Junior Member
Join Date: Jul 2011
Location: Sunshine Coast, QLD, Australia
Posts: 4
Thanks: 0
Thanked 2 times in 2 posts
|
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;
Last edited by sprad001; 03-08-2012 at 11:54 PM.
|
|
|
|
|
The following user says thank you to sprad001 for this post: |
|
|
|
#8 |
|
Senior Member
Join Date: Aug 2011
Location: France
Posts: 268
Thanks: 24
Thanked 15 times in 15 posts
|
Thanks,
it works great now
|
|
|
|
|
|
#9 |
|
Member
Join Date: Aug 2011
Posts: 45
Thanks: 11
Thanked 0 times in 0 posts
|
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 |
|
|
|
|
|
#10 |
|
Junior Member
Join Date: Jul 2011
Location: Sunshine Coast, QLD, Australia
Posts: 4
Thanks: 0
Thanked 2 times in 2 posts
|
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]); Code:
int referenceY2 = ChartControl.GetYByValue(BarsArray[0], Close[0] - TickSize); Code:
int tickSizeInPixels = referenceY1 - referenceY2; 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()); I hope this points you to the right direction.
Last edited by sprad001; 03-10-2012 at 05:31 PM.
|
|
|
|
|
|
#11 |
|
Member
Join Date: Aug 2011
Posts: 45
Thanks: 11
Thanked 0 times in 0 posts
|
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.
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 01:34 AM |
| Relative Bar Number | WolliWilli | Version 7 Beta General Questions & Bug Reports | 3 | 11-09-2009 08: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 06:48 AM |