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

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.FullNameBars.BarsType.BarsPeriod.BarsPeriodTypeBars.BarsType.BarsPeriod.ValueMarketDataType.Last);
         
    AddDataSeries(Pair2.FullNameBars.BarsType.BarsPeriod.BarsPeriodTypeBars.BarsType.BarsPeriod.ValueMarketDataType.Last);
        }
       }
      }

      [
    NinjaScriptProperty]
      [
    Display(Name "Pair 1"Order 0GroupName "1. Instruments")]
      public 
    Cbi.Instrument Pair1 getset; }

      [
    NinjaScriptProperty]
      [
    Display(Name "Pair 2"Order 1GroupName "1. Instruments")]
      public 
    Cbi.Instrument Pair2 getset; }

     }

    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.FullNameBars.BarsType.BarsPeriod.BarsPeriodTypeBars.BarsType.BarsPeriod.ValueMarketDataType.Last);
         
    AddDataSeries(Pair2.FullNameBars.BarsType.BarsPeriod.BarsPeriodTypeBars.BarsType.BarsPeriod.ValueMarketDataType.Last);
        }
       }
      }


      [
    Display(Name "Pair 1"Order 0GroupName "1. Instruments")]
      public 
    Cbi.Instrument Pair1 getset; }

      [
    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 1GroupName "1. Instruments")]
      public 
    Cbi.Instrument Pair2 getset; }

      [
    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 cre8able, 02-11-2023, 05:43 PM
        3 responses
        235 views
        0 likes
        Last Post rhubear
        by rhubear
         
        Started by frslvr, 04-11-2024, 07:26 AM
        8 responses
        113 views
        1 like
        Last Post NinjaTrader_BrandonH  
        Started by stafe, 04-15-2024, 08:34 PM
        10 responses
        44 views
        0 likes
        Last Post stafe
        by stafe
         
        Started by rocketman7, Today, 09:41 AM
        3 responses
        11 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by traderqz, Today, 09:44 AM
        2 responses
        10 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Working...
        X