Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to 'copy' properties in a constructor?

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

    How to 'copy' properties in a constructor?

    Hello,

    I am confused about how best to 'capture' the values of some Strategy Properties into an instance of a class.

    What I want to do is use the values of some properties as the "base" parameters in my logic but also have them over written by some other Strategy Properties depending on the situation.

    I want to make a new class whose constructor would create default values copying the "base" parameters but received an error that I need an instance of the strategy. I tried supplying 'this.' in several different ways but I couldn't figure it out.

    One solution presented by Visual Studio was to turn the Strategy Properties into static ones and it seemed like it would work but I read a post that NT is not built with static variables in mind.

    As a laborious workaround, I made a method that would create a new blank class and then populate the values using the "base" parameters. This should work but any time in the future if I change the Strategy Properties, I'll have to change the class as well as the method.

    So, some final questions:

    1. How can I correctly access the current instance of the Strategy Properties in a class constructor?

    2. Would turning the Strategy Properties into static ones cause any problems?

    3. What version of C# and .NET does the most recent NT8 use?

    4. Is there a better way to do this?
    Thanks!
    Last edited by hillborne; 09-06-2020, 10:53 PM.

    #2
    Hello hillborne, thanks for your post.

    1. If you need access to the strategy class from some addon class, you would need to pass in "this" (type StrategyBase) into the class to access a strategies properties. So your custom class would take in a StrategyBase parameter into the constructor, then when you create an instance of that class in your strategy, pass in "this" from the strategy.

    2. You can not change the default strategy properties to static because the Strategy class (and its parent classes) are not static. A static class is a class that will only have one instance of the class made at run time, and new instances can not be created. I have this example that demonstrates a use case:
    https://ninjatrader.com/support/foru...dsaddonexample

    3. NinjaTrader 8 uses .NET 4.5 and C# 5.0.

    I look forward to assisting.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris,

      Thanks for the fast reply.

      Could you clarify how to use StrategyBase to access a property in my specific Strategy or provide a simple example? I think I need to send an instance of myStrategy but not sure. Please note that the class I'm creating is within the strategy, not external.

      Here's what I'm trying to do:

      Code:
      [NinjaScriptProperty]
      [Range(0, 10)]
      [Display(ResourceType = typeof(Custom.Resource), Name = "StopDistanceBasePropValue",
      Description = "Stop distance option 1", Order = 1, GroupName = "050 Main Parameters")]
      public int StopDistanceBasePropValue { get; set; }
      
      [NinjaScriptProperty]
      [Range(0, 10)]
      [Display(ResourceType = typeof(Custom.Resource), Name = "StopDistanceSpecialValue",
      Description = "Stop distance special value", Order = 2, GroupName = "050 Main Parameters")]
      public int StopDistanceSpecialValue { get; set; }
      
      public class Test
      {
      public int StopDistanceBase;
      
      Test(StrategyBase strategyBase) //Goes here?
      {
      this.StopDistanceBase = strategyBase  ?? StopDistanceBasePropValue  //......? How to get StopDistanceBasePropValue here?
      }
      }
      
      if(certainCondition)
      {
         myTest.StopDistanceBase = ......StopDistanceSpecialValue
      }
      Depending on the conditions, I want to change the 'default' value of StopDistanceBase inTest class to be StopDistanceSpecialValue instead of StopDistanceBasePropValue.

      I want them both to be properties because I can iterate them in backtesting.

      Also, in regards to you point #2: I didn't want to make the strategy class static, just the properties. Is that a problem? I could see it being a problem during backtesting if IsInstantiatedOnEachOptimizationIteration was false, but otherwise?
      Last edited by hillborne; 09-07-2020, 11:40 PM.

      Comment


        #4
        Hello hillborne, thanks for your reply.

        Since the class will be within the strategy itself, there is no need to pass in the StrategyBase. It would be written more or less like this:

        https://pastebin.com/n4Ua8gDw

        You can use static variables in a non static class, but you must always access the static variable from the class name not the class instance. In NinjaScript, you don't actually perform the instantiation of the Strategy class, so you have no handle to the object (we never call "new Strategy()") and there would no scenario where you would access StrategyName.Property from any other .cs file.

        I am having trouble understanding what the primary goal is here, could you clarify further?
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hi Chris,

          I would like to have a parameter-less constructor in a class retrieve the values of about 10 properties. These properties describe how to enter and exit a trade. Like the number of contracts, stop distance, whether to trail the stop, etc.

          That instance represents a generic order instruction set and are passed to a TradeManager() method that will enter, watch, and exit a trade using those instructions if appropriate.

          However, if certain chart conditions are recognized by Trademanager(), some of those default properties will be replaced with values from other properties. Like entering a scalp trade would be different than a with-trend trade so maybe a few values are swapped. I want to use properties because they can be iterated during backtesting.

          In any case, I was trying to cut down on copying/pasting/renaming those 10 properties for every possible condition OR having to pass in 10 parameters to every instance of the order instructions class because if anything changes, I'll have to do lots of renaming and the duplication bugs me.

          I noticed in your example that you aren't referencing a StrategyName.Property and not using them as default values in a parameter-less constructor.
          And this is because there is no accessible instance of the strategy available? So, it's not possible...

          The only way I've figured out how to do this is to have a method create a blank instance of the OrderInstructions class and return it filled out with the property values. But why can a method access the StrategyName.Property but not a constructor? I'm sure it's a basic programming concept I've overlooked but I'd appreciate it if you could point it out.

          Thanks again!

          Comment


            #6
            Hello hillborne, thanks for your reply.

            There is not a way to get parameters from the strategy unless you pass them into the constructor of a custom class, but if the class is already a member of the main Strategy class, there would not really be a need for a custom class in the first place unless its for organization purposes. If you wanted to, you could make a "fancy" inheritance system, where you define classes that inherit from the Strategy class itself and then define properties of their own. I have never tried such a thing, and the Strategy class is already build off a complex inheritance system, so this idea would need to be tested thoroughly. It might also be more straight forward if you define all of the needed parameters and branch off the strategy logic all within the Strategy class no matter how much code is produced.

            Best regards.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Ah! I think I understand now. When I create a method within the strategy to pass in the property values to my custom class, it's operating within the scope of the strategy.
              A class created within the strategy is one level lower in the hierarchy and has no access to the parent strategy.

              So, even the people who make and sell external add-ons can't access the specific instance of the Strategy.Property directly? They'd have to ask the user to copy some method into the strategy for access to find out the Time in Force, for example?

              Comment


                #8
                Hello hillborne, thanks for your reply.

                That is correct, any addon in the system can not make an instance of a strategy, so they do not have direct access to the object handle. One would need to set up a static class like I do in this forum post that contains a StrategyBase object, then from the strategy you would need to set the StrategyBase object from the addon to "this", then you would be able to get properties from the strategy. Again, that would need to be thoroughly tested.

                Kind regards,
                -ChrisL
                Chris L.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by hudqwmkohbgp, Today, 10:42 PM
                0 responses
                1 view
                0 likes
                Last Post hudqwmkohbgp  
                Started by hudqwmkohbgp, Today, 10:41 PM
                0 responses
                0 views
                0 likes
                Last Post hudqwmkohbgp  
                Started by Bidder, Today, 09:16 PM
                0 responses
                7 views
                0 likes
                Last Post Bidder
                by Bidder
                 
                Started by connorgrant5, Today, 07:12 PM
                0 responses
                15 views
                0 likes
                Last Post connorgrant5  
                Started by Trader_55, Today, 06:01 PM
                1 response
                15 views
                0 likes
                Last Post NinjaTrader_EricB  
                Working...
                X