Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Help Serializing multiple properties

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

    Help Serializing multiple properties

    I have a strategy that uses 2 TimeSpan properties. I was having a problem where setting a template would load 0's instead of the time. In doing a search I found that TimeSpan objects need to be serialized programmatically. I have created a class variable to hold the time value, turned the properties into strings and added 2 methods with the property names and "Serialize" after. Now when I save a template and load it, the start time TimeSpan loads find, but the end time TimeSpan still loads as 0. Do I have to do something special to support multiple manual serializable properties?


    Here are my property definitions:

    [NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Description = "Range Start Time", Order = 1, GroupName = "Range Settings")]
    public TimeSpan pRangeStartTime
    {
    get { return cRangeStartTime; }
    set { cRangeStartTime = value; }
    }

    [NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Description = "Range End Time", Order = 2, GroupName = "Range Settings")]
    public TimeSpan pRangeEndTime
    {
    get { return cRangeEndTime; }
    set { cRangeEndTime = value; }
    }​


    Here is my serializing methods:

    [Browsable(false)]
    public string pRangeStartTimeSerialize
    {
    get { return cRangeStartTime.ToString(); }
    set { cRangeStartTime = TimeSpan.Parse(value); }
    }

    [Browsable(false)]
    public string pRangeEndTimeSerialize
    {
    get { return cRangeEndTime.ToString();}
    set { cRangeEndTime = TimeSpan.Parse(value); }
    }​



    My 2 class variables:

    private TimeSpan cRangeStartTime;
    private TimeSpan cRangeEndTime;​


    And my values from state.default:

    //TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds)
    cRangeStartTime = new TimeSpan(0, 9, 15, 0, 0);

    cRangeEndTime = new TimeSpan(0, 09, 30, 0, 0);​





    #2
    Hello WonderMellon,

    Thanks for your post.

    You are correct, TimeSpan's and most other more complex Types would need to be put into a string to be serialized and that is able to be re-parsed by that types Parse method.

    What version of NinjaTrader are you using? Please provide the entire version number. This can be found under Help -> About (Example: 8.?.?.?)

    Do you receive an error on screen? Are there errors on the Log tab of the Control Center? If so, what do these errors report?

    Do you have the XmlIgnore attribute applied to your TimeSpan properties?

    An example of serializing a TimeSpan could be seen in the second section of sample code on the help guide documentation below.



    Please let us know if we may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      I am using version: 8.1.2.1 64-bit

      Here is an example of a strategy with only 2 timespans. And the saved template XML. I can see the values in the XML, but they will not get set when I try to load the template.

      I am willing to admit I have done something wrong in the code, but I can't figure out what that might be.

      Note: Forum does not allow file extensions of XML, so I had to rename it txt

      SerializeTest.cs loadtestxml.txt
      Attached Files

      Comment


        #4
        Hello WonderMellon,

        Thanks for your notes.

        When testing the SerializeTest script you shared, I am seeing the strategy template load the correct values for pRangeStartTime and pRangeEndTime.

        See the demonstration video below showing a strategy template is saved for the SerializeTest strategy and when we load the strategy template, the correct values appear.

        Demonstration video: https://brandonh-ninjatrader.tinytak...M18yMzAwMTcyMg

        Please test saving a strategy template for the SerializeTest strategy script you shared using the same steps seen in the demonstration video. Do you see the pRangeStartTime and pRangeEndTime values populate from the saved strategy template?
        Brandon H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by mishhh, 05-25-2010, 08:54 AM
        19 responses
        6,189 views
        0 likes
        Last Post rene69851  
        Started by gwenael, Today, 09:29 AM
        0 responses
        3 views
        0 likes
        Last Post gwenael
        by gwenael
         
        Started by Karado58, 11-26-2012, 02:57 PM
        8 responses
        14,829 views
        0 likes
        Last Post Option Whisperer  
        Started by Option Whisperer, Today, 09:05 AM
        0 responses
        2 views
        0 likes
        Last Post Option Whisperer  
        Started by cre8able, Yesterday, 01:16 PM
        3 responses
        11 views
        0 likes
        Last Post cre8able  
        Working...
        X