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

Shared code for multiple strategies

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

    Shared code for multiple strategies

    Hello,
    I appreciate your hard work and I would like to ask for an advice.
    I have couple of strategies, which share the same part of source code, especially some custom methods. If I want to modify this code, then I have to do in in every file independently using copy/paste.
    I would like to have one editable file with shared methods, that would be included in every strategy. So that I can call up custom methods from one shared file in all strategies. What is the easiest way to achieve that? What steps should I take?

    #2
    Hello InteRadek,

    Thank you for your inquiry.

    If you wish to have methods that your strategies can share, you can modify the UserDefinedMethods.cs file. Here, you will be able to place the methods you would like to share and then call them from your particular strategy.

    You would be able to access UserDefinedMethods by clicking on Tools -> Edit NinjaScript -> Strategy in the NinjaTrader Control Center. Once the Edit Strategy window pops up, you'll see the UserDefinedMethods. Choose this.

    Now, in the Strategy partial class, you can add your methods. To call them from a strategy, you would just call them by the name you have defined in UserDefinedMethods.

    Please read the NinjaTrader help guide at this link for more information about UserDefinedMethods: http://ninjatrader.com/support/helpG...ed_methods.htm

    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Simple, elegant, brilliant. I didn't suspect, that this will be that easy. Thank you!

      Comment


        #4
        It seems that UserDefinedMethods isn't as good way as I thought. I need to export both source and compiled assembly, but UserDefinedMethods doesn't support it (as far as I read on forum).
        Is there any other way to share methods that support exporting?

        Comment


          #5
          Hello InteRadek,

          Unfortunately, UserDefinedMethods is not exportable.

          You would have to place the methods into the corresponding strategies in order to export.

          Please, let us know if we may be of further assistance.
          Zachary G.NinjaTrader Customer Service

          Comment


            #6
            Hello,
            how can I reach methods from external DLL? I've created a custom strategy with my methods, exported it as an assembly and imported back to NT. Now I have a DLL file in my Custom folder and it is visible in Strategy -> right click -> References. But methods are not visible in strategy.
            What am I missing?

            EDIT> For example I have external DLL file with content:
            Code:
            namespace NinjaTrader.Strategy
             {
              public class Methods : Strategy
              {
               public int Test()
               {
                 return 1;
               }
              }
             }
            And I have my Strategy:
            Code:
            namespace NinjaTrader.Strategy
             {
              public class Buy : Strategy
              {
               int value = Test();
              }
             }
            What to add to use Test() in the Buy strategy?
            Last edited by InteRadek; 07-15-2015, 03:36 AM. Reason: Added example

            Comment


              #7
              Originally posted by InteRadek View Post
              It seems that UserDefinedMethods isn't as good way as I thought. I need to export both source and compiled assembly, but UserDefinedMethods doesn't support it (as far as I read on forum).
              Is there any other way to share methods that support exporting?
              Create a different partial class in the Strategy namespace, and use that. The simplest way is to just save your UserDefinedMethods.cs file to a different name, then remove all the duplicate stuff from UserDefnedMethods.cs itself.

              Comment


                #8
                Originally posted by InteRadek View Post
                Hello,
                how can I reach methods from external DLL? I've created a custom strategy with my methods, exported it as an assembly and imported back to NT. Now I have a DLL file in my Custom folder and it is visible in Strategy -> right click -> References. But methods are not visible in strategy.
                What am I missing?

                EDIT> For example I have external DLL file with content:
                Code:
                namespace NinjaTrader.Strategy
                 {
                  public class Methods : Strategy
                  {
                   public int Test()
                   {
                     return 1;
                   }
                  }
                 }
                And I have my Strategy:
                Code:
                namespace NinjaTrader.Strategy
                 {
                  public class Buy : Strategy
                  {
                   int value = Test();
                  }
                 }
                What to add to use Test() in the Buy strategy?
                Strategies do not cross-communicate, so that is not a workable proposition. At least, not without creating a new instance of your called Strategy and then calling its methods. It is much simpler to just use a partial class.
                Last edited by koganam; 07-15-2015, 07:37 AM.

                Comment


                  #9
                  Originally posted by InteRadek View Post
                  Hello,
                  how can I reach methods from external DLL? I've created a custom strategy with my methods, exported it as an assembly and imported back to NT. Now I have a DLL file in my Custom folder and it is visible in Strategy -> right click -> References. But methods are not visible in strategy.
                  What am I missing?

                  EDIT> For example I have external DLL file with content:
                  Code:
                  namespace NinjaTrader.Strategy
                   {
                    public class Methods : Strategy
                    {
                     public int Test()
                     {
                       return 1;
                     }
                    }
                   }
                  And I have my Strategy:
                  Code:
                  namespace NinjaTrader.Strategy
                   {
                    public class Buy : Strategy
                    {
                     int value = Test();
                    }
                   }
                  What to add to use Test() in the Buy strategy?
                  Hello InteRadek,

                  There are two ways you can access the Test() method from your Methods class:
                  1. Create an instance of the Methods class in your Buy class and access your Test() method that way
                    Example:
                    Code:
                    Methods methods = new Methods();
                    int value = methods.Test();
                  2. Make your Test() method a static method
                    Example:
                    Code:
                    public class Methods : Strategy
                    {
                         public static int Test()
                         {
                              return 1;
                         }
                    }
                    
                    public class Buy : Strategy
                    {
                         int value = Methods.Test();
                    }
                  Zachary G.NinjaTrader Customer Service

                  Comment


                    #10
                    Thank you koganam, you helped me second time, I appreciate that very much.
                    Thank you ZacharyG for examples, they'd cleared my view on some things.

                    Both solutions are great. But I was looking further and I had found a perfect solution. I have used inheritance. One simple change in my code and everything works as it should. All my methods are visible for my strategy and no additional code editing is needed.
                    Code:
                    public class Buy : [B]Methods[/B]

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by traderqz, Today, 12:06 AM
                    5 responses
                    8 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by Mongo, Today, 11:05 AM
                    2 responses
                    7 views
                    0 likes
                    Last Post Mongo
                    by Mongo
                     
                    Started by guillembm, Today, 11:25 AM
                    0 responses
                    3 views
                    0 likes
                    Last Post guillembm  
                    Started by Tim-c, Today, 10:58 AM
                    1 response
                    3 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    Started by traderqz, Yesterday, 09:06 AM
                    4 responses
                    29 views
                    0 likes
                    Last Post traderqz  
                    Working...
                    X