Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Master and Secondary Strategy files

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

    Master and Secondary Strategy files

    Hey,

    I want to reduce the amount of work when editing the same parts of code in multiple strategies and possibility of error doing so by just splitting strategy into several files, as my main code (with Entry/Exit/Money and Risk Management filters that are used in all strategies) is exactly the same, and only parts that change are specific entries and exits of each strategy.

    How can I refactor my strategies, so that all of them use main master file and only custom entries and exits?

    #2
    Hello UltraNix,

    Thank you for your post.

    There wouldn't be a way for two strategies to directly communicate with each other, but you could use custom partial classes in the Strategy namespace to hold common logic and then simply use the common logic in your script with the individualized entries and exits.

    Please see this section of our help guide on custom partial classes:



    Please let us know if we may be of further assistance to you.


    Kate W.NinjaTrader Customer Service

    Comment


      #3
      So I can only use methods from one partial class, or anything else?

      Comment


        #4
        Although Kate means well, I would forget the 'partial class' suggestion.

        There is a better way.

        Create your own abstract base class that inherits from Strategy.

        Code:
        abstract public class MyStrategyBase : Strategy
        Then your actual strategy inherits from MyStrategyBase.

        All of your common strategy code goes into MyStrategyBase.

        Comment


          #5
          Originally posted by bltdavid View Post
          Although Kate means well, I would forget the 'partial class' suggestion.

          There is a better way.

          Create your own abstract base class that inherits from Strategy.

          Code:
          abstract public class MyStrategyBase : Strategy
          Then your actual strategy inherits from MyStrategyBase.

          All of your common strategy code goes into MyStrategyBase.
          You attracted my interest. Could you tell me more and provide more resources, as I'm not a full-blown coder, just adapting what I see elsewhere.

          Comment


            #6
            Hello UltraNix,

            Thank you for your reply.

            Abstract classes are a C# concept and would be unsupported by NinjaTrader staff. There can be limitations using advanced C# concepts in NinjaScript, so your mileage may vary. As of now, we do not have any examples that can be used to navigate that path.

            A publicly available resource can be found here: https://www.geeksforgeeks.org/c-sharp-abstract-classes/

            Please let us know if we may be of further assistance to you.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Kate View Post
              Hello UltraNix,

              Thank you for your reply.

              Abstract classes are a C# concept and would be unsupported by NinjaTrader staff. There can be limitations using advanced C# concepts in NinjaScript, so your mileage may vary. As of now, we do not have any examples that can be used to navigate that path.

              A publicly available resource can be found here: https://www.geeksforgeeks.org/c-sharp-abstract-classes/

              Please let us know if we may be of further assistance to you.
              Thank you, even though examples in that website are not that easy for me to implement.

              Comment


                #8
                Sure.

                Study the attached files.

                The abstract base class "MyStrategyBase" acts like a middle-man.

                You're inserting this class into the inheritance hierarchy, and anything
                in MyStrategyBase has full and complete access to the class it inherits
                from -- in this case, the Strategy class.

                You add your own methods, variables, properties, to MyStrategyBase.

                The reason this is so powerful is that you are extending the 'Strategy'
                class with new 'features' -- sorta like what a partial class does -- but
                you can really fine tune the contents of MyStrategyBase for just one or
                two strategies -- or you can make it a common base that you use for all
                your strategies.

                Inheritance is the heart and soul of OOP, and the C# language supports
                inheritance extremely well.

                There are lots of resources on the web.
                Inheritance is a standard feature of the C# language.

                Good luck!



                For sample strategy attached, it will color the background based upon the
                begin/end times you specify (but stop coloring for the break period, which
                represents known high volatility times, like for an economic report).

                For ex,
                Trade BeginTime = 600
                Trade EndTime = 1100
                Break BeginTime = 900
                Break EndTime = 930

                Restricts trading to the hours 6am to 11am, with a 30min break starting
                at 9am. All times 24hr format, and all times are local.
                Attached Files

                Comment


                  #9
                  Originally posted by UltraNIX View Post
                  Thank you, even though examples in that website are not that easy for me to implement.
                  Hmm, you may have a lot of learning ahead of you.
                  (But that's ok, there are plenty of paths forward.)

                  I just wanted to say,
                  You actually asked about an advanced topic common to every professional
                  programmer everywhere: and that is how to best organize the code so you
                  have as little duplication and overlap as possible.

                  You even used a common code word among engineers -- the word "refactor"
                  suggested to me that you might know something about the need and benefits
                  of such code organization. Perhaps I was wrong -- but that's ok.

                  What I want to say is: Welcome to the world of Software Engineering 101.

                  I'm not being mean or condescending at all. I'm trying to say, if you are new
                  to this, then your wonderful journey of discovery is about to begin. The C#
                  language is very elegant and quite beautiful, but learning (more of) it may
                  come at a considerable cost -- mostly your time.

                  My point is: Don't give up!
                  Keep learning, the dividends from mastering the examples in that website link
                  will provide you enormous benefits as you move forward with NinjaScript.

                  Some very advanced concepts in the C# language were invented precisely
                  to help organize code. To do what you are asking, take my example and
                  understand how MyStrategyBase fits into the class hierarchy -- technically,
                  you would say "MyStrategyBase extends Strategy" -- then keep on reading
                  about C# classes and surfing the web for more examples and tutorials.

                  Hang in there!

                  Comment


                    #10
                    Originally posted by bltdavid View Post
                    Sure.

                    Study the attached files.

                    The abstract base class "MyStrategyBase" acts like a middle-man.

                    You're inserting this class into the inheritance hierarchy, and anything
                    in MyStrategyBase has full and complete access to the class it inherits
                    from -- in this case, the Strategy class.

                    You add your own methods, variables, properties, to MyStrategyBase.

                    The reason this is so powerful is that you are extending the 'Strategy'
                    class with new 'features' -- sorta like what a partial class does -- but
                    you can really fine tune the contents of MyStrategyBase for just one or
                    two strategies -- or you can make it a common base that you use for all
                    your strategies.

                    Inheritance is the heart and soul of OOP, and the C# language supports
                    inheritance extremely well.

                    There are lots of resources on the web.
                    Inheritance is a standard feature of the C# language.

                    Good luck!



                    For sample strategy attached, it will color the background based upon the
                    begin/end times you specify (but stop coloring for the break period, which
                    represents known high volatility times, like for an economic report).

                    For ex,
                    Trade BeginTime = 600
                    Trade EndTime = 1100
                    Break BeginTime = 900
                    Break EndTime = 930

                    Restricts trading to the hours 6am to 11am, with a 30min break starting
                    at 9am. All times 24hr format, and all times are local.
                    That's a start! I downloaded both files and took a first look, trying to understand ins and outs. WIll need to spend more time, but, as I said, it's a start, thank you!

                    Comment


                      #11
                      Originally posted by bltdavid View Post

                      Hmm, you may have a lot of learning ahead of you.
                      (But that's ok, there are plenty of paths forward.)

                      I just wanted to say,
                      You actually asked about an advanced topic common to every professional
                      programmer everywhere: and that is how to best organize the code so you
                      have as little duplication and overlap as possible.

                      You even used a common code word among engineers -- the word "refactor"
                      suggested to me that you might know something about the need and benefits
                      of such code organization. Perhaps I was wrong -- but that's ok.

                      What I want to say is: Welcome to the world of Software Engineering 101.

                      I'm not being mean or condescending at all. I'm trying to say, if you are new
                      to this, then your wonderful journey of discovery is about to begin. The C#
                      language is very elegant and quite beautiful, but learning (more of) it may
                      come at a considerable cost -- mostly your time.

                      My point is: Don't give up!
                      Keep learning, the dividends from mastering the examples in that website link
                      will provide you enormous benefits as you move forward with NinjaScript.

                      Some very advanced concepts in the C# language were invented precisely
                      to help organize code. To do what you are asking, take my example and
                      understand how MyStrategyBase fits into the class hierarchy -- technically,
                      you would say "MyStrategyBase extends Strategy" -- then keep on reading
                      about C# classes and surfing the web for more examples and tutorials.

                      Hang in there!

                      I used the word "refactor", because I previously purchased and watched courses for NinjaTrading coding by NinjaCoding.net. So I am not a complete beginner, but far from being a serious coder/developer. My skills and knowledge are enough to create an automated algo, but in terms of structure, I am sure, a professional coder could refactor my code for more efficiency.

                      You are not mean or codescending, I am familiar with your posts and I found you being helpful in other topics.

                      What I consider as difficulty for me - all the resources on web are for pure C# and there are so few courses dedicated to NinjaTrader coding. It is interesting and I follow along, as long as I have resources. But once I am stuck, it gets difficult. I find myself having much more ideas than skills and knowledge that would let me implement those ideas.

                      Comment


                        #12
                        Excellent!
                        Last edited by bltdavid; 08-16-2023, 03:40 PM.

                        Comment


                          #13
                          Originally posted by NinjaTrader_Kate View Post
                          There can be limitations using advanced C# concepts in NinjaScript, so your mileage may vary
                          Hi Kate,

                          With regard to use of an 'abstract base class' ...

                          Can you provide any specific examples where its use caused a problem?

                          I mean, to what limitations are you referring to?

                          Thanks
                          Last edited by bltdavid; 11-06-2021, 11:23 PM.

                          Comment


                            #14
                            Hello bltdavid,

                            There can be limitations with using more advanced C# concepts like inheritance (and other concepts) where NinjaScript wrappers for indicators cannot be properly applied.

                            We have some examples showing how partial classes can be used in NinjaScript, but we do not have examples prepared for abstract classes and methods.

                            Since we have not fully navigated those ideas, we would not officially support/suggest using those concepts.
                            JimNinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_Jim View Post
                              There can be limitations with using more advanced C# concepts like inheritance (and other concepts) where NinjaScript wrappers for indicators cannot be properly applied.

                              We have some examples showing how partial classes can be used in NinjaScript, but we do not have examples prepared for abstract classes and methods.

                              Since we have not fully navigated those ideas, we would not officially support/suggest using those concepts.
                              I would note that since NinjaScript strategies don't have wrappers,
                              no such limitation exists when an abstract base class is used
                              with a Strategy-- is that correct?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by jpkulkarni, Today, 10:12 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post jpkulkarni  
                              Started by jpkulkarni, Today, 09:27 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post jpkulkarni  
                              Started by Scalper888, Yesterday, 05:09 PM
                              5 responses
                              23 views
                              0 likes
                              Last Post Scalper888  
                              Started by CardozoPeggy, Today, 06:01 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post CardozoPeggy  
                              Started by aaaa7389, Today, 05:33 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post aaaa7389  
                              Working...
                              X