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 bmartz, 03-12-2024, 06:12 AM
        4 responses
        32 views
        0 likes
        Last Post bmartz
        by bmartz
         
        Started by Aviram Y, Today, 05:29 AM
        4 responses
        12 views
        0 likes
        Last Post Aviram Y  
        Started by algospoke, 04-17-2024, 06:40 PM
        3 responses
        28 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by gentlebenthebear, Today, 01:30 AM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by cls71, Today, 04:45 AM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X