Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Query about BarsRequest

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

    Query about BarsRequest



    The sample code for processing returned bars has a comment which states:
    "Note: The last returned bar may be a currently in-progress bar"

    How can I determine if the last bar is in-progress?
    And how can I determine the same if I subscribe in realtime via barsRequest.Update
    Last edited by reach4thelasers; 10-05-2015, 10:41 AM.

    #2
    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):

    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();

    }));
    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.

    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 );
    }

    Comment


      #3
      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 DJ888, 04-16-2024, 06:09 PM
      4 responses
      12 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by terofs, Today, 04:18 PM
      0 responses
      11 views
      0 likes
      Last Post terofs
      by terofs
       
      Started by nandhumca, Today, 03:41 PM
      0 responses
      7 views
      0 likes
      Last Post nandhumca  
      Started by The_Sec, Today, 03:37 PM
      0 responses
      3 views
      0 likes
      Last Post The_Sec
      by The_Sec
       
      Started by GwFutures1988, Today, 02:48 PM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_Clayton  
      Working...
      X