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

Problem Creating Order Group Class

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

    Problem Creating Order Group Class

    Look I have a number of orders to manage within my strategy. Each has it's only stop loss and/or target profit and/or trail stop.

    They have different prices. They must be tracked whether executed or not. I need to keep all this information for each group, signal name, IOrder reference, trail stop amount, stop loss amount, etc.

    When I cancel or change the order, I need to use all of those again.

    This is a PERFECT opportunity to make a class to contain all of this for good object oriented design. I call this the "OrderGroup" class.

    However, in my "OrderGroup" subclass, it lacks access to the functions like EnterLongLimit() or data object. So I tried the following:

    1. I tried making my OrderGroup class extend Strategy. That gave it access to all the functions. But when I try to load this strategy onto a chart, NT crashes and dies completely.

    2. So I tried passing in a reference to the Strategy object on the constructor of my OrderGroup class. But then it complains that EnterLongLimit() and other information are all protected methods. Why did you make them protect? That means they can ONLY be called from within the strategy class.

    3. Now, I am trying to to continue with number 2 above make making a public LongLimit() method in my strategy that simply calls the protected EnterLongLimit(). That compiles find but NT still horribly crashes when it tries to load the strategy onto a chart.

    I have commented out the code related to my OrderGroup class and then it loads onto the chart fine.

    Is there any way to do this other than in the Strategy object itself? I was excited about your support of C# as NinjaScript due to the object oriented nature making it possible to use encapsulation and other good design to simplify maintenance of my strategies.

    Please advise if you know of another good way to handle multiple order groups withing a strategy.

    Wayne

    #2
    Unfortunately this is beyond what we provide support for.

    Hint: you should NOT implement your order group as a derived from StrategyBase but rather some custom class which may hold a reference to a strategy.

    Comment


      #3
      I'm not sure what the official answer is on this, but I've run into a variation of the same thing. I have a strategy with all of my position management stuff in it, and an abstract trigger class I can use to plug indicators into. I could not call methods like CrossAbove, Rising, etc. in the inner class. So in my UserDefinedMethods, I created public wrappers for the protected methods I needed access to. I pass a reference to Strategy into the inner classes. One extra step but worth it for the cleaner design.

      Comment


        #4
        I found the reason that NT was crashing was because I had a property typo that caused and endless recursion loop of a function. I fixed that.

        Afterwards, I got the OrderGroup class working by passing the strategy object into the OrderGroup object and writing wrapper methods in the strategy object to expose the methods I need. That worked great except for the added hassles of making wrapper functions for everything

        That really stinks that the Strategy methods are all protected. They should be public so we can pass the strategy into other objects to help manage stuff.

        It would be a simple change in the next release to make the access public instead of protected and never impact or affect any customers existing code right now.

        It would simply open the door for much simpler orbject oriented design of strategies. Please add this to the list and do it as soon as possible.

        Sincerely,
        Wayne

        Comment


          #5
          Guys, could you please compile a list of NinjaScript properties which you wanted to see declared as "public".

          I would not want to just switch any occurrence of "protected" to "public" shortly before release to not break any of the sensitive code (.NET reflection is used NT internally), but rather go case by case.

          Thanks

          Comment


            #6
            I though about using reflection myself to access these since it allows access to protected method using BindingFlags.NonPublic. But it seems like a lot of code to do something quite simple. So I appreciate very much your willingness to go public with some of the methods.

            Making a list, however, is a challenging request. It's difficult because my first thought would be to include just the order methods. But in my Order Group I also needed access to mundane stuff to like Print() and Log(). Then I also wanted Time[] so I could print the bar time when debugging.

            But I can later see a need for separate classes to handle the trading rules. Guess what? That means all the indicators, values, high, low, close, etc. need to be public.

            I understand that since you use reflection it means also mean changing the BindingFlags.NonPublic to Public or adding Public as an additional flag. That could take more time and testing. I see your point.

            One issue I'm wondering about is that I'd like to handle the events for my order in my order class. But only the Strategy object gets called with the overridable methods. So I'll need to code to look up the right OrderGroup object to delegate the event. That will work but is there a better way?

            In short you could just do the fields I refer to above. But you know Murphy's law. It'll be the ones you don't do that others will need.

            With your 6.5 version offering better order handling with GTC. You're going to attract more serious system traders who will appreciate the public fields, for sure when you are finally able to do them all.

            Sincerely,
            Wayne

            Comment


              #7
              >> That will work but is there a better way?
              None, I'm aware of.

              On the list issue: Could you please rip through the NinjaScript documentation and let me know which methods/properties you would want to see public? Thanks

              Comment


                #8
                Alright. I give in. I will go through them. It'll be this evening.

                I will start with the entire list in a spreadsheet and assign priority number to each. Then I'll send you the sorted list.

                That way you can decide how many you have time to do because that's the part I don't know.

                Sincerely,
                Wayne

                Comment


                  #9
                  Sounds great. Thanks

                  Comment


                    #10
                    Turns out, I could only think of 4 categories. This first are methods or properties that don't seem to make sense to be public. Those are left out of the list below. Also, another category are all the Indicators. Those could be public but not as necessary since a reference could be passed into another object to whatever indicator it needs.

                    So the list below only has 2 categories. Those marked 1 are the highest priority that strategy developers will appreciate as public for managing orders, position size, or strategy rules. Those marked 2 may also be important but probably less urgent.

                    NOTE: I marked the GetAtm*() methods as priority 2 only because I don't plan to use them since they can't be fully managed through the event handling. I don't know if others would agree.

                    This was also good exercise since I learned some useful methods that I didn't know about before!

                    Thanks for your attention to this.

                    Sincerely,
                    Wayne


                    1 AccountSize; 1 BarsArray; 1 BarsInProgress; 1 BarsPeriod; 1 BarsPeriods; 1 BarsRequired; 1 CalculateOnBarClose; 1 CancelOrder; 1 CancelShortOrder; 1 Close; 1 Closes; 1 CurrentBar; 1 DefaultInput; 1 DefaultQuantity; 1 EnterLong; 1 EnterLongLimit; 1 EnterLongStop; 1 EnterLongStopLimit; 1 EnterShort; 1 EnterShortLimit; 1 EnterShortOrder; 1 EnterShortStop; 1 EnterShortStopLimit; 1 EntriesPerDirection; 1 EntryHandling; 1 ExitLong; 1 ExitLongLimit; 1 ExitLongStop; 1 ExitLongStop; 1 ExitLongStopLimit; 1 ExitLongStopLimit; 1 ExitShort; 1 ExitShortLimit; 1 FirstTickOfBar; 1 GetCurrentAsk; 1 GetCurrentAskVolume; 1 GetCurrentBid; 1 GetCurrentBidVolume; 1 GetMedian; 1 High; 1 Highs; 1 Historical; 1 IncludeCommission; 1 Input; 1 Inputs; 1 Instrument; 1 Instruments; 1 Log; 1 Low; 1 Lows; 1 Median; 1 Medians; 1 Open; 1 Opens; 1 Performance; 1 Position; 1 Positions; 1 Print; 1 QuantityType; 1 Range; 1 RangeCounter; 1 RealtimeErrorHandling; 1 SetProfitTarget; 1 SetStopLoss; 1 SetTrailStop; 1 Slippage; 1 StopTargetHandling; 1 TickSize; 1 Time 1 TimeInForce; 1 Times; 1 Volume; 1 Volumes; 2 CandleOutlineColor; 2 Count; 2 CountIf; 2 CrossAbove; 2 CrossAbove; 2 CrossBelow; 2 CrossBelow; 2 ExcludeWeekend; 2 Falling; 2 GetAtmStrategyEntryOrderStatus; 2 GetAtmStrategyMarketPosition; 2 GetAtmStrategyPositionAveragePrice; 2 GetAtmStrategyPositionQuantity; 2 GetAtmStrategyRealizedProfitLoss; 2 GetAtmStrategyStopTargetOrderStatus; 2 GetAtmStrategyUniqueId; 2 GetAtmStrategyUnrealizedProfitLoss; 2 HighestBar; 2 LowestBar; 2 Name; 2 PriceAlert; 2 ToDay; 2 ToTime; 2 Typical; 2 Typicals; 2 Variable0; 2 Variable1; 2 Variable2; 2 Variable3; 2 Variable4; 2 Variable5; 2 Variable6; 2 Variable7; 2 Variable8; 2 Variable9; ? MaxProcessedEvents;

                    Comment


                      #11
                      Thanks. Will look into.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by manueldecastro, Today, 10:26 AM
                      0 responses
                      1 view
                      0 likes
                      Last Post manueldecastro  
                      Started by RaddiFX, Yesterday, 09:55 PM
                      4 responses
                      29 views
                      0 likes
                      Last Post RaddiFX
                      by RaddiFX
                       
                      Started by geotrades1, Today, 08:33 AM
                      5 responses
                      16 views
                      0 likes
                      Last Post geotrades1  
                      Started by Entwaze, 02-19-2024, 07:13 PM
                      2 responses
                      68 views
                      0 likes
                      Last Post MalachiHatfield  
                      Started by tkaboris, Today, 08:32 AM
                      5 responses
                      13 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Working...
                      X