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

Indicator not printing the last bar to the output window

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

    Indicator not printing the last bar to the output window

    I am using the below code to print certain indicators to the output window, however it never prints data for the last bar in a contract??

    Anyone know why?

    Thanks

    Code:

    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// Prints the following insicators to the output window:- High, low, MACD, BolengerBands, 50EMA, 200EMA
    /// </summary>
    [Description("Prints the following insicators to the output window:- High, low, MACD, BolengerBands, 50EMA, 200EMA")]
    public class IndicatorPrinter : Indicator
    {
    #region Variables
    // Wizard generated variables
    private int myInput0 = 1; // Default setting for MyInput0
    // User defined variables (add any user defined variables below)
    #endregion

    /// <summary>
    /// This method is used to configure the indicator and is called once before any bar data is loaded.
    /// </summary>
    protected override void Initialize()
    {
    Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
    Overlay = false;


    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {

    //double bolHigh, bolMid, bolLow = Bolenger (2,29) ;
    double bolHigh = Bollinger(2,29).Upper[0];
    double bolMid = Bollinger(2,29).Middle [0];
    double bolLow = Bollinger(2,29).Lower [0];

    //double macd, macdAvg, macdDiff = (6,13,9) ;
    double macd = MACD(6,13,9) [0];
    double macdAvg = MACD(6,13,9).Avg [0];
    double macdDiff = MACD(6,13,9).Diff [0];

    //double ema200 = EMA(200)/ double ema50 = EMA(50);
    double ema200 = EMA(200)[0];
    double ema50 = EMA(50)[0];

    //Prints the indicators above to the output window ;
    Print("" + Time[0] + " " + High[0] + " " + Low[0] + " " + bolHigh.ToString("N2") + " " + bolMid.ToString("N2") + " " + bolLow.ToString("N2") + " " + macd.ToString("N3") + " " + macdAvg.ToString("N3") + " " + macdDiff.ToString("N3") + " " + ema50.ToString("N2") + " " + ema200.ToString("N2"));

    //
    // Use this method for calculating your indicator values. Assign a value to each
    // plot below by replacing 'Close[0]' with your own formula.
    Plot0.Set(High[0]);

    }

    #region Properties
    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries Plot0
    {
    get { return Values[0]; }
    }



    [Description("")]
    [GridCategory("Parameters")]
    public int MyInput0
    {
    get { return myInput0; }
    set { myInput0 = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #region NinjaScript generated code. Neither change nor remove.
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    public partial class Indicator : IndicatorBase
    {
    private IndicatorPrinter[] cacheIndicatorPrinter = null;

    private static IndicatorPrinter checkIndicatorPrinter = new IndicatorPrinter();

    /// <summary>
    /// Prints the following insicators to the output window:- High, low, MACD, BolengerBands, 50EMA, 200EMA
    /// </summary>
    /// <returns></returns>
    public IndicatorPrinter IndicatorPrinter(int myInput0)
    {
    return IndicatorPrinter(Input, myInput0);
    }

    /// <summary>
    /// Prints the following insicators to the output window:- High, low, MACD, BolengerBands, 50EMA, 200EMA
    /// </summary>
    /// <returns></returns>
    public IndicatorPrinter IndicatorPrinter(Data.IDataSeries input, int myInput0)
    {
    if (cacheIndicatorPrinter != null)
    for (int idx = 0; idx < cacheIndicatorPrinter.Length; idx++)
    if (cacheIndicatorPrinter[idx].MyInput0 == myInput0 && cacheIndicatorPrinter[idx].EqualsInput(input))
    return cacheIndicatorPrinter[idx];

    lock (checkIndicatorPrinter)
    {
    checkIndicatorPrinter.MyInput0 = myInput0;
    myInput0 = checkIndicatorPrinter.MyInput0;

    if (cacheIndicatorPrinter != null)
    for (int idx = 0; idx < cacheIndicatorPrinter.Length; idx++)
    if (cacheIndicatorPrinter[idx].MyInput0 == myInput0 && cacheIndicatorPrinter[idx].EqualsInput(input))
    return cacheIndicatorPrinter[idx];

    IndicatorPrinter indicator = new IndicatorPrinter();
    indicator.BarsRequired = BarsRequired;
    indicator.CalculateOnBarClose = CalculateOnBarClose;
    #if NT7
    indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
    indicator.MaximumBarsLookBack = MaximumBarsLookBack;
    #endif
    indicator.Input = input;
    indicator.MyInput0 = myInput0;
    Indicators.Add(indicator);
    indicator.SetUp();

    IndicatorPrinter[] tmp = new IndicatorPrinter[cacheIndicatorPrinter == null ? 1 : cacheIndicatorPrinter.Length + 1];
    if (cacheIndicatorPrinter != null)
    cacheIndicatorPrinter.CopyTo(tmp, 0);
    tmp[tmp.Length - 1] = indicator;
    cacheIndicatorPrinter = tmp;
    return indicator;
    }
    }
    }
    }

    // This namespace holds all market analyzer column definitions and is required. Do not change it.
    namespace NinjaTrader.MarketAnalyzer
    {
    public partial class Column : ColumnBase
    {
    /// <summary>
    /// Prints the following insicators to the output window:- High, low, MACD, BolengerBands, 50EMA, 200EMA
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.IndicatorPrinter IndicatorPrinter(int myInput0)
    {
    return _indicator.IndicatorPrinter(Input, myInput0);
    }

    /// <summary>
    /// Prints the following insicators to the output window:- High, low, MACD, BolengerBands, 50EMA, 200EMA
    /// </summary>
    /// <returns></returns>
    public Indicator.IndicatorPrinter IndicatorPrinter(Data.IDataSeries input, int myInput0)
    {
    return _indicator.IndicatorPrinter(input, myInput0);
    }
    }
    }

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    public partial class Strategy : StrategyBase
    {
    /// <summary>
    /// Prints the following insicators to the output window:- High, low, MACD, BolengerBands, 50EMA, 200EMA
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.IndicatorPrinter IndicatorPrinter(int myInput0)
    {
    return _indicator.IndicatorPrinter(Input, myInput0);
    }

    /// <summary>
    /// Prints the following insicators to the output window:- High, low, MACD, BolengerBands, 50EMA, 200EMA
    /// </summary>
    /// <returns></returns>
    public Indicator.IndicatorPrinter IndicatorPrinter(Data.IDataSeries input, int myInput0)
    {
    if (InInitialize && input == null)
    throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

    return _indicator.IndicatorPrinter(input, myInput0);
    }
    }
    }
    #endregion

    #2
    Hello GKonheiser,
    Please set CalculateOnBarClose to false to print out the values of the last/running bar. You can do it either by setting CalculateOnBarClose to false from the indicator parameter box or append the below code in the Initialzie section of the code:
    Code:
    CalculateOnBarClose = false;


    Please let me know if I can assist you any further.
    Attached Files
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Does this have any implications for all of the other data, does changing that setting alter the other data?

      Comment


        #4
        Hello GKonheiser,
        It will not alter the data, but it will calculate the indicator on every incoming tick, which can be resource heavy if you have a complex indicator.

        If you set CalculateOnBarClose to true, it will calculate the values only once, at the end of the bar.
        JoydeepNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by algospoke, Yesterday, 06:40 PM
        2 responses
        23 views
        0 likes
        Last Post algospoke  
        Started by ghoul, Today, 06:02 PM
        3 responses
        14 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by jeronymite, 04-12-2024, 04:26 PM
        3 responses
        45 views
        0 likes
        Last Post jeronymite  
        Started by Barry Milan, Yesterday, 10:35 PM
        7 responses
        22 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by AttiM, 02-14-2024, 05:20 PM
        10 responses
        181 views
        0 likes
        Last Post jeronymite  
        Working...
        X