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

market analyzer and custom indicator

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

    market analyzer and custom indicator

    Hello

    I have developed simple indicator that has values -1.0, 0.0, and 1.0 (float):

    if (golong && direction != 1)
    {
    Plot_Observer.Set(1.0);
    direction = 1;
    }
    else if (goshort && direction != -1)
    {
    Plot_Observer.Set(-1.0);
    direction = -1;
    }
    else
    {
    Plot_Observer.Set(0.0);
    direction = 0;

    }

    If I use this indicator in chart it works just fine (it really very simple), but in market analyzer it shows strange values ranging -100 to 100 (floats). What can be wrong?

    here is the entire indicator, with conditions removed:

    ********************
    #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 Observer : Indicator
    {
    #region Variables
    // Wizard generated variables
    private int period = 1; // Default setting for Period
    // User defined variables (add any user defined variables below)
    #endregion

    private int direction;

    /// <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.ForestGreen), PlotStyle.Square, "Plot_Observer"));
    Add(new Line(Color.FromKnownColor(KnownColor.SandyBrown), 0, "ZeroLine"));
    Overlay = false;
    CalculateOnBarClose = false;
    direction = 0;
    }

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


    bool golong = true, goshort = true;

    // conditions were removed ...

    if (golong && direction != 1)
    {
    Plot_Observer.Set(1.0);
    direction = 1;
    }
    else if (goshort && direction != -1)
    {
    Plot_Observer.Set(-1.0);
    direction = -1;
    }
    else
    {
    Plot_Observer.Set(0.0);
    direction = 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 Plot_Observer
    {
    get { return Values[0]; }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int Period
    {
    get { return period; }
    set { period = 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 Observer[] cacheObserver = null;

    private static Observer checkObserver = new Observer();

    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    public Observer Observer(int period)
    {
    return Observer(Input, period);
    }

    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    public Observer Observer(Data.IDataSeries input, int period)
    {
    if (cacheObserver != null)
    for (int idx = 0; idx < cacheObserver.Length; idx++)
    if (cacheObserver[idx].Period == period && cacheObserver[idx].EqualsInput(input))
    return cacheObserver[idx];

    lock (checkObserver)
    {
    checkObserver.Period = period;
    period = checkObserver.Period;

    if (cacheObserver != null)
    for (int idx = 0; idx < cacheObserver.Length; idx++)
    if (cacheObserver[idx].Period == period && cacheObserver[idx].EqualsInput(input))
    return cacheObserver[idx];

    Observer indicator = new Observer();
    indicator.BarsRequired = BarsRequired;
    indicator.CalculateOnBarClose = CalculateOnBarClose;
    #if NT7
    indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
    indicator.MaximumBarsLookBack = MaximumBarsLookBack;
    #endif
    indicator.Input = input;
    indicator.Period = period;
    Indicators.Add(indicator);
    indicator.SetUp();

    Observer[] tmp = new Observer[cacheObserver == null ? 1 : cacheObserver.Length + 1];
    if (cacheObserver != null)
    cacheObserver.CopyTo(tmp, 0);
    tmp[tmp.Length - 1] = indicator;
    cacheObserver = 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.Observer Observer(int period)
    {
    return _indicator.Observer(Input, period);
    }

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

    // 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.Observer Observer(int period)
    {
    return _indicator.Observer(Input, period);
    }

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


    *******************

    #2
    Hello sergeysamsonov,

    Do you have enough bars to load for the indicator? You need more than the default 50.

    What is direction? I couldn't get your code to compile as you posted it and had to move it to the variables region from Initialize().
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      I removed direction, it was not important or relevant, same problem, please try to recompile now, I still get the same problem:

      #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 Observer : Indicator
      {
      #region Variables
      // Wizard generated variables
      private int period = 1; // Default setting for Period
      // 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.ForestGreen), PlotStyle.Square, "Plot_Observer"));
      Add(new Line(Color.FromKnownColor(KnownColor.SandyBrown), 0, "ZeroLine"));
      Overlay = false;
      CalculateOnBarClose = false;
      }

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


      bool golong = true, goshort = true;


      golong = golong && RSI(14,1)[0]>50;
      goshort = goshort && RSI(14,1)[0]<50;



      if (golong)
      {
      Plot_Observer.Set(1.0);
      }
      else if (goshort)
      {
      Plot_Observer.Set(-1.0);
      }
      else
      {
      Plot_Observer.Set(0.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 Plot_Observer
      {
      get { return Values[0]; }
      }

      [Description("")]
      [GridCategory("Parameters")]
      public int Period
      {
      get { return period; }
      set { period = 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 Observer[] cacheObserver = null;

      private static Observer checkObserver = new Observer();

      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      public Observer Observer(int period)
      {
      return Observer(Input, period);
      }

      /// <summary>
      /// Enter the description of your new custom indicator here
      /// </summary>
      /// <returns></returns>
      public Observer Observer(Data.IDataSeries input, int period)
      {
      if (cacheObserver != null)
      for (int idx = 0; idx < cacheObserver.Length; idx++)
      if (cacheObserver[idx].Period == period && cacheObserver[idx].EqualsInput(input))
      return cacheObserver[idx];

      lock (checkObserver)
      {
      checkObserver.Period = period;
      period = checkObserver.Period;

      if (cacheObserver != null)
      for (int idx = 0; idx < cacheObserver.Length; idx++)
      if (cacheObserver[idx].Period == period && cacheObserver[idx].EqualsInput(input))
      return cacheObserver[idx];

      Observer indicator = new Observer();
      indicator.BarsRequired = BarsRequired;
      indicator.CalculateOnBarClose = CalculateOnBarClose;
      #if NT7
      indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
      indicator.MaximumBarsLookBack = MaximumBarsLookBack;
      #endif
      indicator.Input = input;
      indicator.Period = period;
      Indicators.Add(indicator);
      indicator.SetUp();

      Observer[] tmp = new Observer[cacheObserver == null ? 1 : cacheObserver.Length + 1];
      if (cacheObserver != null)
      cacheObserver.CopyTo(tmp, 0);
      tmp[tmp.Length - 1] = indicator;
      cacheObserver = 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.Observer Observer(int period)
      {
      return _indicator.Observer(Input, period);
      }

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

      // 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.Observer Observer(int period)
      {
      return _indicator.Observer(Input, period);
      }

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

      Comment


        #4
        I didn't have any issues with the code, although I did have to change the # of bars back in the market analyzer indicator settings. What is yours currently set to?
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          thank you, changed number of bars and problem got solved

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Lumbeezl, 01-11-2022, 06:50 PM
          30 responses
          803 views
          1 like
          Last Post grayfrog  
          Started by xiinteractive, 04-09-2024, 08:08 AM
          3 responses
          11 views
          0 likes
          Last Post NinjaTrader_Erick  
          Started by Johnny Santiago, 10-11-2019, 09:21 AM
          95 responses
          6,193 views
          0 likes
          Last Post xiinteractive  
          Started by Irukandji, Today, 09:34 AM
          1 response
          3 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Started by RubenCazorla, Today, 09:07 AM
          1 response
          6 views
          0 likes
          Last Post RubenCazorla  
          Working...
          X