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 wont plot

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

    Indicator wont plot


    Hello,

    This is my first post. I am new to code but mase a simple strategy to test out. It worked and plotted as expected. Converted it to an indicator. Indicator will not plot. I looked through some tips and updated as such. It compiles just fine. Can someone help me to see what I am missing. Sorry if this has been answered before. And not sure if posted correctly. New to forums in general.

    Thank you in advance for any help.

    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.Gui.Chart;
    #endregion

    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    [Description("Enter the description of your new custom indicator here")]
    public class Indicator5 : Indicator
    {
    region Variables
    // Wizard generated variables
    private int fast = 8; // Default setting for Fast
    private int slow = 13; // Default setting for Slow
    private int smooth = 11; // Default setting for Smooth
    private int aroonPeriod = 14; // Default setting for AroonPeriod
    // 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 = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    {if (CurrentBar < 1)
    return;

    if (Close[0] > Close[1])
    // Do something



    // Condition set 1
    if (MACD(Fast, Slow, Smooth)[0] >= MACD(Fast, Slow, Smooth).Avg[0]
    && Aroon(AroonPeriod).Up[0] >= Aroon(AroonPeriod).Down[0])
    {
    BackColorAll = Color.PaleGreen;
    }

    // Condition set 2
    if (MACD(Fast, Slow, Smooth)[0] <= MACD(Fast, Slow, Smooth).Avg[0]
    && Aroon(AroonPeriod).Up[0] <= Aroon(AroonPeriod).Down[0])
    {
    BackColorAll = Color.RosyBrown;
    }

    // Condition set 3
    if (MACD(Fast, Slow, Smooth)[0] >= MACD(Fast, Slow, Smooth).Avg[0]
    && Aroon(AroonPeriod).Up[0] >= Aroon(AroonPeriod).Down[0]
    && MACD(BarsArray[1],Fast, Slow, Smooth)[0] >= MACD(BarsArray[1],Fast, Slow, Smooth).Avg[0]
    && Aroon(BarsArray[1],AroonPeriod).Up[0] >= Aroon(BarsArray[1],AroonPeriod).Down[0])
    {
    BackColorAll = Color.CornflowerBlue;
    }

    // Condition set 4
    if (MACD(Fast, Slow, Smooth)[0] <= MACD(Fast, Slow, Smooth).Avg[0]
    && Aroon(AroonPeriod).Up[0] <= Aroon(AroonPeriod).Down[0]

    && MACD(BarsArray[1],Fast, Slow, Smooth)[0] <= MACD(BarsArray[1],Fast, Slow, Smooth).Avg[0]
    && Aroon(BarsArray[1],AroonPeriod).Up[0] <= Aroon(BarsArray[1],AroonPeriod).Down[0])

    {
    BackColorAll = Color.Tomato;
    }
    }}

    region Properties
    [Description("Fast")]
    [GridCategory("Parameters")]
    public int Fast
    {
    get { return fast; }
    set { fast = Math.Max(1, value); }
    }

    [Description("Slow")]
    [GridCategory("Parameters")]
    public int Slow
    {
    get { return slow; }
    set { slow = Math.Max(1, value); }
    }

    [Description("Smooth")]
    [GridCategory("Parameters")]
    public int Smooth
    {
    get { return smooth; }
    set { smooth = Math.Max(1, value); }
    }

    [Description("AroonPeriod")]
    [GridCategory("Parameters")]
    public int AroonPeriod
    {
    get { return aroonPeriod; }
    set { aroonPeriod = 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 Indicator5[] cacheIndicator5 = null;

    private static Indicator5 checkIndicator5 = new Indicator5();

    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    public Indicator5 Indicator5(int aroonPeriod, int fast, int slow, int smooth)
    {
    return Indicator5(Input, aroonPeriod, fast, slow, smooth);
    }

    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    public Indicator5 Indicator5(Data.IDataSeries input, int aroonPeriod, int fast, int slow, int smooth)
    {
    if (cacheIndicator5 != null)
    for (int idx = 0; idx < cacheIndicator5.Length; idx++)
    if (cacheIndicator5[idx].AroonPeriod == aroonPeriod && cacheIndicator5[idx].Fast == fast && cacheIndicator5[idx].Slow == slow && cacheIndicator5[idx].Smooth == smooth && cacheIndicator5[idx].EqualsInput(input))
    return cacheIndicator5[idx];

    lock (checkIndicator5)
    {
    checkIndicator5.AroonPeriod = aroonPeriod;
    aroonPeriod = checkIndicator5.AroonPeriod;
    checkIndicator5.Fast = fast;
    fast = checkIndicator5.Fast;
    checkIndicator5.Slow = slow;
    slow = checkIndicator5.Slow;
    checkIndicator5.Smooth = smooth;
    smooth = checkIndicator5.Smooth;

    if (cacheIndicator5 != null)
    for (int idx = 0; idx < cacheIndicator5.Length; idx++)
    if (cacheIndicator5[idx].AroonPeriod == aroonPeriod && cacheIndicator5[idx].Fast == fast && cacheIndicator5[idx].Slow == slow && cacheIndicator5[idx].Smooth == smooth && cacheIndicator5[idx].EqualsInput(input))
    return cacheIndicator5[idx];

    Indicator5 indicator = new Indicator5();
    indicator.BarsRequired = BarsRequired;
    indicator.CalculateOnBarClose = CalculateOnBarClose;
    #if NT7
    indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
    indicator.MaximumBarsLookBack = MaximumBarsLookBack;
    #endif
    indicator.Input = input;
    indicator.AroonPeriod = aroonPeriod;
    indicator.Fast = fast;
    indicator.Slow = slow;
    indicator.Smooth = smooth;
    Indicators.Add(indicator);
    indicator.SetUp();

    Indicator5[] tmp = new Indicator5[cacheIndicator5 == null ? 1 : cacheIndicator5.Length + 1];
    if (cacheIndicator5 != null)
    cacheIndicator5.CopyTo(tmp, 0);
    tmp[tmp.Length - 1] = indicator;
    cacheIndicator5 = 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>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.Indicator5 Indicator5(int aroonPeriod, int fast, int slow, int smooth)
    {
    return _indicator.Indicator5(Input, aroonPeriod, fast, slow, smooth);
    }

    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    public Indicator.Indicator5 Indicator5(Data.IDataSeries input, int aroonPeriod, int fast, int slow, int smooth)
    {
    return _indicator.Indicator5(input, aroonPeriod, fast, slow, smooth);
    }
    }
    }

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    public partial class Strategy : StrategyBase
    {
    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.Indicator5 Indicator5(int aroonPeriod, int fast, int slow, int smooth)
    {
    return _indicator.Indicator5(Input, aroonPeriod, fast, slow, smooth);
    }

    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    public Indicator.Indicator5 Indicator5(Data.IDataSeries input, int aroonPeriod, int fast, int slow, int smooth)
    {
    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.Indicator5(input, aroonPeriod, fast, slow, smooth);
    }
    }
    }
    #endregion

    #2
    Hello trader skip,

    Thank you for your post, and welcome to the NinjaTrader support forums!

    When you add the indicator to a chart, do you get any errors in the Log tab of the Control Center? If so, what do these errors report?
    I suspect that you may need to update the CurrentBar check - it is current set to check if (CurrentBar < 1), although you would need to check that there are a minimum number of bars on the chart to calculate the periods being used. For example, if the aroonPeriod is 14, you would need to update the check to the following:

    if (CurrentBar < 14)
    return;

    For more information, please see the help guide page here about making sure you have enough bars:


    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Hi Emily,

      I changed the code as you suggested:

      if (CurrentBar < 14)
      return;

      Log error:

      Error on calling 'OnBarUpdate' method for indicator'Indicator5' on bar 15: Index was outside the bounds of the array.

      Thank you

      Comment


        #4
        Hello trader skip,

        Thank you for your reply.

        Based on the error, it sounds like there is a different minimum number of bars required to calculate the values for the indicator. If you try adjusting the CurrentBar check, like to 20 for example, do you still receive the error?

        if (CurrentBar < 20)
        return;

        Please let me know if the error persists.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Hi Emily,

          I changed the number to 20 - error on bar 20
          I changed the number to 100 - error on bar 104

          on a blank chart - I changed the number to 100 - error on bar 100

          Thanks

          Comment


            #6
            Hello trader skip,

            Thank you for your patience.

            Upon a closer look at your indicator logic, there are some references to BarsArray[1]. This requires an added Bars object:


            I understand you converted a strategy to an indicator; did the strategy originally have an added bars object for a specific series you were using? I suspect you will need to add this bars object and then you may need to add a CurrentBars (rather than CurrentBar) check for the primary and the added series. For more information about multi-time frame and instruments (including information about CurrentBars under the heading "Using Bars Objects as Input to Indicator Methods"):



            I hope this helps to identify the cause of the error and resolve it within your script so the indicator will plot propertly.

            Please let us know if we may be of further assistance.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              Emily,

              With your assistance pointing me where to look along with some trial and error figuring out the syntax, I was able to get it to work.

              Thank you for your help,

              Trader Skip

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by DJ888, 04-16-2024, 06:09 PM
              3 responses
              10 views
              0 likes
              Last Post NinjaTrader_Erick  
              Started by RookieTrader, Today, 07:41 AM
              0 responses
              3 views
              0 likes
              Last Post RookieTrader  
              Started by maybeimnotrader, Yesterday, 05:46 PM
              1 response
              18 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by Perr0Grande, Yesterday, 08:16 PM
              1 response
              7 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Started by f.saeidi, Yesterday, 08:12 AM
              3 responses
              27 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Working...
              X