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

Serialization of public variables in Properties Section

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

    Serialization of public variables in Properties Section

    Hello,

    According to the NinjaScript Reference, https://ninjatrader.com/support/help...attributes.htm, if Browsable is set to false, it will not appear on UI and omitting XmlIgnore allows serialization.

    Code:
    #region Properties   
    
    [Browsable(false)]    // do not show this value on the UI's property grid
    public bool MyBool  
    { get; set; }
    
    [XmlIgnore]    // removes from serialization
    public bool MyOtherBool
    { get; set; }
    
    #endregion
    "As a best practice as well, your NinjaScript should not have any public fields, since those would get serialized as well - which means their state would be persisted, which in turn could lead to unexpected outcomes."

    My question then is how does one persist a variable and hide from the UI property grid at the same time? I just want some bool flags to persist through a Reload NinjaScript or chart aggregation change, IE going from 30min to 5min.

    Thanks.

    Tom
    Intelligent Trader Inc.
    NinjaTrader Ecosystem Vendor - Artificial Intelligence Based Indicators

    #2
    Hello intelligenttrader,

    You would make a public property for your use case, the guide just notes to avoid that so you don't have values stored unexpectedly. If you want to save values you can ignore the note.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Can you provide a reference link or example? I searched the forum and did not locate one.

      Thank you.

      Tom
      Intelligent Trader Inc.
      NinjaTrader Ecosystem Vendor - Artificial Intelligence Based Indicators

      Comment


        #4
        Hello intelligenttrader,

        I am not certain I understand what you are asking for a sample of. The public property you provided would be what you are asking about or a Browsable False public property. If you want to save a value you would leave off the [XmlIgnore]

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Jesse,

          In my original post my question was how does one persist (save) a variable and hide it from the UI property grid at the same time? I just want some bool flags to persist through a Reload NinjaScript or chart aggregation change, IE going from 30min to 5min. I can either hide from UI and not have it save or have it save and its on the UI grid. The example I showed was just referencing both Attributes. The below code should make it hidden from UI and persistent but it doesn't. Am I missing something here?
          The default behavior will attempt to serialize all public properties
          [XmlIgnore] is omitted completely as I want it serialized. Do you have a script that will change a public bool and have it persist and be hidden from the UI grid? I can not get that functionality to work. I have included a video of serialization not working when Browsable(false). It can be watched here. I am using an int in this example.



          Code:
          #region Properties
          
          [Browsable(false)]  // do not show this value on the UI's property grid
          public bool MyBool { get; set; }
          
          #endregion
          Tom
          Last edited by intelligenttrader; 10-14-2021, 09:00 PM.
          Intelligent Trader Inc.
          NinjaTrader Ecosystem Vendor - Artificial Intelligence Based Indicators

          Comment


            #6
            Hello intelligenttrader,

            In my original post my question was how does one persist (save) a variable and hide it from the UI property grid at the same time?
            Yes you posted a sample of that in post 1, the MyBool.

            I just want some bool flags to persist through a Reload NinjaScript or chart aggregation change
            The public property would be persisted through changes like reloads or changing timeframes. If you are setting MyBool in your runtime logic, for example in OnBarUpdate then that would not be persisted. The value set from State.Defaults would persist for that property because it is browsable(false) it would never be set by the user so it would just have a default value.

            Are you setting the value of MyBool later, for example in OnBarUpdate?

            I look forward to being of further assistance.

            JesseNinjaTrader Customer Service

            Comment


              #7
              Jesse,

              Lets forget about the bool right now and clearly the int is not working either? Were you able to watch the video?

              Tom
              Intelligent Trader Inc.
              NinjaTrader Ecosystem Vendor - Artificial Intelligence Based Indicators

              Comment


                #8
                Hello intelligenttrader,

                If the goal is to save a runtime value, a value you set from your logic then you would need to do that yourself when the script has its State.Terminated run. You could write to a file as one example.

                The public properties are only going to be saved during template/workspace saving and they will also persist between reloads etc but that will only be for the value you set in State.SetDefaults. As you have it browsable false there would be no other value to be set besides the default value you defined.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Thank you Jesse for following up. If you watch the video I posted you will see serialization will happen on values set during runtime logic only if Browsable is set to true. The runtime value of the variable in State.Defaults will be serialized and used during reload or aggregation changes as well workspace saving. Using Browsable as true kind of defeats the purpose of hiding logic from the user. According to the NinjaScript reference the Browsable attribute has nothing to do with serialization. Whereas the reference to the XmlIgnore attribute handles serialization of public properties. Also there is a typo in the XmlIgnore syntax as it shows an overload with a bool. I have attached an image show of compile failure. Thanks for your assistance and I will pursue the use of a config file to handle our needs.
                  .
                  Click image for larger version

