Anybody know the trick when equidistant bar spacing is FALSE?
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Equidistant Bar Spacing FALSE
Collapse
X
-
Equidistant Bar Spacing FALSE
Seems like the DataBox is able to determine the bar index from the x mouse position whether Equidistant bar spacing is on or off.
Anybody know the trick when equidistant bar spacing is FALSE?Last edited by BigWaveDave; 05-28-2014, 01:10 PM. -
Sure Patrick, thanks for the quick reply.
I'm doing my own bar selection based on mouse location.
The charthelper class (undocumented as it may be) does provide a function to convert from mouse x-pos to barIdx... however, it's entirely dependent on equidistant spacing.
I'm trying to determine the barIdx under the mouse using the x-pos within the chart panel.
Databox does it.
Comment
-
Hello BigWaveDave,
Thank you for your response.
As you know this is unsupported and undocumented, and I unfortunately have no information in this area to provide. You may wish to look into one of the unsupported/undocumented threads started by our users on the forum: http://www.ninjatrader.com/support/f...d+undocumented
Comment
-
Ok. Nothing useful there.
Since there doesn't appear to be any public call that I can make, I'm going to have to approximate the barIdx using fixed with bar spacing... then call GetXByBarIdx() a bunch of times to narrow it down and zero in on the correct bar.
Not ideal to have that processing on every single mousemove.
Anybody else? Tell me I'm missing something obvious here...
Thanks Patrick!
Comment
-
Ok, here's the code... This is a function modified from charthelper.cs to convert mouse x-pos into a bar index whether or not EquidistantBarSpacing is enabled.
Patrick, this code works fine (almost, there's still an issue when the chart is scrolled), however, there seems to be an issue with the EquidistantBarSpacing flag when it's set to false.
When it is set to false and I first open a chart window the dataseries displays correctly, however, ChartControl.FirstBarPainted is ALWAYS -1.
In order to get FirstBarPainted to contain an accurate value, I need to go into 'Properties' for the chart and Enable, the re-Disable EquidistantBarSpacing.
Once I've done that, it works as expected.
Is this a bug, or am I missing something?
Code:public int ConvertXtoBarIdx(int x) { _debug = ""; if (chartControl == null) return 0; int numBarsOnCanvas = 0; int idxFirstBar = 0; int idxLastBar = 0; if (chartControl.LastBarPainted < Bars.Count) { numBarsOnCanvas = chartControl.LastBarPainted - chartControl.FirstBarPainted; idxFirstBar = chartControl.FirstBarPainted; idxLastBar = chartControl.LastBarPainted; } else { numBarsOnCanvas = Bars.Count - chartControl.FirstBarPainted; idxFirstBar = chartControl.FirstBarPainted; idxLastBar = Bars.Count - 1; } int barIndex; int firstBarX = chartControl.GetXByBarIdx(Bars, idxFirstBar); double dRatio; int halfBarWidth = (int)Math.Round(((double)(chartControl.BarSpace / (double)2)), 0, MidpointRounding.AwayFromZero); int margin = firstBarX + halfBarWidth; dRatio = 1 + ((x - margin) / (double)(chartControl.BarSpace)); int numberPeriods = (int)Math.Truncate(dRatio); barIndex = idxFirstBar + numberPeriods; _debug = "BarIndex = " + barIndex + " NumBarsCanvas= " + numBarsOnCanvas + " FirstBar=" + idxFirstBar + " LastBar=" + idxLastBar + " Ratio = " + dRatio + " NumPeriods =" + numberPeriods; // Scootch up to where we really wanna be if (!chartControl.EquidistantBars) { int closeX = chartControl.GetXByBarIdx(Bars, barIndex); if (closeX < x) { while ((closeX < x) && (barIndex < chartControl.LastBarPainted)) { barIndex++; closeX = chartControl.GetXByBarIdx(Bars, barIndex); } barIndex--; } else { while ((closeX > x) && (barIndex > chartControl.FirstBarPainted)) { barIndex--; closeX = chartControl.GetXByBarIdx(Bars, barIndex); } } } return barIndex; }Last edited by BigWaveDave; 05-27-2014, 06:42 PM.
Comment
-
Solved
Ok, as Patrick points out this is all undocumented. So use it at your own risk.
As it turns out ChartControl.FirstBarPainted and ChartControl.LastBarPainted are only valid with EquidistantBars set to TRUE.
Conversely, ChartControl.LastBarTimePainted is only valid with EquidistantBars set to FALSE.
Fortunately, this is just enough to get the job done.
I've also included a bonus function to the get last visible valid bar index regardless of the EquidistantBars mode.
Again, this code has been leveraged from charthelper.cs available elsewhere on this site.
Code:public int ConvertXtoBarIdx(int x) { _debug = ""; if (chartControl == null) return 0; int numBarsOnCanvas = 0; int idxFirstBar = 0; int idxLastBar = 0; int barIndex; int firstBarX; double dRatio; int halfBarWidth = (int)Math.Round(((double)(chartControl.BarSpace / (double)2)), 0, MidpointRounding.AwayFromZero); if (!chartControl.EquidistantBars) { // This appears to always be valid when bars are NOT equidistant idxLastBar = Bars.GetBar(chartControl.LastBarTimePainted); barIndex = idxLastBar; firstBarX = chartControl.GetXByBarIdx(Bars, barIndex); // In my case I detect if the mouse is beyond the rightmost bar in the chart by testing it against the last VisibleBar as calculated // by the helper function below... // so if the mouse is beyond the rightmost bar, let's return a barIndex that is beyond the rightmost valid visible bar index... if (x > (firstBarX + halfBarWidth)) return (idxLastBar + 1); while (firstBarX > (x+halfBarWidth)) { barIndex--; firstBarX = chartControl.GetXByBarIdx(Bars, barIndex); } // firstBarX will be the exact center of the bar... // we want to return the same index for the entire bar so we account for that... if (x < (firstBarX - halfBarWidth)) barIndex--; else if (x > (firstBarX + halfBarWidth)) barIndex++; } else { if (chartControl.LastBarPainted < Bars.Count) { numBarsOnCanvas = chartControl.LastBarPainted - chartControl.FirstBarPainted; idxFirstBar = chartControl.FirstBarPainted; idxLastBar = chartControl.LastBarPainted; } else { numBarsOnCanvas = Bars.Count - chartControl.FirstBarPainted; idxFirstBar = chartControl.FirstBarPainted; idxLastBar = Bars.Count - 1; } firstBarX = chartControl.GetXByBarIdx(Bars, idxFirstBar); int margin = firstBarX + halfBarWidth; dRatio = 1 + ((x - margin) / (double)(chartControl.BarSpace)); int numberPeriods = (int)Math.Truncate(dRatio); barIndex = idxFirstBar + numberPeriods; _debug = "BarIndex = " + barIndex + " NumBarsCanvas= " + numBarsOnCanvas + " FirstBar=" + idxFirstBar + " LastBar=" + idxLastBar + " Ratio = " + dRatio + " NumPeriods =" + numberPeriods; } return barIndex; } public int GetLastValidVisibleBarIdx() { int idxLastBar; if (!chartControl.EquidistantBars) idxLastBar = Bars.GetBar(chartControl.LastBarTimePainted); else idxLastBar = chartControl.LastBarPainted; return Math.Min(idxLastBar, Bars.Count - 1); }
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
558 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
324 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
101 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
545 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
547 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment