Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Optimizing on Time

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

    Optimizing on Time

    Hi

    Struggling a bit with this, I'm trying to optimize on time - so the system buys @ 9:00am or 10:00am etc
    The strategy works fine with default values but once I tried to optimize I realized I could not.

    So it seems I can optimize on integers but not time, so I changed the code in if (State == State.SetDefaults)
    to go from
    OpenTime = DateTime.Parse("09:00", System.Globalization.CultureInfo.InvariantCulture) ;
    CloseTime = DateTime.Parse("14:00", System.Globalization.CultureInfo.InvariantCulture) ;


    Didnt work:
    OpenTime = new DateTime( 2018, 01, 01, OptimStr, 0, 0);
    CloseTime = new DateTime( 2018, 01, 01, OptimStp, 0, 0);

    I also tried this - but also didnt work
    OpenTime = DateTime.Parse("00:00", System.Globalization.CultureInfo.InvariantCulture) .AddHours(OptimStr);
    CloseTime = DateTime.Parse("00:00", System.Globalization.CultureInfo.InvariantCulture) .AddHours(OptimStp);


    What does work:
    OpenTime = new DateTime( 2018, 01, 01, 9, 0, 0);
    CloseTime = new DateTime( 2018, 01, 01, 14, 0, 0);

    How come if the 14 is changed from 14 to "OptimStp" then it stops working?
    The values on the Strategy Analyser is 9 and 14 as well.
    Everytime I try to print out anything the Output window clears if I run the backtest so I can never seem to print out to a log.


    Indicator code:
    [NinjaScriptProperty]
    [Range(0, 24)]
    [Display(Name="OptimStr", Description="OptimStr", Order=1, GroupName="Parameters")]
    public int OptimStr { get; set; }

    [NinjaScriptProperty]
    [Range(0, 24)]
    [Display(Name="OptimStp", Description="OptimStp", Order=2, GroupName="Parameters")]
    public int OptimStp { get; set; }

    #2
    Hello bchip,

    Thanks for your post.

    Where else are updating your DateTime variables? If you are only setting these in State.SetDefaults, I would not expect this to effect the optimization. If you update your DateTime variables in OnStateChange under State.DataLoaded or State.Configure, I would expect these variables to be updated from the Optimizer. SetDefaults is simply used to assign default parameter values, so they will not update as the optimization modifies a variable.

    If you are seeing something different, could you attach a simple example that demonstrates what you have set up exactly?

    I look forward to being of further assistance.

    Comment


      #3
      Hi

      Yes my problem was that the values for OptimStr & OptimStp was in the State.SetDefaults
      I changed it to State.Configure and it works now.

      Thank you

      Comment


        #4
        A working example for the community on optimizing time.
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        79 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        45 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        29 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        32 views
        0 likes
        Last Post TheRealMorford  
        Started by Mindset, 02-28-2026, 06:16 AM
        0 responses
        65 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Working...
        X