Name:	xmlignore_error.jpg
Views:	405
Size:	57.6 KB
ID:	1175043
                  Intelligent Trader Inc.
                  NinjaTrader Ecosystem Vendor - Artificial Intelligence Based Indicators

                  Comment


                    #10
                    Hello intelligenttrader,

                    The error is caused by using (true), XmlIgnore does not take any parameters.


                    Please let me know if I may be of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      Hello Jesse,
                      On this topic of persistent variables, I am also having problem with NON-INPUT variable persistence. Attached is an indicator (test6.zip) to show the problem.
                      In this indicator, there are two related buttons shown on the right side of the chart - "Inc.Var" for incrementing the contents of the variables for testing purpose and "Show" to display the contents of the variables.

                      The testing can be performed as follows:
                      1. Start NT, on the Connections tab of the main menu, disconnect any data feed and load the indicator "test6".
                      2. On the chart, press "Show" button to show the variable values.
                      3. On the chart, press "Inc.Var" button to increase the variable values so that they are not the same as the default values.
                      4. Via the indicator input, enter a value for "Input variable", say "-95".
                      5. Exit NT, saving the workspace.
                      6. restart NT without any data feed connection.
                      7. On the chart, press the "Show" button - at this point, all is well. The contents of all three variables are retained.
                      8. Now, on the NT main menu, on the Connection tab, enable the "Kinetick - End of Day (Free).
                      9. On the chart, press the "Show" button, you will see that only dValue_input data is retained. The contents of the other two NON-INPUT variables have been reset to the default values. This happens whether if the variables are set or not in the State.SetDefaults section.
                      Attached Files

                      Comment


                        #12
                        Hello kkc2015,

                        Non input variables will not be persisted, the only items that are saved are public user input properties.

                        If you need to save other data which is not an input you would have to do your own saving/loading to file for those values.

                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          Hello Jesse,
                          Thank you for the quick response. After looking through the workspace xml file (attached test.zip), I found that the contents of the three public variables dValue_input, ibar_persist, dValue_persist were written to the workspace xml files as below.

                          <ZOrder>10004</ZOrder>
                          <dValue_input>-96.8</dValue_input>
                          <ibar_persist>3</ibar_persist>
                          <dValue_persist>4.5</dValue_persist>

                          Thus, as per Ninja documentation, contents of non-input variables of simple type can be saved. After NT has been loaded completely, it was the subsequent enabling of the data feed connection that reset the values of the non-input variables. By setting the data feed Kinetick - End of Day (Free) to "Connect at start up", the values of the non-input variables are retained without any problems.

                          Summarizing:
                          1. When NT is loaded without data feed connection, the contents of the non-input variables are retained. If data feed is subsequently enabled, it reset the contents of the non-input variables.
                          2. If data feed connection is enabled to "Connect at start up", the contents of the non-input variables are retained as intended.
                          To retain the contents of non-input variables, the workaround is to enable the data feed connection at start up. Is this by program design?
                          Attached Files

                          Comment


                            #14
                            Hello kkc2015,

                            I would suggest to make a more simple test that demonstrates the problem using a Print instead of the user interface items, I won't be able to debug the code in the test you provided as it is.

                            Do you still see the problem if you use a new empty indicator and then create only the properties and print their value from OnBarUpdate?

                            You can view the following help guide article which demonstrates saving a Brush property which uses a hidden public string property for the purpose of saving to the workspace, that would also make a good test or to and use the same format that is shown in that sample.




                            One possible problem with your Button_Action event is that you are not using a TriggerCustomEvent inside the Button_Action. Any NinjaScript based properties or series won't be updated inside that method when the button is pressed. Removing that Button_Action event will help the debugging process to make sure you are not hitting any UI specific logical problems.

                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              Hello Jesse,
                              Thank you for your suggestion. No further action is required on my inquiry.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by martin70, 03-24-2023, 04:58 AM
                              14 responses
                              105 views
                              0 likes
                              Last Post martin70  
                              Started by TraderBCL, Today, 04:38 AM
                              1 response
                              4 views
                              0 likes
                              Last Post bltdavid  
                              Started by Radano, 06-10-2021, 01:40 AM
                              19 responses
                              606 views
                              0 likes
                              Last Post Radano
                              by Radano
                               
                              Started by KenneGaray, Today, 03:48 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post KenneGaray  
                              Started by thanajo, 05-04-2021, 02:11 AM
                              4 responses
                              470 views
                              0 likes
                              Last Post tradingnasdaqprueba  
                              Working...
                              X