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:	128
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:	116
Size:	38.5 KB
ID:	1326918

    BarsPeriod serialized XML:

    Click image for larger version

Name:	2024-12-07 12_38_54-Window.png
Views:	111
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:	113
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 DannyP96, 05-18-2026, 02:38 PM
        1 response
        85 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 05-11-2026, 05:56 AM
        0 responses
        143 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 05-10-2026, 08:12 PM
        0 responses
        83 views
        0 likes
        Last Post CarlTrading  
        Started by Hwop38, 05-04-2026, 07:02 PM
        0 responses
        256 views
        0 likes
        Last Post Hwop38
        by Hwop38
         
        Started by Mindset, 04-21-2026, 06:46 AM
        0 responses
        334 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Working...
        X