Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Save/Restore BarsPeriod

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

    Save/Restore BarsPeriod

    I have an AddOn that uses an IntervalSelector similar to the AddOnFramewoek example. I have been trying to save/restore the corresponding BarsPeriod. I am using the BarsPeriod.ToXml method to save, and the FromXml method to restore. The problem I am running into is the ToXml method seems to be only saving the value and not the entire BarsPeriod. For example, if I set to 1000 volume, and save, it is saving 1000 minute in the resulting xml, and the restore restores that instead of the original.

    You can see the issue if you add the following code to the AddOnFramework example
    Code:
    //  add to: protected override void Restore(XElement element)
    
                // Restore the previously selected interval
                BarsPeriod barsPeriod = NinjaTrader.Data.BarsPeriod.FromXml(element);
    
                if (intervalSelector != null && barsPeriod != null)
                    intervalSelector.Interval = barsPeriod;
    
    
    // add to: protected override void Save(XElement element)
    
                // Save the currently selected interval
                if (BarsPeriod != null)
                    BarsPeriod.ToXml(element);
    Am I doing something wrong or is there an issue in ToXml()?

    Also, would be nice if this was added properly to the example. Thanks.
    Last edited by aslane; 06-08-2020, 09:01 AM.

    #2
    Hello aslane,

    The issue is that a BarsPeriod class is not easily converted to a string.

    Try saving the BarsPeriod.Id and BarsPeriod.Value individually instead, and then instantiate a new BarsPeriod using the saved Id and Value.

    Also, below is a link to an example of workspace persistence you may find helpful.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      So in other words, ToXml doesn't work

      Comment


        #4
        Hello aslane,

        ToXml() works for anything easily converted to a string.

        This is the same for inputs in an indicator or strategy that need serializing. (NinjaTrader uses the ToXml() on NinjaScript properties that are easily converted to strings. Things that are not easily converted to strings have to be converted to a string first, before it can be saved.)

        https://ninjatrader.com/support/help...definedbrushes
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Ok, I understand now. I thought this was a specific method of BarsPeriod. I will implement my own. Thanks.

          Comment


            #6
            Just for others, you can save/restore by doing some quick casting. You can use something like the following to save:
            Code:
                    private void SaveBarPeriod(XElement element)
                    {
                        if (BarsPeriod == null)
                            return;
            
                        XElement bpElement = new XElement("BarsPeriod");
            
                        bpElement.Add(new XElement("BaseBarsPeriodType") { Value = ((int)BarsPeriod.BaseBarsPeriodType).ToString() });
                        bpElement.Add(new XElement("BaseBarsPeriodValue") { Value = BarsPeriod.BaseBarsPeriodValue.ToString() });
                        bpElement.Add(new XElement("BarsPeriodType") { Value = ((int)BarsPeriod.BarsPeriodType).ToString() });
                        bpElement.Add(new XElement("MarketDataType") { Value = ((int)BarsPeriod.MarketDataType).ToString() });
                        bpElement.Add(new XElement("PointAndFigurePriceType") { Value = ((int)BarsPeriod.PointAndFigurePriceType).ToString() });
                        bpElement.Add(new XElement("ReversalType") { Value = ((int)BarsPeriod.ReversalType).ToString() });
                        bpElement.Add(new XElement("Value") { Value = BarsPeriod.Value.ToString() });
                        bpElement.Add(new XElement("Value2") { Value = BarsPeriod.Value2.ToString() });
            
                        element.Add(bpElement);
                    }
            You can restore with something like:
            Code:
                    private void RestoreBarPeriod(XElement topElement)
                    {
                        try
                        {
                            if (BarsPeriod == null)
                                return;
            
                            XElement bpElement = topElement.Element("BarsPeriod");
            
                            if (bpElement == null)
                                return;
            
                            XElement element;
            
                            BarsPeriod barsPeriod = new BarsPeriod();
            
                            element = bpElement.Element("BarsPeriodType");
                            if (element != null)
                                barsPeriod.BarsPeriodType = (BarsPeriodType)int.Parse(element.Value);
                            else
                                return;    // no period type not ok
            
                            element = bpElement.Element("BaseBarsPeriodType");
                            if (element != null)
                                barsPeriod.BaseBarsPeriodType = (BarsPeriodType)int.Parse(element.Value);
            
                            element = bpElement.Element("BaseBarsPeriodValue");
                            if (element != null)
                                barsPeriod.BaseBarsPeriodValue = int.Parse(element.Value);
            
                            element = bpElement.Element("MarketDataType");
                            if (element != null)
                                barsPeriod.MarketDataType = (MarketDataType)int.Parse(element.Value);
            
                            element = bpElement.Element("PointAndFigurePriceType");
                            if (element != null)
                                barsPeriod.PointAndFigurePriceType = (PointAndFigurePriceType)int.Parse(element.Value);
            
                            element = bpElement.Element("ReversalType");
                            if (element != null)
                                barsPeriod.ReversalType = (ReversalType)int.Parse(element.Value);
            
                            element = bpElement.Element("Value");
                            if (element != null)
                                barsPeriod.Value = int.Parse(element.Value);
            
                            element = bpElement.Element("Value2");
                            if (element != null)
                                barsPeriod.Value2 = int.Parse(element.Value);
            
                            if (intervalSelector != null)
                                intervalSelector.Interval = barsPeriod;            
                        }
                        catch (Exception ex)
                        {
                            // LogError("Problem restoring BarsPeriod, ex= "+ex);
                        }
                    }
            There are some assumptions in above code (geared toward the AddOnFramework example) and there may be better ways to do, but wanted to provide for anyone looking for a start.

            Comment


              #7
              Hello aslane,

              Thank you for posting your solution.

              The community may find this very helpful.
              Chelsea B.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              558 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              324 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              101 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              545 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              547 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X