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

Creating User Defined methods

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

    Creating User Defined methods

    I am reading the section regarding Partial Classes (Porting methods and properties from UserDefinedMethods.cs) at this link and I have some questions. In the Partial Class Usage example, shouldn't there be a using statement such as:

    Code:
    using MyMethods;
    If so, is that what that would look like? I am unable to get this to work but I don't feel the examples are fully fleshed out. Should I compile the partial class separately? If so, where does the .dll get placed so I can reference?

    #2
    Hello swcooke,

    There would not be a need for a using statement for this case, the partial class will be part of the namespace/class you are working from. Partial classes are a C# concept, for further education on this topic I would suggest using external C# education materials surrounding partial classes.

    The sample in the help guide would be specifically what the partial class looks like, what did you try that was not working?

    If you are exporting this as a DLL, you need to include this inside of the export, however, if your overall plan is to share this between DLL's that would not be possible using a partial class.

    A partial class cannot be used from a different type than where it was defined (indicator) or different assembly. Potentially this is where the problem in what you were trying comes from. The sample in the help guide is a complete Indicator sample which includes both the necessary code and instructions on where to define the file (addons folder). During the export, the scripts which use the partial class and the partial class itself would need to be exported, but as noted before the partial class cannot be a dll by its self, this is going to need to be exported in addition to the other scripts that use it.


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

    Comment


      #3
      I was able to get it working however, I noticed that if I export a compiled assembly to distribute to a customer, my partial class shows up as an indicator is their Indicator window. How can I avoid that happening?

      Comment


        #4
        Hello swcooke,

        Did you put the partial class in the Addon folder or have you placed it in the Indicator folder? If you place it in the indicator folder, it will show up as an indicator. The help guide sample notes that this needs to be placed in the Addons folder, have you done this?

        Partial classes are now built manually and saved in the C:\Users\<user>\Documents\NinjaTrader 8\bin\Custom\AddOns folder.
        I look forward to being of further assistance.

        JesseNinjaTrader Customer Service

        Comment


          #5
          Yes, I did. Here is a screenshot:


          Why is it suggested that these go into the AddOn folder but the example shows them namespaced like this:
          Code:
          namespace NinjaTrader.NinjaScript.Indicators
          Is the issue that it should be namespaced like this?
          Code:
          namespace NinjaTrader.NinjaScript.AddOns
          Or does that have nothing to do with it? It is definitely showing up when I export the Indicator as a compiled assembly. It shows right in the Indicator window. Please advise.

          Comment


            #6
            Hello swcooke,

            Thank you for your reply.

            The namespace would be correct, this is part of how partial classes work. You would need the partial class inside of the addon folder and the class its self would be in the Indicators namespace so that the class can be part of the Indicator class which is also in the Indicators namespace.

            I tried the sample we have in the help guide and exported it as a compiled assembly then imported it, however, I am not seeing the class show up as an indicator. Can you provide a sample of how you defined your partial class?

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

            Comment


              #7
              What is the preferred method for creating one? Do I click the plus icon on the Tabbed interface at the bottom and choose New Add On and let the Wizard do it and then edit the file? Or should I manually create the file outside of NT and drop it into the folder?

              Comment


                #8
                Hello swcooke,

                You can use the addon wizard to generate the file, you would then need to remove the existing code and use the code from the help guide. The using statements at the top can be retained, you mainly need to remove the addon namespace and generated addon code from the file.

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

                Comment


                  #9
                  Take a look at this video and let me know what you think:

                  Comment


                    #10
                    Hello swcooke,

                    I took a look at the video however this does not necessarily help in this case. I would still want to see a sample of the partial class that I can test. Could you export what you have created as source code so that I can try this on my end with identical code?

                    I look forward to being of further assistance.

                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Attached is the ,cs file of the partial class
                      NpLicenseCheck.cs

                      Comment


                        #12
                        Hello swcooke,

                        Thank you for your reply.

                        I can now see where the disconnect is, the help guide sample seems to be demonstrating how to define a named partial class which would not be specifically what you want to use here.

                        In this case, the class name is NpLicenseCheck and you are also defining this as inheriting from an Indicator which makes it an indicator. This will show up in the menu as it is currently defined. I will note this to development to see if this can be changed to a more standard partial class example.

                        In your use case, you would just want to make a partial indicator class or exactly what you see in NT7's UserDefinedMethods file (its just a C# partial class). Here is the NT8 help guide sample without the indicator name:

                        Code:
                        namespace NinjaTrader.NinjaScript.Indicators
                        {
                          public partial class Indicator
                          {
                              //Sample method which calculates the delta of two prices
                              public double CalculateDelta(double firstPrice, double secondPrice)
                              {
                                  return Math.Abs(firstPrice - secondPrice);
                              }
                        
                              //Sample method which prints Position information
                              public void PrintPositionInfo(Position position)
                              {
                                  Print(String.Format("{0}: {1} {2} at {3}", position.Instrument, position.Quantity, position.MarketPosition, position.AveragePrice));
                              }
                          }
                        }
                        Note how the class is defined:
                        Code:
                        public partial class Indicator

                        I look forward to being of further assistance.


                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Thanks! That did the trick!

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by alifarahani, Today, 09:40 AM
                          6 responses
                          29 views
                          0 likes
                          Last Post alifarahani  
                          Started by Waxavi, Today, 02:10 AM
                          1 response
                          17 views
                          0 likes
                          Last Post NinjaTrader_LuisH  
                          Started by Kaledus, Today, 01:29 PM
                          5 responses
                          13 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by Waxavi, Today, 02:00 AM
                          1 response
                          12 views
                          0 likes
                          Last Post NinjaTrader_LuisH  
                          Started by gentlebenthebear, Today, 01:30 AM
                          3 responses
                          17 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Working...
                          X