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

Error with serialization of custom types

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

    Error with serialization of custom types

    Hello, I have found a problem with the serialization of custom types that seems to be a bug.

    For the test I have used the strategy that I attach and whose code is based on this indicator:


    The strategy has two custom parameters:
    • Myvalue (Custom Type)
    • Mylistvalues (Collection of Custom Type)

    1.- First I open the strategy (for example from the Strategies Tab of Control Center) and fill the data for Myvalue and Mylistvalues, and save a template.

    I can open new instances of the strategy, load that template and check that serialization and deserialization work right at this time. I can also edit the values and save new templates, and serialization/deserialization continue to work right.

    2.- Now I open the NinjaScript Editor and push the Compile button.

    3.- Return to the Strategies tab of the ControlCenter, load a new instance of the strategy and load a previously saved template: now Mylistvalues deserializes right, but Myvalue does not.

    Even if I now save/load new templates, the deserialization of Myvalue does not work, although the Mylistvalues collection does.

    4.- I have to restart NinjaTrader so that the templates will work right again.

    Maybe I'm missing something but it seems that there is a bug in the process.

    Thanks

    ​​​
    Attached Files

    #2
    Hello cls71,

    Thanks for your post.

    We have received your question regarding the serialization of custom types and are researching this matter at this time.

    Once we are finished investigating this matter we will update this forum thread with our findings.

    Thanks for your patience.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hello cls71,

      Thanks for your patience.

      It seems that you are trying to recreate the PriceLevel class the Fibonacci Tools use but you have made some differences.

      If you are trying to make a list of values, the only supported way to do that is to copy exactly what the PriceLevels.cs file has in it and change the naming of the classes and type converter so it reflects your own custom code. List<PriceLevel> could be serialized, or in your case List<MyCustomType>.

      The PriceLevels.cs file you be found by opening a New > NinjaScript Editor window, opening the DrawingTools folder, and double-click on the PriceLevels file.

      To make a list of items you essentially need to make the PriceLevel class which contains your individual data in the list and the PriceLevelTypeConverter for the property grid.

      Your main class would be similar to the PriceLevelContainer class, the drawing objects use that as a base class and you can see it inherits from DrawingTool and has a public property of the PriceLevels which contains the data. You do not need to recreate the PriceLevelContainer but you do need to use that PriceLevels.cs file as an example of how to set up the public properties and also how it uses CopyTo and creates the list instance, and so on.

      While we cannot go into that level of support, you can use the existing drawing tools as an example of how to create a list of items that can be serialized but you would need to implement it exactly as it is used in those files.​
      Brandon H.NinjaTrader Customer Service

      Comment


        #4
        Hello Brandon,

        maybe I have not explained the problem well, but I don't know how to explain it better, sorry. The code that I attached is a simplified example (using your own classes from another code your 'SampleIndicatorTypeConverter'). Basically it is all your code. And to demonstrate anomalous behavior in the load of templates (deserialization) that has to do with the deserialization of isolated CustomTypes.

        You can check the error following the steps I listed (I know it is a time that you may not have, but it will only take a few minutes to check it).

        The problem is not with a List<CustomType>. As I said in my previous post that works well, always, even between compilations.

        The problem is with the deserialization of a CustomType that is outside a collection. I have searched in the documentation and in the forum and I have not found any example of isolated CustomType property in indicator or strategy, only of CustomTypes within collections, as in the Drawingtool of Pricelevels or the indicator SampleIndicatorTypeConverter. But none about CustomTypes out of collections.

        And as I said, the serialization/deserialization process of templates works well for custom types isolated (out of collections) until compile. After compilation the deserialization (load of the template with the custom type isolated) stops working but only for isolated custom types. However, with hosted custom types in collections, work right. On the other hand, this error in deserialization does not print any error message in the Trace or Log files.

        Thanks​

        Comment


          #5
          Hello cls71,

          Thanks for your notes.

          It would not be supported to serialize a CustomType in that manner. You would need to make a secondary property that makes it into a .ToString() that can be saved, and the same for deserialization. It would need to parse the string and create a new object. This would be a similar concept as Brush serialization.

          Using Brushes Defined on the UI Interface: https://ninjatrader.com/support/help...th_brushes.htm

          You will have to come up with your own custom logic based on what is in your custom type to accomplish this.. A simple example would be a class that has a public string property, during the serialization you need to read the instance of the class and collect the value of the string and make a string that contains that string. During deserialization you can parse that string and create a new instance of the class, then assign that string to the public properties in the class.

          if what you are doing in the custom type is all serializable values like strings or stuff that does noy need any custom property for serialization, you could try using the method in the sample script linked on the help guide page below.



          Serialize a class into xml which is a string and save that string using the extra serialization property. Then for deserialization read the xml and pass it to the method to deserialize into an object and assign the object to the property. The sample on the forum thread linked above does that in OnStateChange. You would do this in the get; set; for the property instead.
          Last edited by NinjaTrader_BrandonH; 02-13-2024, 09:07 AM.
          Brandon H.NinjaTrader Customer Service

          Comment


            #6
            Hello Brandon,

            the bug is between compilations. After compiling is when deserialization stops working (loading the template). The serialization works well because the data is saved in the XML file of the template. What does not work is only deserialization. And only after compiling. If I do not compile, it always works well, I can edit the templates, save them, load them, ... keep working well. But if you compile later, it doesn't work anymore.

            And this bug only occurs for objects outside of collections. If the objects are within collections this error with deserialization does not happen and continue to work well after compiling.
            And when I restart Ninjatrader, everything works right again, and the templates that did not load the customtype data, after restarting they do.

            If the code had any error, it would never work. But it does work ... until it compiles again. The simple fact of compiling, without editing any code, causes that bug. And that is what I do not understand, unless it is an internal bug.

            To overcome the problem I already thought about including my own logic to serialize/deserialize, but as it is a process that already makes Ninjatrader through the templates, I preferred to discover the error and use the standard. In any case, is there any way of accessing the file with the template of the indicator or strategy to deserialize it myself?

            Thanks​

            Comment


              #7
              Hello cls71,

              Thanks for your notes.

              There are no documented or supported methods or properties for accessing the strategy template .xml files located in the Documents\NinjaTrader 8\templates\Strategy folder in a custom NinjaScript.

              As noted in post # 5, to serialize the CustomType you would have to write a .ToString() override in your custom class and something that could be used to parse a string and generate a new object. This would require that you come up with your own custom logic to accomplish this.

              Some items do not need custom serialization; a bool, int, double, string, SimpleFont, for example.

              These are basic types that are automatically serialized, meaning the object is converted to a string and then converted back to the original object type. Everything else must use the XmlIgnore attribute on the actual custom class type cariable and can optionally have custom serialization using a secondary string variable.

              This forum thread will be open for other community members to share their insights on the topic and possible unsupported code you could try to access a strategy template .xml file.


              Last edited by NinjaTrader_BrandonH; 02-13-2024, 09:13 AM.
              Brandon H.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by blar58, Today, 10:37 AM
              2 responses
              6 views
              0 likes
              Last Post blar58
              by blar58
               
              Started by gemify, 12-13-2022, 06:10 PM
              48 responses
              3,922 views
              5 likes
              Last Post Jthunders  
              Started by AngelGL, Today, 10:59 AM
              0 responses
              3 views
              0 likes
              Last Post AngelGL
              by AngelGL
               
              Started by philmg, 02-16-2024, 08:45 AM
              3 responses
              37 views
              0 likes
              Last Post philmg
              by philmg
               
              Started by dweems, Today, 10:42 AM
              0 responses
              8 views
              0 likes
              Last Post dweems
              by dweems
               
              Working...
              X