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

declarative property tags

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

    declarative property tags

    do I have this right?

    [NinjaScriptProperty] use if you want the property included in the calling object (Warning: Only types which can be Xml Serialized should be marked as a NinjaScriptAttribute

    [xmlignore] use if you want to save property to a workspace or template

    [Browsable(false)] use if you want property in NinjaTrader UI's property grid

    still a bit confused about when I need to serialsize?

    and this

    Warning: Only types which can be Xml Serialized should be marked as a NinjaScriptAttribute, otherwise you may run into errors when persisting values in various scenarios (e.g., saving workspace, or running Strategy Optimizations). Should you have a property you wish to use as user defined input, you will need to implement a secondary simple type (such as an int or string) as the value to be serialized as user input. Please see the example below which demonstrates using a simple type as the NinjaScriptProperty against types which cannot be serialized

    in help guide

    #2
    Hello fingers,

    By 'declarative property tags' are you referring to attributes?


    NinjaScriptProperty makes the variable a parameter for method call and optimizing.
    XmlIgnore prevents the variable from being saved with Xml (workspaces and templates).
    Browsable determines if the variable will show in the UI


    An xml file stores information as text. This file type is a specially formatted file that uses markup language tags to organize the text in storage. For values used in an application that are not text, have to be converted from the original object type to text that can be saved in the text files, and then restored from text back to the original object. This process is known as serialization.

    NinjaTrader automatically attempts to save all variables using the ‘public’ access modifier to xml workspaces and templates. This saves the values the user has set for the inputs in the Indicator or Strategy window when restarting NinjaTrader, closing and opening workspaces, and when applying templates.

    NinjaTrader auto-converts, to text, simple object types and some NinjaScript specific object types which are automatically serialized by NinjaTrader. Object types that do not easily convert into text strings cannot be saved in the xml file as text, causing an error.

    Data types that do not need serialization
    • string
    • bool
    • int, double, float (or number type)
    • enum
    • SimpleFont
    • Stroke
    If the object type is in the list above, applying the XmlIgnore attribute and serializing is not required.

    Public variables of all other data types will need the [XmlIgnore] attribute applied to prevent the variable from being included from being saved in the xml file, resulting in an error.
    Help guide: NinjaScript > Language Reference > Common > Attributes > XmlIgnoreAttribute

    ERROR: Could not save indicator 'MyCustomIndicatorName:' There was an error reflecting type 'NinjaTrader.NinjaScript.Indicators.MyCustomIndica torName'

    This error appears on the Log tab of the Control Center when saving xml files, such as workspaces or templates, with a script that has public variables that are not easily converted to text and need the XmlIgnore() attribute applied. Serializing


    Some object types, such as Brushes, TimeSpans, and custom classes, can be converted to a string and then converted back to the original object type, which can be serialized into the xml file. Serializing means to convert the object into text that can be saved in a xml file, and then the text read back from the file as the workspace is opened or template applied and the value restored to the original object data type. This is done by using a secondary public string variable that can hold that converted value as text for saving in the workspace. In the ‘get’ of the secondary string variable, the object is converted to a string. Within the ‘set’ the string returned from xml is converted back to the original object.

    The help guide provides a detailed code sample on serializing Brush (color) inputs.

    Help guide: NinjaScript > Educational Resources > Tips > User Definable Color Inputs

    Help guide: NinjaScript > Educational Resources > Working with Brushes > Using brushes defined on the user interface

    Serialize a Brush code sample
    Code:
    [XmlIgnore()]
    public Brush BorderBrush
    { get; set; }
    
    [Browsable(false)]
    public string BorderBrushSerialize
    {
    get { return Serialize.BrushToString(BorderBrush); }
    set { BorderBrush = Serialize.StringToBrush(value); }
    }​
    Serialize a TimeSpan code sample
    Code:
    [XmlIgnore]
    public TimeSpan OpenTime
    { get; set; }
    
    [Browsable(false)]
    public string OpenTimeSerialize
    {
    get { return OpenTime.ToString(); }
    set { OpenTime = TimeSpan.Parse(value); }
    }​
    Serialize a DateTime code sample
    Code:
    [XmlIgnore]
    public DateTime StartDateTime
    { get; set; }
    
    [Browsable(false)]
    public string StartDateTimeSerialize
    {
    get { return StartDateTime.ToString(); }
    set { StartDateTime= DateTime.Parse(value); }
    }​
    It may be desired to make a variable public for the express purpose of accessing the object from a hosting script or window, that makes calls this indicator to return values or objects.

    Public variables that are not intended to be an input for the user to set, or are not convertible to a string and then back, cannot not be serialized with custom code. These still must have the [XmlIgnore] attribute applied to prevent errors when being saved in xml.

    Object types that cannot be serialized and restored:
    • Series<T>
    • Order
    • Execution
    • Trade
    • Position
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by RDTrader16, Today, 10:19 PM
    0 responses
    3 views
    0 likes
    Last Post RDTrader16  
    Started by gemify, 03-08-2023, 08:02 AM
    9 responses
    148 views
    0 likes
    Last Post culpepper  
    Started by elirion, Today, 10:03 PM
    0 responses
    1 view
    0 likes
    Last Post elirion
    by elirion
     
    Started by RaddiFX, Today, 09:55 PM
    0 responses
    6 views
    0 likes
    Last Post RaddiFX
    by RaddiFX
     
    Started by Trader146, 03-29-2024, 01:22 PM
    4 responses
    25 views
    0 likes
    Last Post Trader146  
    Working...
    X