Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

There was an error reflecting type... Using Cbi.Instrument property

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

    There was an error reflecting type... Using Cbi.Instrument property

    EDIT: I have solved this issue. This method enables the use of Instrument picker / selector in the UI. Please see the 2nd post for a solution. I would still be interested in any useful comments. Thank you.

    Hi there,

    this snippet of code below is not allowing me to save the indicator when closing the workspace. I've had no problem in prior versions of NT8. I've read elsewhere it's due to XML serialization/deserialization issues ...... perhaps a null Instrument object?

    The full error I am getting is:

    Could not save indicator 'MyTwoDataSeriesIndicatorWithPickers:' There was an error reflecting type 'NinjaTrader.NinjaScript.Indicators.MyTwoDataSerie sIndicatorWithPickers'.

    PHP Code:
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
     public class MyTwoDataSeriesIndicatorWithPickers : Indicator
     {
      protected override void OnStateChange()
      {
       if (State == State.SetDefaults)
       {  
        Pair1        = Instrument.GetInstrument("EURUSD");
        Pair2        = Instrument.GetInstrument("AUDUSD");
       }
       else if (State == State.Configure)
       {  
        if (Bars != null)
        {
         AddDataSeries(Pair1.FullName, Bars.BarsType.BarsPeriod.BarsPeriodType, Bars.BarsType.BarsPeriod.Value, MarketDataType.Last);
         AddDataSeries(Pair2.FullName, Bars.BarsType.BarsPeriod.BarsPeriodType, Bars.BarsType.BarsPeriod.Value, MarketDataType.Last);
        }
       }
      }
    
      [NinjaScriptProperty]
      [Display(Name = "Pair 1", Order = 0, GroupName = "1. Instruments")]
      public Cbi.Instrument Pair1 { get; set; }
    
      [NinjaScriptProperty]
      [Display(Name = "Pair 2", Order = 1, GroupName = "1. Instruments")]
      public Cbi.Instrument Pair2 { get; set; }
    
     }
    } 
    
    Last edited by Sim22; 10-08-2018, 02:17 AM.

    #2
    Edit:

    Here is my fix. Rather than delete this post I think it would be useful for anyone else that has this issue.

    PHP Code:
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
     public class MyTwoDataSeriesIndicatorWithPickersFixed : Indicator
     {
      protected override void OnStateChange()
      {
       if (State == State.SetDefaults)
       {
        //--> Was this....
        //Pair1        = Instrument.GetInstrument("EURUSD");
        //Pair2        = Instrument.GetInstrument("AUDUSD");
    
        //--> Now this.... Set defaults as a string representation of the instrument.
        Pair1FullNameStr = "EURUSD";
                    Pair2FullNameStr = "AUDUSD";
    
       }
       else if (State == State.Configure)
       {  
        if (Bars != null)
        {
         AddDataSeries(Pair1.FullName, Bars.BarsType.BarsPeriod.BarsPeriodType, Bars.BarsType.BarsPeriod.Value, MarketDataType.Last);
         AddDataSeries(Pair2.FullName, Bars.BarsType.BarsPeriod.BarsPeriodType, Bars.BarsType.BarsPeriod.Value, MarketDataType.Last);
        }
       }
      }
    
    
      [Display(Name = "Pair 1", Order = 0, GroupName = "1. Instruments")]
      public Cbi.Instrument Pair1 { get; set; }
    
      [NinjaScriptProperty]
            [Browsable(false)] //--> Hide Pair1FullNameStr in the UI defaults window.
            public string Pair1FullNameStr
            {
                get { return Pair1.FullName; } //--> Pair1FullNameStr = Pair1.FullName. 'Save' this to the workspace XML file.
                set { Pair1 = Pair1.GetInstrument(value); } //--> Pair1 = Pair1.GetInstrument(Pair1FullNameStr). Loads the 'Saved' Pair1FullNameStr into the Instrument picker.
            }
    
      [Display(Name = "Pair 2", Order = 1, GroupName = "1. Instruments")]
      public Cbi.Instrument Pair2 { get; set; }
    
      [NinjaScriptProperty]
            [Browsable(false)] //--> Hide Pair2FullNameStr in the UI defaults window.
            public string Pair2FullNameStr
            {
                get { return Pair2.FullName; } //--> Pair2FullNameStr = Pair2.FullName. 'Save' this to the workspace XML file.
                set { Pair2 = Pair2.GetInstrument(value); } //--> Pair2 = Pair2.GetInstrument(Pair2FullNameStr). Loads the 'Saved' Pair2FullNameStr into the Instrument picker.
            }
    
     }
    } 
    
    Last edited by Sim22; 10-08-2018, 02:17 AM.

    Comment


      #3
      Hello Sim22,

      Thank you for your post and the resolution you found.

      Please note that one of the warnings provided for 'AddDataSeries()' in our Help Guide documentation advises that arguments supplied to AddDataSeries() should be hardcoded and not dependent on run-time variables obtained during State.Configure (such as your 'Instrument' variables).
      Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result in an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner.
      You can find this warning under the documentation at the following link: https://ninjatrader.com/support/help...dataseries.htm

      Please let me know if you have any questions.

      Comment


        #4
        Good point, thanks for the reminder

        I'm not using it in a strategy so I could add some null checking and a bool called hasAddedDataSeries...... such as:

        PHP Code:
        
        private bool hasAddedDataSeries;
        
        protected override void OnStateChange()
        {
        
         if (State == State.Configure)
         {
        
          if (Bars != null && Pair1 != null && Pair2 != null)
          {
           AddDataSeries
           (
            Pair1.FullName,
            Bars.BarsType.BarsPeriod.BarsPeriodType,
            Bars.BarsType.BarsPeriod.Value,
            MarketDataType.Last);
           AddDataSeries
           (
            Pair2.FullName,
            Bars.BarsType.BarsPeriod.BarsPeriodType,
            Bars.BarsType.BarsPeriod.Value,
            MarketDataType.Last);
        
           hasAddedDataSeries = true;
          }
         }
        }
        
        protected override void OnBarUpdate()
        {
         if (!hasAddedDataSeries)
         {
          //Insert text message here
          return;
         }
        } 
        
        Last edited by Sim22; 10-08-2018, 09:41 PM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        647 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        369 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        108 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        572 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        573 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X