Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Query about BarsRequest
Collapse
X
-
Tags: None
-
Ok so here's what I ended up doing - to save anyone else the bother of spending hours figuring this out:
For historical bars i.e. the callback from the Request() method of BarsRequest:
(not sure this is correct but its what I'm going with... until I hear otherwise)
There is a property on the BarsRequest object in the callback: Bars.BarsSeries.LastBarTime - which contains the Time of the latest bar to be processed. Most of the time when I was inspecting the properties of BarsRequest this was in the Future - because the last bar was in progress, and was due to close at "LastBarTime".
So I'm looping through the bars in the response, starting the for loop at 1 not zero (so skipping the first one - because we are going to always get the previous bar):
Process bar i-1 - skip the last one as its probably in-progress. There's a small risk with this method, that we will exclude the last bar which is actually closed and we want to include it. But I couldn't figure out another way.var barsPeriodFactory = new BarsPeriodFactory(_context);
_higherBarsRequest = new BarsRequest(_context.Instrument, _settings.HigherTimeFrameBars);
_higherBarsRequest.BarsPeriod = barsPeriodFactory.HigherTimeframePeriod;
_higherBarsRequest.TradingHours = TradingHours.Get("Forex");
_higherBarsRequest.Update += OnHigherBarUpdate;
_higherBarsRequest.Request(new Action<BarsRequest, ErrorCode, string>((bars, errorCode, errorMessage) =>
{
if (errorCode != ErrorCode.NoError)
{
NinjaTrader.Code.Output.Process(string.Format("Err or on requesting bars: {0}, {1}", errorCode, errorMessage),
PrintTo.OutputTab1);
return;
}
NinjaTrader.Code.Output.Process(string.Format("{0} Higher Timeframe Bars have been received. Dispatching to Higher Pipeline for processing", bars.Bars.Count), PrintTo.OutputTab1);
for (int i = 1; i < bars.Bars.Count; i++)
{
if (bars.Bars.BarsSeries.LastBarTime != bars.Bars.GetTime(i))
{
var candle = new Candle(bars.Bars.GetHigh(i-1), bars.Bars.GetLow(i-1), bars.Bars.GetOpen(i-1), bars.Bars.GetClose(i-1), bars.Bars.GetTime(i-1), bars.Bars.GetVolume(i-1), i-1);
var args = new NewCandleEventArgs(candle);
HigherTimeframeEventDispatcher.Dispatch(args);
}
}
NinjaTrader.Code.Output.Process(string.Format("Fin ished processing Higher Timeframe bars", bars.Bars.Count), PrintTo.OutputTab1);
CommonAssemblyMetaRenderer.UpdateChartStatusInfo() ;
_context.InvalidateVisual();
}));
Realtime updates with the Update Handler are a bit more definite because we got a "TickCount" property. We check if this is 1 - meaning its the first tick of a new bar. so the previous one just closed. Therefore process bar i-1:
if (e.BarsSeries.TickCount != 1) return;
for (int i = e.MinIndex; i <= e.MaxIndex; i++)
{
// Processing every single tick
NinjaTrader.Code.Output.Process(string.Format("DIS PATCHING NEW INTERMEDIATE TIMEFRAMEBAR: Time: {0} Open: {1} High: {2} Low: {3} Close: {4}",
e.BarsSeries.GetTime(i-1),
e.BarsSeries.GetOpen(i-1),
e.BarsSeries.GetHigh(i-1),
e.BarsSeries.GetLow(i-1),
e.BarsSeries.GetClose(i-1)), PrintTo.OutputTab1);
var candle = new Candle(e.BarsSeries.GetHigh(i - 1), e.BarsSeries.GetLow(i - 1), e.BarsSeries.GetOpen(i - 1), e.BarsSeries.GetClose(i - 1), e.BarsSeries.GetTime(i - 1), e.BarsSeries.GetVolume(i - 1), i);
var args = new NewCandleEventArgs(candle);
IntermediateTimeframeEventDispatcher.Dispatch(args );
}
-
Hi reach4thelasers,
Thanks for your contribution, it is likely that fellow NT8 users will get some use out of this post.<span class="name">Alex C.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
666 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
376 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
110 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
575 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
580 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment