Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

BarsPeriod.ToXml() not serializing all fields in BarsPeriod

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

    BarsPeriod.ToXml() not serializing all fields in BarsPeriod

    I have implemented IIntervalProvider on my custom NTTabPage and overridden Save(XElement element) and Restore(XElement element) as follows:

    Code:
    private BarsPeriod _barsPeriod;
    public BarsPeriod BarsPeriod
    {
        get => _barsPeriod;
        set
        {
            if (!EqualityComparer<BarsPeriod>.Default.Equals(_barsPeriod, value))
            {
                TheIntervalSelector.Interval = _barsPeriod = value;
    
                RefreshHeader();
                PropagateIntervalChange(value);
            }
        }
    }
    
    private void TheIntervalSelector_OnIntervalChanged(object sender, BarsPeriodEventArgs e)
    {
        BarsPeriod = e.BarsPeriod;
    }
    
    protected override void Save(XElement element)
    {
        if (element == null)
            return;
    
        // save the currently selected interval
        if (BarsPeriod != null)
        {
            var intervalElement = new XElement("Interval");
            BarsPeriod.ToXml(intervalElement); // does not serialize BarsPeriod.BarsPeriodType because the property is adorned with XmlIgnore!!!
            element.Add(intervalElement);
        }
    }
    
    protected override void Restore(XElement element)
    {
        if (element == null)
            return;
    
        // restore the previously selected interval
        XElement intervalElement = element.Element("Interval");
        if (intervalElement != null && !string.IsNullOrEmpty(intervalElement.Value))
            BarsPeriod = BarsPeriod.FromXml(intervalElement); // does not deserialize BarsPeriod.BarsPeriodType because the property is adorned with XmlIgnore!!!
    }​
    Notice the above code is using BarsPeriod.ToXml(intervalElement) to serialize the BarsPeriod provided by the IntervalSelector.

    However, because BarsPeriod.BarsPeriodType is adorned with the XmlIgnore attribute it is not serialised. This results in incorrect IntervalSelector state when the workspace is restored:

    IntervalSelector with 10 Seconds selected:

    Click image for larger version

Name:	2024-12-07 12_26_55-Window.png
Views:	117
Size:	16.1 KB
ID:	1326917

    BarsPeriod properties at the point of serialization:


    Click image for larger version

Name:	2024-12-07 12_38_32-Window.png
Views:	105
Size:	38.5 KB
ID:	1326918

    BarsPeriod serialized XML:

    Click image for larger version

Name:	2024-12-07 12_38_54-Window.png
Views:	103
Size:	53.0 KB
ID:	1326919

    Because of the above, after the workspace is restored the IntervalSelector now shows 10 Minutes selected, not 10 Seconds:

    Click image for larger version

Name:	2024-12-07 14_48_25-Window.png
Views:	100
Size:	16.0 KB
ID:	1326920

    So how are you supposed to save and restore the BarsPeriod provided by the IntervalSelector?

    #2
    Hello tcdouglas,

    I see that happening with the addon sample as well, I will need to report that to see if that is intended to if some other process of saving is required. Once I have more details on that I will reply back here.

    Comment


      #3
      Hello tcdouglas,

      It looks like that is to be expected, most controls do not automatically save their values such as the interval selector. You would have to make your own xml node and save the data/restore the data. You can find a sample of saving custom values here. https://forum.ninjatrader.com/forum/...830#post759830

      Comment


        #4
        hmm, ok, so the BarsPeriod has it's own ToXml() and FromXml() for saving/restoring it's own state, but they don't save/restore it's own state completely so they are of no use!

        As you suggest, I'll implement my own BarsPeriod persistence, but it's not as straight forward as it could be because XmlIgnore attribute is adorned on some BarsPeriod properties!

        Just an FYI: Because NTTabPage Save() and Restore() are used when duplicating a tab, the IntervalSelector's selection changes from Seconds to Minutes when duplicating a tab (as well as saving/restoring workspaces)
        Last edited by tcdouglas; 12-10-2024, 02:25 AM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by CarlTrading, 03-31-2026, 09:41 PM
        1 response
        43 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        20 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        30 views
        1 like
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        49 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        38 views
        0 likes
        Last Post CarlTrading  
        Working...
        X