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

Error CS1729 -- moved a class from MyProgram to a new cs file

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

    Error CS1729 -- moved a class from MyProgram to a new cs file

    My top program is getting too big (12 pages with all the print and debug stuff) so I am trying to move some into other cs files that I can call from the top program. I tried moving a class that was nested in top program to the MyKeyEvent.cs file. I added a constructor to the class so I could call it from top with parameters. This following the approach in MS C# FundamentalsTutorial "Explore object oriented programming with classes and objects." It uses a constructor which won't compile and throws the message below: If I comment out the constructor it compiles but it would not do what I want in a stand alone cs file.

    MyKeyEvent.cs 'NinjaTrader.NinjaScript.Strategies.MyKeyEvent' does not contain a constructor that takes 0 arguments." If I comment out the constructor it compiles but it would not do what I want which is to call the MyKeyEvent with three parameters.

    I attached a cleaned up and simplified version of the script that generates the error. NOTE: an expanded version of this works when it is a nested class in the top program, without a constructor, and not in its own cs file. I use a list of these class objects as a sprce for my algorithm. MyKeyEvent.cs

    I am an amateur with C# and this family of languages.

    Thank you with any help.

    #2
    Hello JGRtrader,

    Thank you fro the post.

    The error would indicate that you have made a public property of your custom class. That type of error gets thrown when serializing a class and it does not have a default constructor.

    If you want the class to be serialized the easiest solution is to have a default constructor with no parameters and then make a second constructor with parameters for when you create the object yourself.

    The sample you provided has a few problems, here is a sample of a general C# class with a default constructor and a paramaterized constructor.

    Code:
    public class MyKeyEvent
    {
       public int EventID;
       public string EventKind;
       public double CurBar;
    
       public MyKeyEvent() {}
    
       public MyKeyEvent(int eventID, string kind, double curBar)
       {
           this.EventID = eventID;
          this.EventKind = kind;
          this.CurBar = curBar;
       }
    }

    In another class you could then create an instance of this class when you wanted to use it.



    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Jesse,
      Thank you. That is very helpful. I had thought about that but not tried it.

      I appreciate any other comments you may have about what I am doing there. I am not clear if the privileges should be protected instead of public or something else. Also I have not yet tried to read the final List from the calling script. My guide for this has been that tutorial I mentioned in my post and the NT SampleLevel2Book.cs which does not use a constructor but is not serialized.

      Comment


        #4
        Hello JGRtrader,

        public would be the standard modifier to use for something you are making exposed to other classes. The protected modifier is only used with the internal overrides.

        The error would have likely come from making a class level property of that type, for example:

        Code:
        public MyKeyEvent myEventClassInstance;
        If that public property was serialized it would make an error without having a no parameter constructor. If thats just a utility class and not something you need in templates or to be saved you can add:

        Code:
        [XmlIgnore]
        public MyKeyEvent myEventClassInstance;
        https://ninjatrader.com/support/help...tsub=XmlIgnore


        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Jesse, Thank you for the help. I wanted to be able to call the class from a top level strategy with parameters and and have available some produce of that class in the calling script. So far I have not been able to make that work in NT. The Microsoft tuorial include a number of constructions that don't work at lest the way I tried to use them. MS recommends Visual Studio 2019 but I see the use of interpolated strings which are not supported until .NET 6 I think.

          Anyway, I am continuing to develop my strategy without serializing classes. If you can [please point me to an example of serializing classes in NT. The only example I see in the SamplePanelPlot alnd SampleStrategyPlot. That is cumbersome.

          Thank you. Jim

          Comment


            #6
            Hello JGRtrader,

            So far I have not been able to make that work in NT. The Microsoft tuorial include a number of constructions that don't work at lest the way I tried to use them.
            Most tutorials that have constructors generally do so to demonstrate passing some data to the class. If you wanted to have parameters passed to the class you could do that either by using a constructor or as parameters in a method. Alternatively you can make public properties that can be set from outside the class instance. These are all standard C# concepts, NinjaScript is just C#.

            MS recommends Visual Studio 2019 but I see the use of interpolated strings which are not supported until .NET 6 I think.
            Some features may not be available in NinjaScript depending on the tutorial you are looking at and the .net version they used. You can always search for other tutorials to see other ways of accomplishing the goal.

            Anyway, I am continuing to develop my strategy without serializing classes. If you can [please point me to an example of serializing classes in NT. The only example I see in the SamplePanelPlot alnd SampleStrategyPlot. That is cumbersome.
            Post #2 would be a general class structure that could be used. Did you have some difficulty with that type of class?

            I look forward to being of further assistance.

            JesseNinjaTrader Customer Service

            Comment


              #7
              Jesse,
              Thank you for all your help with this.

              I got it to work, not what I was planning to do with it but I can create the class in the called cs file.
              Something that I found is that I had to put successors {get; private set;} in the class definition. The MS tutorial showed that with just the {get:} but that would not compile. Had to add the set too. That seems to have been what was preventing it from working.

              I had planned to build a list in that cs file too but that did not play at all. It would only execute up through SetDefaults. Do I need those NT event methods at all for this usage?

              I created the calling script "MyEventCalc" to test this out and got that to work. I generated a 7 MB log file in the process. I think there may be some threading issue with this construction. For some reason, it would not execute the if (tempevent <31) block inline but apparently on a different slower thread. I changed the structure and it worked today.

              I attached MyEventCalc and the one it calls MyKeyEvent. Both are loaded with my test Print statements and comments. I create a list in MyEventCalc.

              Thank you for the help, I learned a lot. MyKeyEvent.cs MyEventCalc.cs

              Comment


                #8
                Hello JGRtrader,

                The MS tutorial showed that with just the {get:} but that would not compile. Had to add the set too. That seems to have been what was preventing it from working.
                Yes you may need to make modifications to the sample code that you are working with depending on what you were trying to do with it. the get set and requiring a set depends on the use case.


                I had planned to build a list in that cs file too but that did not play at all. It would only execute up through SetDefaults. Do I need those NT event methods at all for this usage?
                Lists can be used in NinjaScript as can most C# concepts. If you are having trouble with specific items not working its generally best to make a very simple example script and upload that as a new post on the forum. We can try to help with each individual concept that way.


                I created the calling script "MyEventCalc" to test this out and got that to work. I generated a 7 MB log file in the process. I think there may be some threading issue with this construction. For some reason, it would not execute the if (tempevent <31) block inline but apparently on a different slower thread. I changed the structure and it worked today.
                The scripts you attached would not work correctly due to how your MyKeyEvent class is set up. You are inheriting from strategy in that class but that is not a strategy.

                You would want the MyKeyEvent class to look like the following:

                Code:
                public class MyKeyEvent
                {
                
                   public int EventID {get; set;}
                   public string EventKind {get; set;}
                   public double CurtBar {get; set;}
                
                
                   public MyKeyEvent() {}
                
                   public MyKeyEvent(int eventID, string kind, double curtBar)
                   {
                       this.EventID = eventID;
                      this.EventKind = kind;
                      this.CurtBar = curtBar;
                   }
                }
                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by wellness, Today, 11:22 PM
                0 responses
                1 view
                0 likes
                Last Post wellness  
                Started by wellness, Today, 11:21 PM
                0 responses
                2 views
                0 likes
                Last Post wellness  
                Started by ezrollin, Today, 08:51 PM
                1 response
                16 views
                0 likes
                Last Post bltdavid  
                Started by Rubix, Today, 09:31 PM
                0 responses
                14 views
                0 likes
                Last Post Rubix
                by Rubix
                 
                Started by ark321, Today, 05:12 PM
                1 response
                20 views
                0 likes
                Last Post NinjaTrader_EricB  
                Working...
                X