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

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 bortz, 11-06-2023, 08:04 AM
        47 responses
        1,611 views
        0 likes
        Last Post aligator  
        Started by jaybedreamin, Today, 05:56 PM
        0 responses
        9 views
        0 likes
        Last Post jaybedreamin  
        Started by DJ888, 04-16-2024, 06:09 PM
        6 responses
        19 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by Jon17, Today, 04:33 PM
        0 responses
        6 views
        0 likes
        Last Post Jon17
        by Jon17
         
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        22 views
        0 likes
        Last Post Javierw.ok  
        Working...
        X