Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multiple classes within strategy

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

    Multiple classes within strategy

    Hello,

    I want to use multiple classes within a strategy however I can't get this working. The OnBarUpdate portion of the strategy searches for the criteria to enter into a position and then creates an object to handle setting stop losses and profit targets as they can change on a tick by tick basis. The objects are stored within a list - I think the problem is happening here. When I try to access the object through the list it gives me the error "Object reference not set to an instance of an object." I believe I am instantiating the object correctly (I use the new keyword when intiailizing the list). Any ideas about what's not working?

    I can provide source if needed.

    Thanks!

    #2
    So you have an object in your list which contains methods to manipulate stop and profit targets, is that what you mean?

    If this is the case it may be that you are trying to create stop and targets for an order that has not been filled yet, check the internal order handling rules section of the manual.

    otherwise post the code and I'll take a look

    Comment


      #3
      jaf123, welcome to the forums - do you check for 'null' objects prior to accessing those?

      Comment


        #4
        @sefstrat - The problem I'm having is instantiating the object and "getting it into" the list. The object itself doesn't actually contain any entries/stop losses/profit targets, it just has a method which draws a green diamond on the bar which it instantiated.

        @NinjaTrader_Bertrand - Yes, I have a second boolean list that records whether or not the object list has an instantiated object within it.

        I'm not comfortable posting my entire strategy here. Here are the portions with code that pertains the objects creation.

        Firstly, the variables namespace:

        Code:
         
                   private List<bool> tradesUsed = new List<bool>();
                   private List<Trade> trades = new List<Trade>();
                   private Trade temp = new esBBChris_Trade();
        Here's the code within the Initialize method that is responsible for populating the list.

        Code:
                        for (int i=1;i<=10;i++) 
                        {
                        temp = new Trade();
                        trades.Add(temp);
                        tradesUsed.Add(false);
                        }
        If I try to call my test method EnterLongTrade right after the line trades[i - 1] = temp; it fails as well.

        This is the code that occurs once the entry rules have been met:
        Code:
        int tempInt = FindFreeIndex(); // FindFreeIndex enumerates the tradesUsed loop and returns an index that is equal to false.  It returns 10 if there are no free indicies.
                                if (tempInt != 10) {
                                    Print(tempInt);
                                    //trades[temp] = new esBBChris_Trade();
                                    trades[tempInt].EnterLongTrade();
        FindFreeIndex's code:
        Code:
                    protected int FindFreeIndex() {
                    // If FindFreeIndex returns with a 10 it means that no objects were currently available to handle the trade.
                        for (int i=0;i<=9;i++) 
                        {
                            if (tradesUsed[i] == false) 
                            {
                                tradesUsed[i] = true;
                                Print("Instance: " + i + " at " + CurrentBar);
                                return i;    
                            }
                        }
                    Print("Need more indicies!");
                    return 10;    
                    }
        The object itself:
        Code:
           public class Trade : Strategy
            {
                
                public void Construct() 
                {
                return;    
                }
                
                public void EnterLongTrade() 
                {
                DrawDiamond("EnterLong" + CurrentBar, true, 0, Low[0] - 4*TickSize, Color.Green);
                Print("Enter at " + CurrentBar);
                }
                    
                
            }
        }
        Is the fact that Trade is inheriting the Strategy class a problem?

        Thanks for both of your responses!
        Last edited by jaf123; 08-04-2009, 10:35 AM.

        Comment


          #5
          I'd suggest to move the 'populating' list section out of the Intialize() into the OnBarUpdate() method.

          Comment


            #6
            Yeah Trade class should not inherit from strategy for what you are trying to do.

            Pass your strategy into Trade constructor if you need to access Strategy members.

            ie

            public class Trade
            {
            Strategy s;

            public Trade(Strategy strat) { s = strat; }

            .....
            }


            then inside your strategy:

            Trade t = new Trade(this);

            Comment


              #7
              Why exactly is it better to pass the strategy through the constructor of Trade rather than just have the Trade class simply inherit the strategy class?

              Comment


                #8
                Common OO basics. Inheritance is an "IS A" specification, and a Trade IS NOT a Strategy.

                I do something similar here - if you do more complex order management, the Ninja base methods pretty immediately get pretty unusable without a lot of management code. Not saying they are bad, but you end up with a TON of IOrder references that have no correlance and need to be managed.

                I basically am writing a "BlackNinja" pack (a dll that I will reference) that, among other things, contains a TradeManager (all in all about a dozen classes so far) that allows me in the strategy to have a lot easier order management as well as track the current status in the various trade positions in a trade.

                Personally I think Ninja is a terrible solution for developing this. Not that Ninja per se is bad, but it gets really cumbersome pretty fast - the editor is pretty basic, and handling multiple classes is pretty painfull. Thus my decision to move development of this base stuff out of Ninja. In Visual Studio (2010 beta is really nice) I have a lot better support for complex development scenarios (including this arcane little things supposedly not needed when developing strategies called source control). I can then add the DLL to Ninja and reference the helper classes from within the strategy.

                Comment


                  #9
                  In addition to what NetTecture said, it is also very inefficient to have Trade inherit from Strategy.

                  If you use reflector to look at the metadata for StrategyBase, you will see its a monsterous class with hundreds of fields, properties and methods. So if your trade class inherits from Strategy and thus StrategyBase in turn, you are wasting quite a bit of memory. That is definitely not something you want to do considering the memory handling problems that plague the current version of NT.

                  Speaking of which, monster classes like StrategyBase are generally considered poor OO design for exactly this reason, they almost always lead to bad resource handling.

                  overloaded classes + singleton pattern + managed gc == black hole

                  Comment


                    #10
                    >> Trade inherit from Strategy
                    Class "Trade" is not inherited from class "Strategy" (nor from class "StrategyBase")

                    Comment


                      #11
                      Thanks a lot for all your help guys! The object is now working the way it should be.

                      Comment


                        #12
                        It would be really nice to see this issue discussed in more detail with further examples. This was a big help in getting my methods/classes seperated from my strategies and centralizing everything. The cutting and pasting and using Regions was horrible.

                        I am really surprised there isn't more people asking about this as well as more support from NT in this regard. I am grateful to have found this thread.

                        Thank you to all who participated.

                        Comment


                          #13
                          Originally posted by NinjaTrader_Dierk View Post
                          >> Trade inherit from Strategy
                          Class "Trade" is not inherited from class "Strategy" (nor from class "StrategyBase")
                          It is in the sample code further down:

                          public class Trade : Strategy
                          {

                          Not sure what your C# looks like, but in this plant the ": Strategy" indicates inheritance FROM strategy.

                          Comment


                            #14
                            NinjaTrader.Strategy.Trade (NT's own "Trade" class) is not derived from NinjaTrader.Strategy.Strategy nor NinjaTrader.Strategy.StrategyBase.

                            I suggest not using class names which already are used by NT (in fact you might even run into compile problems).

                            Comment


                              #15
                              Good point. Can one use an override?

                              Originally posted by NinjaTrader_Dierk View Post
                              NinjaTrader.Strategy.Trade (NT's own "Trade" class) is not derived from NinjaTrader.Strategy.Strategy nor NinjaTrader.Strategy.StrategyBase.

                              I suggest not using class names which already are used by NT (in fact you might even run into compile problems).

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              648 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              369 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              108 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              572 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              573 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X