Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using external custom classes

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

    Using external custom classes

    Hello, I'm trying to recreate my strategy, it has become very cluttered, and i want to make it as modular as possible within the limits of the engine.

    The strategy uses a method that calls a sequential order of methods in a row, and they all collapse into a bool value at the end allowing me to enter a trade or not.

    Code:
            if (isEligibleForTrade())
            {
                EnterLong(1, entryOrderName);
            }
    
            protected bool isEligibleForTrade()
            {
                if(method1()) return true
                else if (method2()) return true
                etc.
    
                return false; // if all failed.
            }
    
            protected bool method1()
            {
                if(stageOne() && StageTwo())
                {
                    return true;
                }
                return false;
            }​
    Because of the nature of these methods, the amount of logic is beginning to be too much for one script, and i want to seperate those methods into classes and use them in my strategy, this way they are eligible for other strategies to use as well.

    My questions are:

    1. In the case that i just want to take each method (i.e. method1/x/y/z) and separate them into classes of their own for a cleaner and more modular code, where should i store those classes, and as what? a strategy? an indicator? an addon? all these classes use bar series data, and should be able to support it by default if possible.

    2. in the case that it can be done the way i wanted, do i need to add the bar series of the main strategy to those classes? or is the object reference created in the strategy enough?


    Any reference to other solutions/ recommended data structures/ documents will be highly appreciated!

    Thanks in advance, Aviram Y.

    #2
    Hello Aviram Y,

    The best solution for making custom classes would be to make a new C# class inside a file within the addons folder. You can use the NinjaScript editor to right click -> create addon then delete the class inside the file leaving only the using statements. You can define your own namespaces in that file as well.

    You can then make your own class with your custom methods inside that file.

    A standard C# class does not inherit from Strategy so you would need to pass either the instance of the strategy or whatever values you needed to the method. Here is a very simple example:

    Code:
    namespace MyCustomNamespace {
      public class MyCustomMethods // goes in addon folder
      {
         protected bool isEligibleForTrade()
         {
             if(method1()) return true
            else if (method2()) return true
            etc.
    
            return false; // if all failed.
         }
         public void MethodThatUsesStrategyProperty(MyCustomStrategy myStrategy)
         {
            myStrategy.Print(myStrategy.Close[0]);
         }​
      }
    }
    in your strategy:

    Code:
    private MyCustomNamespace.MyCustomMethods myMethods = new MyCustomNamespace.MyCustomMethods();
    
    protected override void OnBarUpdate()
    {
        myMethods.MethodThatUsesStrategyProperty(this);
    
        if(myMethods.isEligibleForTrade())
        {
    
        }
    }
    You may come across other examples of making classes in NinjaScript such as using custom inheritance on your classes however we do not suggest using custom inheritance unless you are a very advanced C# developer that can debug that type of code in case of issues. Using a standard C# class like this is the most flexible way that does not interfere with exporting scripts or have issues with the platforms internal mechanics.
    Last edited by NinjaTrader_Jesse; 03-22-2023, 01:35 PM.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you.
      I will try it out!

      Comment


        #4
        You might try using an abstract base class.

        A good example is available here.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by MiCe1999, Today, 06:49 AM
        0 responses
        7 views
        0 likes
        Last Post MiCe1999  
        Started by gaz0001, 12-12-2023, 09:36 AM
        13 responses
        282 views
        0 likes
        Last Post kevinenergy  
        Started by timwey, Today, 06:01 AM
        0 responses
        6 views
        0 likes
        Last Post timwey
        by timwey
         
        Started by patrickgeys7, Today, 06:01 AM
        0 responses
        2 views
        0 likes
        Last Post patrickgeys7  
        Started by ayham12, Today, 05:42 AM
        0 responses
        11 views
        0 likes
        Last Post ayham12
        by ayham12
         
        Working...
        X