Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Last Bar on Chart Not Executed?

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

    Last Bar on Chart Not Executed?

    Hi,

    I create a strategy whose purpose is to write into text certain indicator values, in the example below - the "High" values.

    However, I notice that the last bar indicator value is not printed. Therefore, I cannot get the latest reading, which is the most important.

    Any idea how to solve it?

    Code:
    #region Using declarations
    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.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    using System.IO;
    #endregion
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Write Bar State
        /// </summary>
        [Description("Write Bar State")]
        public class alvBarWriteBarState : Strategy
        {
            #region Variables
    			private bool			bInit				= true;
    			private string			Inst				= "";
    			private string			path				= "";
    
    		#endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                CalculateOnBarClose = true;
    			BarsRequired = 30;
    			
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    			
    			if (bInit) 
    			{
    				Inst = Instrument.MasterInstrument.Name;
    				path = @"C:\Users\Celebes\Documents\NinjaTrader 7\db\barstate\" + Inst + ".csv";
    				File.WriteAllText(path, "Time,BarState" + Environment.NewLine);
    				bInit = false;
    			}
    			
    			File.AppendAllText(path, ToDay(Time[0]) + "," + High[0] + Environment.NewLine);
    			
            }
    
            #region Properties
            #endregion
        }
    }

    #2
    Alvantage, please ensure to work with CalculateOnBarClose = false to get access to the last bar's values. With 'true' you would only have access to the last completed bar's value (one bar ago).



    Thanks,

    Comment


      #3
      Great! Thanks.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      649 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      370 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      109 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      574 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      576 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X