Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

MA help

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

    MA help

    Morning,

    I hope someone can help with my two questions.

    First off (i guess i'll be the millionth person to ask) - I have tried to create an indicator for market analyser. However was unable to write the script as a indicator instead had to use a strategy. Is it possible to change a strategy to an indciator or copy and paste the script into the indicator editor.

    Secondly,

    I put together the below script. i am, however, coming up with an error message. it says line 39, column 13 "only assignment, call, increment, decrement and new object expressions can be used. Can someone help? I have highlighted the bit read.




    #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;
    #endregion

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Sector asset Allocation
    /// </summary>
    [Description("Sector asset Allocation")]
    public class SectorTAA : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int price = 1; // Default setting for Price
    private int mA20 = 1; // Default setting for MA20
    private int mA50 = 1; // Default setting for MA50
    private int mA200 = 1; // Default setting for MA200
    // User defined variables (add any user defined variables below)
    #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);


    }


    /// <summary>;
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (Close[0] > SMA(200)[0]&&(SMA(20)[0] > SMA(50)[0]))
    {
    Alert("MyAlert0", Priority.High, "Buy", "", 0, Color.White, Color.Black);
    Log("Buy", LogLevel.Information);

    }
    else if (Close[0] > SMA(200)[0]&&(SMA(20)[0] < SMA(50)[0]))
    {
    Alert("MyAlert1", Priority.High, "Reduce", "", 0, Color.White, Color.Black);
    Log("Reduce", LogLevel.Information);

    }

    else if (Close[0] < SMA(200)[0]&&SMA(20)[0] < SMA(50)[0])
    {
    Alert("MyAlert2", Priority.High, "Sell", "", 0, Color.White, Color.Black);
    Log("Sell", LogLevel.Information);
    }

    else if (Close[0] < SMA(200)[0]&&SMA(20)[0] > SMA(50)[0])
    {
    Alert("MyAlert3", Priority.High, "Add", "", 0, Color.White, Color.Black);
    Log("Add", LogLevel.Information);

    }

    else if (Close[0] > SMA(200)[0]&&SMA(20)[0] > SMA(50)[0])
    {
    Alert("MyAlert4", Priority.High, "Buy", "", 0, Color.White, Color.Black);
    Log("Buy", LogLevel.Information);
    }
    }

    #region Properties
    [Description("Price of Instrument")]
    [GridCategory("Parameters")]
    public int Price
    {
    get { return price; }
    set { price = Math.Max(1, value); }
    }

    [Description("MA20 of Instrument")]
    [GridCategory("Parameters")]
    public int MA20
    {
    get { return mA20; }
    set { mA20 = Math.Max(1, value); }
    }

    [Description("MA50 of Instrument")]
    [GridCategory("Parameters")]
    public int MA50
    {
    get { return mA50; }
    set { mA50 = Math.Max(1, value); }
    }

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

    #2
    Hello Beng986,
    Yes, you can do it. However you have to make the following modifications:

    Change
    Code:
    namespace NinjaTrader.Strategy
    Code:
    public class SectorTAA : Strategy
    to
    Code:
    namespace NinjaTrader.Indicator
    Code:
    public class SectorTAA : Indicator
    The code for which you are getting the error should be
    Code:
    CalculateOnBarClose =true; // not [B]T[/B]rue and remove the ()
    Please let me know if I can assist you any further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      thanks

      now it is saying "the namespace 'NinjaTrader.Indicator' already contains a definition for 'SectorTAA'

      any ideas


      #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;
      #endregion

      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Indicator
      {
      /// <summary>
      /// Sector asset Allocation
      /// </summary>
      [Description("Sector asset Allocation")]
      public class SectorTAA : Indicator
      {
      #region Variables
      // Wizard generated variables
      private int price = 1; // Default setting for Price
      private int mA20 = 1; // Default setting for MA20
      private int mA50 = 1; // Default setting for MA50
      private int mA200 = 1; // Default setting for MA200
      // User defined variables (add any user defined variables below)
      #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);//


      }

      /// <summary>;
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Condition set 1
      if (Close[0] > SMA(200)[0]&&(SMA(20)[0] > SMA(50)[0]))
      {
      Alert("MyAlert0", Priority.High, "Buy", "", 0, Color.White, Color.Black);
      Log("Buy", LogLevel.Information);

      }
      else if (Close[0] > SMA(200)[0]&&(SMA(20)[0] < SMA(50)[0]))
      {
      Alert("MyAlert1", Priority.High, "Reduce", "", 0, Color.White, Color.Black);
      Log("Reduce", LogLevel.Information);

      }

      else if (Close[0] < SMA(200)[0]&&SMA(20)[0] < SMA(50)[0])
      {
      Alert("MyAlert2", Priority.High, "Sell", "", 0, Color.White, Color.Black);
      Log("Sell", LogLevel.Information);
      }

      else if (Close[0] < SMA(200)[0]&&SMA(20)[0] > SMA(50)[0])
      {
      Alert("MyAlert3", Priority.High, "Add", "", 0, Color.White, Color.Black);
      Log("Add", LogLevel.Information);

      }

      else if (Close[0] > SMA(200)[0]&&SMA(20)[0] > SMA(50)[0])
      {
      Alert("MyAlert4", Priority.High, "Buy", "", 0, Color.White, Color.Black);
      Log("Buy", LogLevel.Information);
      }
      }

      #region Properties
      [Description("Price of Instrument")]
      [GridCategory("Parameters")]
      public int Price
      {
      get { return price; }
      set { price = Math.Max(1, value); }
      }

      [Description("MA20 of Instrument")]
      [GridCategory("Parameters")]
      public int MA20
      {
      get { return mA20; }
      set { mA20 = Math.Max(1, value); }
      }

      [Description("MA50 of Instrument")]
      [GridCategory("Parameters")]
      public int MA50
      {
      get { return mA50; }
      set { mA50 = Math.Max(1, value); }
      }

      [Description("MA200 of Instrument")]
      [GridCategory("Parameters")]
      public int MA200
      {
      get { return mA200; }
      set { mA200 = 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 SectorTAA[] cacheSectorTAA = null;

      private static SectorTAA checkSectorTAA = new SectorTAA();

      /// <summary>
      /// Sector asset Allocation
      /// </summary>
      /// <returns></returns>
      public SectorTAA SectorTAA(int mA20, int mA200, int mA50, int price)
      {
      return SectorTAA(Input, mA20, mA200, mA50, price);
      }

      /// <summary>
      /// Sector asset Allocation
      /// </summary>
      /// <returns></returns>
      public SectorTAA SectorTAA(Data.IDataSeries input, int mA20, int mA200, int mA50, int price)
      {
      if (cacheSectorTAA != null)
      for (int idx = 0; idx < cacheSectorTAA.Length; idx++)
      if (cacheSectorTAA[idx].MA20 == mA20 && cacheSectorTAA[idx].MA200 == mA200 && cacheSectorTAA[idx].MA50 == mA50 && cacheSectorTAA[idx].Price == price && cacheSectorTAA[idx].EqualsInput(input))
      return cacheSectorTAA[idx];

      lock (checkSectorTAA)
      {
      checkSectorTAA.MA20 = mA20;
      mA20 = checkSectorTAA.MA20;
      checkSectorTAA.MA200 = mA200;
      mA200 = checkSectorTAA.MA200;
      checkSectorTAA.MA50 = mA50;
      mA50 = checkSectorTAA.MA50;
      checkSectorTAA.Price = price;
      price = checkSectorTAA.Price;

      if (cacheSectorTAA != null)
      for (int idx = 0; idx < cacheSectorTAA.Length; idx++)
      if (cacheSectorTAA[idx].MA20 == mA20 && cacheSectorTAA[idx].MA200 == mA200 && cacheSectorTAA[idx].MA50 == mA50 && cacheSectorTAA[idx].Price == price && cacheSectorTAA[idx].EqualsInput(input))
      return cacheSectorTAA[idx];

      SectorTAA indicator = new SectorTAA();
      indicator.BarsRequired = BarsRequired;
      indicator.CalculateOnBarClose = CalculateOnBarClose;
      #if NT7
      indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
      indicator.MaximumBarsLookBack = MaximumBarsLookBack;
      #endif
      indicator.Input = input;
      indicator.MA20 = mA20;
      indicator.MA200 = mA200;
      indicator.MA50 = mA50;
      indicator.Price = price;
      Indicators.Add(indicator);
      indicator.SetUp();

      SectorTAA[] tmp = new SectorTAA[cacheSectorTAA == null ? 1 : cacheSectorTAA.Length + 1];
      if (cacheSectorTAA != null)
      cacheSectorTAA.CopyTo(tmp, 0);
      tmp[tmp.Length - 1] = indicator;
      cacheSectorTAA = 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>
      /// Sector asset Allocation
      /// </summary>
      /// <returns></returns>
      [Gui.Design.WizardCondition("Indicator")]
      public Indicator.SectorTAA SectorTAA(int mA20, int mA200, int mA50, int price)
      {
      return _indicator.SectorTAA(Input, mA20, mA200, mA50, price);
      }

      /// <summary>
      /// Sector asset Allocation
      /// </summary>
      /// <returns></returns>
      public Indicator.SectorTAA SectorTAA(Data.IDataSeries input, int mA20, int mA200, int mA50, int price)
      {
      return _indicator.SectorTAA(input, mA20, mA200, mA50, price);
      }
      }
      }

      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      public partial class Strategy : StrategyBase
      {
      /// <summary>
      /// Sector asset Allocation
      /// </summary>
      /// <returns></returns>
      [Gui.Design.WizardCondition("Indicator")]
      public Indicator.SectorTAA SectorTAA(int mA20, int mA200, int mA50, int price)
      {
      return _indicator.SectorTAA(Input, mA20, mA200, mA50, price);
      }

      /// <summary>
      /// Sector asset Allocation
      /// </summary>
      /// <returns></returns>
      public Indicator.SectorTAA SectorTAA(Data.IDataSeries input, int mA20, int mA200, int mA50, int price)
      {
      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.SectorTAA(input, mA20, mA200, mA50, price);
      }
      }
      }
      #endregion

      Comment


        #4
        sorry line 22 colum 18

        public class SectorTAA : Indicator

        Comment


          #5
          Hello Beng986,
          The message indicates that you already have an indicator with the same name. Please change the name of the indicator to any other name.such as below.

          Code:
          public class SectorTAANew : Indicator
          Please let me know if I can assist you any further.
          JoydeepNinjaTrader Customer Service

          Comment


            #6
            thanks for your help so far. It is now working however

            In the script i set it to give an output as a as a message - it however only gives it as a number. is it possible to set the the indicator to give the messages i wanted it to in the code.

            Comment


              #7
              Hello Beng986,
              The code rightly printing out the values in the log tab in Control Center (and the Alert in the Alert window).

              Can you send a screenshot describing the exact scenario.

              To send a screenshot press Alt + PRINT SCREEN to take a screen shot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save as a jpeg file and send the file as an attachment.

              For detailed instructions please visit the following link

              How to take a screenshot on your smartphone, tablet, notebook or desktop computer


              I look forward to assisting you further.
              JoydeepNinjaTrader Customer Service

              Comment


                #8
                thanks.

                yer is it doing what i say but not (my mistake) what i want.

                can i get it to say buy, sell, add, reduce instead of displaying the number?

                Comment


                  #9
                  Hello Beng986,
                  The log tab is displaying the Buy/Sell etc messages and not any number. This is true for the Alert box also.

                  If you want to show the message only and remove any other colume then please follow the below steps:
                  • Right click on the grid
                  • In the context menu click on Grid>Properties
                  • In the Properties dialog uncheck all boxes except for message.



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

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  103 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  52 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  33 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  36 views
                  0 likes
                  Last Post TheRealMorford  
                  Started by Mindset, 02-28-2026, 06:16 AM
                  0 responses
                  73 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Working...
                  X