Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 Segwin, 05-07-2018, 02:15 PM
        14 responses
        1,789 views
        0 likes
        Last Post aligator  
        Started by Jimmyk, 01-26-2018, 05:19 AM
        6 responses
        838 views
        0 likes
        Last Post emuns
        by emuns
         
        Started by jxs_xrj, 01-12-2020, 09:49 AM
        6 responses
        3,294 views
        1 like
        Last Post jgualdronc  
        Started by Touch-Ups, Today, 10:36 AM
        0 responses
        13 views
        0 likes
        Last Post Touch-Ups  
        Started by geddyisodin, 04-25-2024, 05:20 AM
        11 responses
        63 views
        0 likes
        Last Post halgo_boulder  
        Working...
        X