Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Partial class compiling issue for indicator

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

    Partial class compiling issue for indicator

    When I defined an indicator with "partial class" from two files, the error popped up at compiling,

    The type 'NinjaTrader.NinjaScript.Indicators.Indicator' already contains a definition for 'cacheGIndicatorBase',CS0102

    the reason is that the NT8 auto generated code for the indicator duplicated in the two files, but they cannot be removed or be commented out. So is there any way to work around because my indicator has complex logic I need to separate them into different files.

    #region NinjaScript generated code. Neither change nor remove.

    namespace NinjaTrader.NinjaScript.Indicators
    {
    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    {
    private ZTraderInd.GIndicatorBase[] cacheGIndicatorBase;
    public ZTraderInd.GIndicatorBase GIndicatorBase(bool backTest)
    {
    return GIndicatorBase(Input, backTest);
    }

    public ZTraderInd.GIndicatorBase GIndicatorBase(ISeries<double> input, bool backTest)
    {
    if (cacheGIndicatorBase != null)
    for (int idx = 0; idx < cacheGIndicatorBase.Length; idx++)
    if (cacheGIndicatorBase[idx] != null && cacheGIndicatorBase[idx].BackTest == backTest && cacheGIndicatorBase[idx].EqualsInput(input))
    return cacheGIndicatorBase[idx];
    return CacheIndicator<ZTraderInd.GIndicatorBase>(new ZTraderInd.GIndicatorBase(){ BackTest = backTest }, input, ref cacheGIndicatorBase);
    }
    }
    }

    #2
    Hello [email protected],

    Can you provide a specific example of the partial class you created instead of the generated code the platform makes? I would likely need to see a sample that generates the problem to be of further help.

    Based on the description, it sounds like you have made a partial class that also inherits from Indicator and it is getting the generated code is this correct? If so, very likely you would need to make a partial class of Indicator and not your specific indicator and also with no inheritance, the partial class would just be of type Indicator and would not inherit from indicator.

    Code:
     public partial class Indicator{
    
        }
    I look forward to being of further assistance.

    Comment


      #3
      Jesse,

      Thanks for your response, the following are the two partial classes sample, they have to inherit from indicator for my program. I used to do the partial class from Indicator itself in NT7, but the functions I added will impact all of indicators anybody else created. So when migrating to NT8 I decided to create my own IndicatorBase class which is inherited from Indicator so that it won't impact someone else's. The inheritance does not do things wrong. The problem is the NT auto generated code, it cannot work with partial keyword for indicator. And also I did the similar design with my strategy, the auto generated code does not really bother me.

      public partial class GIndicatorBase : Indicator
      {
      /* Body for partial class one */

      #region NinjaScript generated code. Neither change nor remove.

      namespace NinjaTrader.NinjaScript.Indicators
      {
      public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
      {
      private ZTraderInd.GIndicatorBase[] cacheGIndicatorBase;
      public ZTraderInd.GIndicatorBase GIndicatorBase(bool backTest)
      {
      return GIndicatorBase(Input, backTest);
      }

      public ZTraderInd.GIndicatorBase GIndicatorBase(ISeries<double> input, bool backTest)
      {
      if (cacheGIndicatorBase != null)
      for (int idx = 0; idx < cacheGIndicatorBase.Length; idx++)
      if (cacheGIndicatorBase[idx] != null && cacheGIndicatorBase[idx].BackTest == backTest && cacheGIndicatorBase[idx].EqualsInput(input))
      return cacheGIndicatorBase[idx];
      return CacheIndicator<ZTraderInd.GIndicatorBase>(new ZTraderInd.GIndicatorBase(){ BackTest = backTest }, input, ref cacheGIndicatorBase);
      }
      }
      }
      ................
      ................
      #endregion
      }


      public partial class GIndicatorBase : Indicator
      {
      /* Body for partial class two */

      #region NinjaScript generated code. Neither change nor remove.

      namespace NinjaTrader.NinjaScript.Indicators
      {
      public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
      {
      private ZTraderInd.GIndicatorBase[] cacheGIndicatorBase;
      public ZTraderInd.GIndicatorBase GIndicatorBase(bool backTest)
      {
      return GIndicatorBase(Input, backTest);
      }

      public ZTraderInd.GIndicatorBase GIndicatorBase(ISeries<double> input, bool backTest)
      {
      if (cacheGIndicatorBase != null)
      for (int idx = 0; idx < cacheGIndicatorBase.Length; idx++)
      if (cacheGIndicatorBase[idx] != null && cacheGIndicatorBase[idx].BackTest == backTest && cacheGIndicatorBase[idx].EqualsInput(input))
      return cacheGIndicatorBase[idx];
      return CacheIndicator<ZTraderInd.GIndicatorBase>(new ZTraderInd.GIndicatorBase(){ BackTest = backTest }, input, ref cacheGIndicatorBase);
      }
      }
      }
      ................
      ................
      #endregion
      }

      Comment


        #4
        Hello [email protected],

        Thank you for the additional details. Yes, this is not really how we would suggest creating the classes as you will run into specifically the problem you reported. In general, we do not suggest creating your own base classes as there are many problems that can arise from doing so. We also do not provide any specific samples on this subject as this type of development is generally very specific.

        A partial indicator class specifically should not inherit from indicator as that just becomes another indicator which also requires that the NinjaScript code is automatically generated. If you are trying to share methods or properties and not have them available to other scripts besides these scripts, that would be a good use case for a regular class. You could create an instance to that class and use its methods or properties, you could also explore making static properties which would not require an object instance. In either case, you would pass in the instance to the indicator to that class so you can use indicator methods in addition to custom logic.

        Code:
         public class MySharedMethods{
                public void MySharedMethod(Indicator myIndicator)
                {
                    myIndicator.Print("");
                }
            }

        You could alternatively create an indicator class:
        Code:
        public class GIndicatorBase : Indicator
        and then create partial classes of your indicator class:

        Code:
        public partial class GIndicatorBase
            {
                public void Test(Indicator myIndicator)
                {
                   myIndicator.Print("");    
                }
            }
        This would just share the code in the partial class with your base but this is not an indicator so you need to pass in an instance if you want indicator specific logic to be used.

        The other alternative would be to just resume what you did in NT7 which would also be the suggested way to approach this using the Indicator partial class, although this makes it available to other scripts this is the way it would work as this also allows for the inherited properties like Print.

        Code:
        public partial class Indicator
            {
                public void Test()
                {
                    Print("");    
                }
            }



        I look forward to being of further assistance.

        Comment


          #5
          Hi Jesse,

          Your comments are very helpful, I am thinking of alternative ways to implement the partial class of my base indicator. I understand there are reasons NT team put the auto generated code there for indicator and forced it stay. So I probably won't mess up with that.

          I might try the second approach you recommended, i.e., resume back to the partial class of indicator. Because I have tried the first approach before actually the NT script editor is smart enough to put the auto generated code there even its grand-grand-grand-pa is Mr. INDICATOR!

          Anyway, my overall goal is to make the logic simple and clear. My framework will go further away from indicators, it will include strategy, pattern recognition, money mgmt, trade mgmt, etc. Hopefully NT8 could handle them better than NT7.

          Thanks again for your assistance!

          Gerry

          Comment

          Latest Posts

          Collapse

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