Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Portfolio of Strategies

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

    Portfolio of Strategies

    HI All,

    I have successfully managed to run four trade ideas (strategies) within one strategy file using AddDataSeries and carefully managed unique entrance tags and exit tags for associating fills with one another. I believe they are not impacting one another. Easy to test this by comparing the aggregate equity curve (and trade list) to each separate one when run as their own strategy class. By combining them, I can run a port of independent strategies and optimize for some port metric.

    Let's assume for this thread that every BarSeries will be the same granularity: daily.

    I now want to be able to do this for say a dozen trade strategies. Some will be different futures run into the same strat, others will be different strategies run into the same future, etc. Instead of having it as one giant file, my interest is in building a class for each strategy and then having one file per strategy plus a single main file which I will call from Strategy Analyzer as the trade itself. From within the main file, I can call each strategy's class and pass in time series from each AddDataSeries etc.

    How I set it up is: I copied the same 'Using declarations' region from the main strategy. Also, I used the namespace NinjaTrader.NinjaScript.Strategies but I am not calling the public class that I build a ': strategy.' Finally, I am not using the OnStateChange method at its beginning.

    I am able to call the public class' public methods from the main strategy file just fine. BUT none of the basic default methods are available to me. For example, the basic built in methods like StochasticsFast(...).

    Can anyone assist in this pursuit? I'm not a terrific developer but I get the basics. Thank you in advance!

    -Tim



    Last edited by ozziyhtomit; 09-01-2020, 10:39 AM.

    #2
    Hello ozziyhtomit,

    Thanks for the post.

    While you can do this there are going to be some adjustments in how you write your code. In a custom class you wont have NinjaScript properties or methods like Print or CurrentBar, OnStateChange etc. You will need to pass in anything you need, your class is only a container.

    The reason that the strategy can use all of those items is that it inherits them from Strategy base at the top of the file and end of the class : Strategy. I would not suggest making each sub strategy Inherit, or working with custom inheritance at all and just pass whatever you need in.

    If you need a price in a method you can pass a double value, you can also work with the instance of the strategy if you wanted a strategy property. Here is an indicator which works directly with an instance of a strategy, the same concept would apply in your class if you want to access the strategy property. https://ninjatrader.com/support/help..._a_ninjasc.htm


    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks Jesse,

      Totally forgot about inheritance from the good ole C++ programming classes way back when.

      1. Ok so you don't recommend going the route of my classes inheriting from : Strategy. Why?

      2. I will review the link now and give feedback here. Really appreciate it!

      3. Just as a quick bigger picture question: I am sure many folks want this functionality. Jesse, would you say I am going about this in the simplest way for an amateur programmer? If not, what would be the better way?

      Thank you


      Comment


        #4
        Hello ozziyhtomit,

        1. Ok so you don't recommend going the route of my classes inheriting from : Strategy. Why?
        That just becomes a strategy at that point. If you try and make that a base class and inherit from it in other scripts it can get messy, your original plan of creating a class is likely the easiest solution for this type of task. You just need to remember to pass in an instance to the strategy if you wanted to use Print or something else NinjaScript related.

        HTML Code:
        public void MyMethod(Straetgy myInstance)
        {
            myInstance.Print(""); 
        }
        3. Just as a quick bigger picture question: I am sure many folks want this functionality. Jesse, would you say I am going about this in the simplest way for an amateur programmer? If not, what would be the better way?
        Really the only more simple way would be to just duplicate the strategy for each specific situation however that will mean you need to edit each strategy individually if something they all share changes. Your approach with the class is probably the most simple for the given goal. This does rely on using some non NinjaScript features, just standard features of C# so you can learn more about this topic if you wanted to dive deeper. For working with the class you are really just using object oriented programming in C#, a class and instances so any general C# tutorials that cover OOP will help here.

        I look forward to being of further assistance.

        JesseNinjaTrader Customer Service

        Comment


          #5
          Jesse,

          1. The SampleStrategyPlot looks like uses inheritance, no?

          "public class SampleStrategyPlot : Strategy" -> isn't that inheritance for SampleStrategyPlot from Strategies

          Again I am not a solid programmer so I am probably misunderstanding here. I believe you said to not use inheritance?

          2. In your response you wrote "You will need to pass in anything you need, your class is only a container." I am looking at the file SampleStrategyPlot. I don't see how this is being done. Can you help me understand what you mean?

          Thanks again Jesse

          Comment


            #6
            Hello ozziyhtomit,

            1. The SampleStrategyPlot looks like uses inheritance, no?

            "public class SampleStrategyPlot : Strategy" -> isn't that inheritance for SampleStrategyPlot from Strategies
            Again I am not a solid programmer so I am probably misunderstanding here. I believe you said to not use inheritance?
            Yes it does, what I meant was not to use inheritance on your custom sub class that you are making. Some users will mistakenly create a custom class, run into the problem you had of not being able to access NinjaScript items like Print and then add the inheritance thinking that should be the solution. That means you just made a strategy out of that custom class, but then you would have to add OnStateChange and OnBarUpdate etc.

            Your custom class would just be a class: public class MyClass { }

            And then you can use what is shown in the sample that I linked to, or passing the strategy instance to the class. The sample I linked to actually uses a public property to do that:

            [Browsable(false)]
            [XmlIgnore()]
            public NinjaTrader.NinjaScript.Strategies.SampleStrategyP lot Strategy
            {
            get;set;
            }

            If you replicated this in your class when you created your class from your strategy it would look like:

            Code:
            myClass = new MyCustomClass();
            myClass.Strategy= this;
            Then in the class you can use it by doing:

            Code:
            Strategy.Print("");


            2. In your response you wrote "You will need to pass in anything you need, your class is only a container." I am looking at the file SampleStrategyPlot. I don't see how this is being done. Can you help me understand what you mean?
            In that sample the Indicator is replacing the Class for the purpose of demonstration. If we assume your Class is the indicator that is how you would make the class to accept the Strategy as input. You can alternatively just accept different inputs in your classes methods:

            public void MyMethod(double someInput)

            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Concepts discussed here and here should also work with strategies.

              Comment


                #8
                Jesse,

                I almost have this working. In reality I don't entirely understand all the functionality so any help w basic language explaining this would be great. I can get the compile to work, but am getting the error "Object reference not set to an instance of an object."

                Let's call the name of the main strat I will be running from Strategy Analyzer "Main_Strat". The name of the separate class I am trying to run certain methods of per bar (from Main_Strat) I'll call "Trade_Strat".

                Here's what I have set up:

                In the Main_Strat:

                1. In first line of class Main_Strat:Strategy I have

                private Trade_Strat instanceOfMyClass;

                2. In "if (State == State.DataLoaded)" I have

                instanceOfMyClass = new Trade_Strat(45, 30, 125, 75, 15, 20, 25, -45); -> Using constructor to set values.

                3. "protected override void OnBarUpdate()" I have

                instanceOfMyClass.runPerBarCalculations(15); -> public method within Trade_Strat that I will use to run the sub-strategy's calcs and order actions.


                In the Trade_Strat:

                1. In "public void runPerBarCalculations(int someValue)" I have

                Main_Strategy.Print(someValue);


                2. At bottom of class I have

                [Browsable(false)]
                [XmlIgnore()]
                public NinjaTrader.NinjaScript.Strategies.Main_Strat Main_Strategy
                {
                get;set;
                }



                -> All I am trying to do is: pass a value into Trade_Strat and process it through a method I have passed in from Main_Strat (in this case Print).


                What am I missing?

                Thank you,
                Tim
                Last edited by ozziyhtomit; 09-02-2020, 07:59 AM.

                Comment


                  #9
                  Hello ozziyhtomit,

                  The error "Object reference not set to an instance of an object." means something was null, an object you used was null when you tried to use it.

                  This is likely the Main_Strategy property. You can add that to your constructor:

                  Code:
                  public Trade_Strat (otherParamers, NinjaTrader.NinjaScript.Strategies.Blossom_Class_MAIN strategy)
                  {
                  Main_Strategy = strategy;

                  Or you can just set it when you create the class:

                  Code:
                  instanceOfMyClass = new Trade_Strat(45, 30, 125, 75, 15, 20, 25, -45); -> Using constructor to set values
                  instanceOfMyClass.Main_Strategy = this;
                  I look forward to being of further assistance.
                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Got it! Awesome!!!

                    Thanks for your patience.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by hdge4u, Yesterday, 09:09 PM
                    0 responses
                    19 views
                    0 likes
                    Last Post hdge4u
                    by hdge4u
                     
                    Started by hdge4u, Yesterday, 09:02 PM
                    0 responses
                    10 views
                    0 likes
                    Last Post hdge4u
                    by hdge4u
                     
                    Started by stormcloudseven, Yesterday, 08:41 PM
                    1 response
                    10 views
                    0 likes
                    Last Post DavidTrades  
                    Started by GussJ, Yesterday, 01:24 PM
                    0 responses
                    24 views
                    0 likes
                    Last Post GussJ
                    by GussJ
                     
                    Started by frslvr, Yesterday, 12:40 PM
                    0 responses
                    16 views
                    0 likes
                    Last Post frslvr
                    by frslvr
                     
                    Working...
                    X