Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

TimeSpan Template Bug

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

    TimeSpan Template Bug

    Hello,

    There seems to be a bug in the template code when it comes to timespans and the TimeSpan variable.

    I have the following code in my properties:

    PHP Code:
    [NinjaScriptProperty]
    [Display(Name="Test Time", Description="Start of the daily trade time", Order=4, GroupName="Debuging")]
    public TimeSpan mTestTime
    { get; set; } 
    
    and set the value in

    PHP Code:
    protected override void OnStateChange()
    {
        if (State == State.SetDefaults)
        {
             mTestTime = new TimeSpan(2, 14, 18);
        }
    } 
    
    Everything runs fine, but when I save a template of my strategy, and load it later, the template does not save the values of the TimeSpan. All values become 00:00:00
    Could this please be looked at?

    V/R,
    --Maxim S

    #2
    Hello Maxim,

    Thank you for your post.

    TimeSpan objects can not be serialized, so I suspect that when you are saving your template the TimeSpan value is not properly saved and this is why you see 00:00:00 when you load the template again. There is a way to prevent this, however. You could convert the TimeSpan into a simple type, such as a string, so that the proper information may be serialized. We have an example "Using a simple type as the NinjaScriptProperty against types which cannot be serialized" on our help guide page here:



    Another type of object that does not serialize well and needs to be converted to a string is a Brush. For another example of how to convert a non-serializable object into a simple type of object for serialization, please see the User Definable Color Inputs page here:



    The Microsoft C# documentation about Serialization may be found at the following publicly available page:

    This article provides information about .NET serialization technologies, including binary serialization, XML and SOAP serialization, and JSON serialization.



    Please let us know if we may be of further assistance.

    Comment


      #3
      NinjaTrader_Emily You are right, the TimeSpan does not inherit System.Runtime.Serialization.ISerializable as DateTime does.
      Is there another way to expose the timespan of a time value of some sort that can be serialized? I am not married to using TimeSpan, but making it a string will prevent NT8 from doing optimization on it.

      Comment


        #4
        Hello DieSlower,

        Thank you for your response.

        The platform would still be able to run an optimization on your strategy with the TimeSpan, however, the TimeSpan would not be incremented. If you would like to optimize the period of time, you could use int input values and ToTime(Time[0]) to check if the bar is within the hour and minutes selected. There has been other discussion on this topic in our forums, including the following post which includes a sample reference file:

        Hi guys, how can I optimize a time period? I know how to optimize Stop and Take Profit sizes in the Strategy Analyzer Optimization (Variables), but I want to have the time period (it doesn't matter which Data Series or symbol I use) which is the most profitable with entry time and exit time. Example: I want to make an entry


        I appreciate your patience. Please let us know if we may be of further assistance.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        633 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        364 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        105 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        567 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        568 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X