Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

GetBar Always Returns 0

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

    GetBar Always Returns 0

    Hello:

    I read a thread that said CurrentBar could not be relied on to be in sync within the OnMarketData method. So I am trying to store my custom bar data - indexed by barID - by getting the bar ID based on the time in the MarketDataEventArgs.

    The problem is: absolutely every time I invoke GetBar it returns 0.

    Here is the test case:


    Code:
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    
    namespace NinjaTrader.Indicator
    {
    	public class MarketDataTest : Indicator
    	{
    		protected override void Initialize() {
    			Overlay = true;
    			CalculateOnBarClose = false;
    		}
    
    		protected override void OnBarUpdate() {
    			Print("");
    			Print("OnBarUpdate()");
    			Print("   DateTime.Now=" + DateTime.Now);
    			Print("   CurrentBar=" + CurrentBar);
    			
    			// ALWAYS 0!
    			Print("   GetBar(DateTime.Now)=" + GetBar(DateTime.Now));
    			// Assert:
    			if (GetBar(DateTime.Now) != 0) throw new InvalidOperationException("Never thrown.");
    			
    			Print("      Time[0]=" + Time[0]);
    			Print("      GetBar(Time[0])=" + GetBar(Time[0]));
    			// Assert:
    			if (GetBar(Time[0]) != 0) throw new InvalidOperationException("Never thrown.");
    		}
    		
    		protected override void OnMarketData(MarketDataEventArgs evt) {
    			Print("");
    			Print("\tOnMarketData()");
    			Print("\t   evt=" + evt);
    			Print("\t   CurrentBar=" + CurrentBar);
    			
    			// I read a thread that said CurrentBar could not be relied on to be in sync
    			// within the OnMarketData method.
    			// So I am trying to store my custom bar data - indexed by barID - by getting
    			// the bar ID based on the time in the MarketDataEventArgs.
    			
    			// NinjaScript > Language Reference > Data > Bars >
    			// GetBar()
    			// Definition
    			// Returns the bar index of the 1st bar from oldest to newest
    			// that matches the time stamp based on the DateTime parameter passed in.
    			// If the time stamp passed in is older than the 1st bar,
    			// a bar index of 0 is returned.
    			// If the time stamp is newer than the last bar, the last bar index is returned.
    			
    			// ALWAYS 0!
    			Print("\t   GetBar(evt.Time)=" + GetBar(evt.Time));
    			// Assert:
    			if (GetBar(evt.Time) != 0) throw new InvalidOperationException("Never thrown.");
    		}
    	}
    }

    #2
    I seem to have essentially solved this for myself: Bars.GetBar is returning what I expected.

    2 questions remain:

    1) Was I correct about CurrentBar being unreliable in sequence from the OnMarketData method?

    2) Should the documentation for GetBar be updated to be clearer? (I think so ...)
    Last edited by steevcoco; 04-29-2014, 08:04 AM.

    Comment


      #3
      Originally posted by steevcoco View Post
      I seem to have essentially solved this for myself: Bars.GetBar is returning what I expected.

      2 questions remain:

      1) Was I correct about CurrentBar being unreliable in sequence from the OnMarketData method?

      2) Should the documentation for GetBar be updated to be clearer? (I think so ...)
      GetBar() and Bars.GetBar() are different methods, belonging to different objects. GetBar() is not documented: it returns essentially the opposite of Bars.GetBar(); returning the number of barsAgo rather than the index of the referenced bar, which is what Bars.GetBar() returns.
      Last edited by koganam; 01-08-2017, 07:40 PM. Reason: Clarified grammar.

      Comment


        #4
        I see this now: the documentation DOES give the syntax Bars.GetBar(DateTime time) I missed that at first. Yet still, I see no documentation for the method I was using. It might still be good to update the documentation for disambiguation ... Thanks for the reply. This part of my code is working now.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        596 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        343 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        103 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        556 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        554 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X