Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Optimizing bool/enum/TimeSpan parameters

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

    Optimizing bool/enum/TimeSpan parameters

    Here is a little class to help anyone who wants to optimize properties in their strategies that are booleans, enums, or TimeSpans.

    Since NT only supports optimizing on integer and double parameters, all your optimizable parameters need to be of these types. The attached OptimizableProperty<T> class implements a generic property that can be treated as both an integer for exposing to NT optimization and a more natural type for the internals of your strategy. Currently it supports bool, enums, and TimeSpan types.

    To create one, simply specify the type of property you desire in the angle brackets. E.g.:

    public enum MovingAverageType { Simple, Exponential };

    OptimizableProperty<MovingAverageType> maType = MovingAverageType.Simple;

    OptimizableProperty<bool> fade = true;

    You can then treat the property either as an integer or whatever type you specified in the angle brackets:

    if (fade) {
    ...
    }


    if (maType == MovingAverageType.Exponential) {
    ...
    }


    ...


    public int Fade {
    get { return fade; }
    set { fade = value; }
    }


    public int MAType {
    get { return maType; }
    set { maType = value; }
    }

    Calling OptimizableProperty<T>.ToString() will return the natural string value of the property (e.g. 'true' or 'false' for OptimizableProperty<bool>).

    The integer values for an OptimizableProperty<bool> are 0 (false) or 1 (true) while for an enum they range from 0 (the first enum value) to n-1 (the last value) where n is the number of values in the enum. For OptimizableProperty<TimeSpan> it is the number of minutes since midnight (the seconds portion is ignored).

    Note that in some cases you will need to explicitly cast the OptimizableProperty<T> to either int or bool/enum/TimeSpan if the compiler is unable to automatically determine which type you want.

    To install, copy the attached file to My Documents\NinjaTrader 6.5\bin\Custom\Type and compile a strategy once.
    Attached Files

    #2
    Thanks for sharing this -Swig-!
    BertrandNinjaTrader Customer Service

    Comment


      #3
      This is an interesting workaround. I assume you have to then do data entry in the optimizer parameters dialog as an int, though, correct? You don't get the dropdown you get for enum parameters now?

      Comment


        #4
        Originally posted by Anagoge View Post
        This is an interesting workaround. I assume you have to then do data entry in the optimizer parameters dialog as an int, though, correct? You don't get the dropdown you get for enum parameters now?
        Yes, nope, respectively. Unfortunately the parameters have to be exposed to NT as int or double or else it won't allow optimisation on them.

        The main reason I put this together was so that NT's limitation to optimising doubles/ints wouldn't prevent use of more natural data types (i.e. bool/enum/TimeSpan) resulting in cleaner strategy code.

        All it really does is hide the boilerplate int <-> bool/enum/TimeSpan conversion code that would otherwise be needed.


        -Swig-

        Comment


          #5
          this is great. lets hope that NT7 allows one to set enum/timespan/datetime values in the optimization dialog...

          Comment


            #6
            Is the above still the best way to optimize on non-integer, non-double values?

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by adeelshahzad, Today, 03:54 AM
            0 responses
            0 views
            0 likes
            Last Post adeelshahzad  
            Started by CortexZenUSA, Today, 12:53 AM
            0 responses
            2 views
            0 likes
            Last Post CortexZenUSA  
            Started by CortexZenUSA, Today, 12:46 AM
            0 responses
            1 view
            0 likes
            Last Post CortexZenUSA  
            Started by usazencortex, Today, 12:43 AM
            0 responses
            5 views
            0 likes
            Last Post usazencortex  
            Started by sidlercom80, 10-28-2023, 08:49 AM
            168 responses
            2,266 views
            0 likes
            Last Post sidlercom80  
            Working...
            X