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

Error Reflecting Type

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

    Error Reflecting Type

    I have a custom indicator I created that plots correctly as follows:

    Click image for larger version

Name:	Q01A.png
Views:	752
Size:	15.2 KB
ID:	1048967

    To save charting space and to reduce the cluster in my charts, I have combined a few indicators into this one. The code is 961 lines long and contains liner regression, T3, VMA, woodies CCI, Inverse Fisher RSI, STC, QuotientTransform and a few others. Since these indicators each have different scales, I did the following to overcome scaling concerns:

    All values of indicators are first calculated and stored in Series<double> data series variables. I have created separate functions for each indicator to build these Series<double>, private, class wide variables. These functions are called in the OnBarUpdate. The created Series<double> variable is then passed through a scale function and stored in the appropriate plot values data series as in the example below:


    Values[0] = jelScaleLastValue(dsSTC, 1.0, -1.0);
    The scaling function is as follows

    private double jelScaleLastValue(ISeries<double> iDouble, double vMax, double vMin)
    {
    double retValue;
    double lastValue = iDouble[0];
    int sCount = iDouble.Count;
    double sMax = MAX(iDouble, sCount)[0];
    double sMin = MIN(iDouble, sCount)[0];
    double num = lastValue - sMin;
    double num2 = sMax - sMin;
    retValue = (vMax - vMin) * (num/num2) + vMin;

    return retValue;
    }
    When I make an attempt to save a chart template containing this indicator, or save a workspace that has a chart with this indicator, I receive the following message:

    Click image for larger version

Name:	Q01B.png
Views:	810
Size:	18.1 KB
ID:	1048968

    The code compiles without error. When I close the workspace and reopen it, charts that contained the indicator do not have the indicator. When I apply the chart template that indicator is not applied to the chart.

    I have deleted the chart template and recreated it. I have deleted the workspace and built a new one with new charts. I have also even created a new indicator then paste my code within the class, compiled it successfully. None of these attempts to solve the problem have helped.

    The indicator plots fine, but to use it in its current state, I have to add it to each chart every time I open the workspace.

    Any insight on what may be causing this error?
    Attached Files

    #2
    Hello jeliner,

    The error you have presented would generally happen if the public properties or structure of the script cannot be serialized. It sounds like this is the case based on the details you provided. The private method specifically would not likely be related however the public properties or other public objects you have defined likely are.

    The easiest way to find out what is causing the problem is to quickly debug the script using the following steps.
    1. Open the script in the NinjaScript editor
    2. Comment out all the logic in OnBarUpdate so the script essentially does nothing. Ensure to comment out other areas you used logic such as OnRender.
    3. Comment out the SetDefaults for any public properties you created, you can leave the scripts default settings.
    4. Re apply the script on a chart, and check that it still fails to save.
    5. Go back to the script, comment out all public properties or anything you have specifically created with the public modifier
    6. Remove the script from where it was applied, re apply it and re test.

    Once you find the point which toggles the error on and off, we can take a look at that code to find out why that's happening. You may need to use the above steps to gradually uncomment parts of the file to find the breaking point.

    The solution is very likely going to be appending the [XmlIgnore] attribute to the property causing the problem, however, if the problem is not a property we would need to review what code is actually generating the error and go from there.



    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      The solution is very likely going to be appending the [XmlIgnore] attribute to the property causing the problem, however, if the problem is not a property we would need to review what code is actually generating the error and go from there.
      I found it. One of my Series<double> data sets was declared as public and not private. I changed it to private and everything is working fine. So considering what I discovered, dose NT 8 limit dataset exposure only to those that are plotted, such as those stored in Values[x]? Since it looks like NT 8 dose not support this approach, How would I go about exposing the real value of dsSTC instead of its scaled value stored in Values[0] without making it public? I thought making it public would accomplish that.
      Last edited by jeliner; 02-21-2019, 02:46 PM.

      Comment


        #4
        Hello jeliner,

        Thank you for your reply.

        The problem is not that the series was public, it is specifically what I noted about using the attribute [XmlIgnore] with the public property. You can have public series properties to expose the series however a series cannot be serialized.

        When you "serialize" an object (export the values to xml) everything needs to become a string which a series cannot do. This is also why there is special syntax for a Brush property. When trying to serialize something that can't be made into a string, this causes an error. The solution is just don't save the series. This is the purpose of [Attributes] you see and specifically [XmlIgnore].

        You can expose a Series<T> for other scripts to use, this is shown in most of the internal indicator such as the Bollinger indicator with its Plots. A Plot uses the Values collection which you can find an example of in the Properties region of the Bollinger indicator. You can also see it uses [XmlIgnore] to prevent the series from being saved in the workspace. Any other variable you have which is a Series<T> such as internal collection you made or private series would use a different type of public property to expose that series. you may additionally need to use Update() for other properties which is also shown in the following sample: https://ninjatrader.com/support/help...alues_that.htm


        I look forward to being of further assistance.
        Last edited by NinjaTrader_Jesse; 02-21-2019, 03:36 PM.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Aha!

          If you want a series to be publicly accessible from other scripts, you might think you need to make that series public, but that causes the error.

          Instead, when you declare the series in the variables region at the start of the script, you make that series private, but then to make that series available to other scripts, you create a property, which is public, and that property can access the internal private series. The tricky part others may miss is the capital letter on the public property name, vs the lowercased name of the private series.

          from the attached script:

          Code:
          #region Properties
          
          /*
          Creating public properties that access our internal
          private Series<bool> allows external access
          to this indicator's Series<bool>
          */
                  [Browsable(false)]
                  [XmlIgnore]
                  public Series<bool> BearIndication
                  {
                      get { return bearIndication; }    
                      /*
                      Allows our public BearIndication Series<bool> to access and expose
                      our internal bearIndication Series<bool>
                      PAY ATTENTION TO THE CASE
                      */
                  }
          Last edited by balltrader; 02-04-2020, 06:16 AM.

          Comment


            #6
            thank you balltrader. I had the same problem and used your properties solution to take care of it

            Comment


              #7
              I have a similar issue, which is not easily resolve. I have 5 plots all of them public, start with a capital, and all have [XmlIgnore]. The indicator preform well, but when trying to save a workspace with the indicator, I get an error:
              Could not save indicator 'MyIndicator:' There was an error reflecting type 'NinjaTrader.NinjaScript.Indicators.MyIndicator'

              Here is the property section code:

              Code:
              #region #### Properties ####
              
              #region ##<< Input Params >>##
              
              #region #< Strucutre Setup Params >#
              [Range(1, int.MaxValue), NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "Swing Strength", Description = "Strength of the Swings. Highier values will reduce noise", GroupName = "Setup", Order = 2)]
              public int SwingStrength { get; set; }
              
              [Range(1, int.MaxValue), NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "ATR Period", Description = "the lookup period for the ATR bases distances", GroupName = "Setup", Order = 4)]
              public int ATRPeriod { get; set; }
              
              [NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "Use High/Low Swings", Description = "Base the Swings on the High/Low values. Otherwise the Open and Close are used", GroupName = "Setup", Order = 6)]
              public bool UseHighLowSwings { get; set; }
              
              [NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "Calculate Divergance", Description = "Calculate Divergance", GroupName = "Setup", Order = 6)]
              public bool CalculateDivergance { get; set; }
              #endregion
              
              #region #< Divergence Setup Params >#
              [NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "Function type", Description = "the lookup period for the ATR bases distances", GroupName = "Divergence Setup", Order = 2)]
              public DivFunctionType DivHistogramFunction { get; set; }
              
              [Range(1, int.MaxValue), NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "Fast Period", Description = "Fast Period", GroupName = "Divergence Setup", Order = 4)]
              public int DivFastPeriod { get; set; }
              
              [Range(1, int.MaxValue), NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "Slow Period", Description = "Slow Period", GroupName = "Divergence Setup", Order = 6)]
              public int DivSlowPeriod { get; set; }
              
              [Range(1, int.MaxValue), NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "Smoth Period", Description = "Smoth Period", GroupName = "Divergence Setup", Order = 8)]
              public int DivSmothPeriod { get; set; }
              #endregion
              
              #region #< Setup Visual Params >#
              [RefreshProperties(RefreshProperties.All)] // Needed to refresh the property grid when the value changes
              [Display(ResourceType = typeof(Custom.Resource), Name = "Show Swing Legs", Description = "Display the Swing Structure", GroupName = "Setup Visual", Order = 2)]
              public bool ShowSwingLegs { get; set; }
              
              [RefreshProperties(RefreshProperties.All)] // Needed to refresh the property grid when the value changes
              [Display(ResourceType = typeof(Custom.Resource), Name = "Show Swing Labels", Description = "Display the Swing Labels", GroupName = "Setup Visual", Order = 3)]
              public bool ShowSwingLabels { get; set; }
              
              [Display(ResourceType = typeof(Custom.Resource), Name = "Swings Up Line", Description = "The Swings Leg UP stroke (color,shape,color)", GroupName = "Setup Visual", Order = 4)]
              public Stroke SwingsUpStroke { get; set; }
              
              [Display(ResourceType = typeof(Custom.Resource), Name = "Swings Down Line", Description = "The Swings Leg DONE stroke (color,shape,color)", GroupName = "Setup Visual", Order = 6)]
              public Stroke SwingsDownStroke { get; set; }
              
              [Display(ResourceType = typeof(Custom.Resource), Name = "Divergance Line", Description = "The Divergance Line stroke (color,shape,color)", GroupName = "Setup Visual", Order = 8)]
              public Stroke DiverganceStroke { get; set; }
              #endregion
              
              #endregion
              
              #region ##<< Output Params >>##
              
              #if DEBUG
              [Browsable(false)][XmlIgnore] public Series<double> CurBarNum { get { return Values[plotCurBar]; } }
              #endif
              [Browsable(false)][XmlIgnore] public Series<double> SwingPoints { get { return Values[plotSwings]; } }
              [Browsable(false)][XmlIgnore] public Series<double> SwingLen { get { return Values[plotSwingLen]; } }
              [Browsable(false)][XmlIgnore] public Series<double> Histogram { get { return Values[plotHistogram]; } }
              [Browsable(false)][XmlIgnore] public Series<double> Divergance { get { return Values[plotDivergance]; } }
              #endregion
              
              #endregion
              Here is the plot sections in OnStateChange:

              Code:
               base.OnStateChange();
              switch (State)
              {
              #region State.SetDefault
              case State.SetDefaults:
              Description = @"BWMarketStructure";
              Name = "BWMarketStructure";
              Calculate = Calculate.OnBarClose;
              
              // Setup Params
              SwingStrength = 4;
              ATRPeriod = 28;
              UseHighLowSwings = true;
              // Divergance Setup
              DivHistogramFunction = DivFunctionType.RSI;
              DivFastPeriod = 12;
              DivSlowPeriod = 26;
              DivSmothPeriod = 9;
              // Setup Visual
              ShowSwingLegs = true;
              ShowSwingLabels = true;
              SwingsUpStroke = new Stroke(Brushes.RoyalBlue,DashStyleHelper.Solid,2);
              SwingsDownStroke = new Stroke(Brushes.Red,DashStyleHelper.Solid,2);
              DiverganceStroke = new Stroke(Brushes.Aqua,DashStyleHelper.Solid,2);
              // NT8 Standard
              DrawOnPricePanel = true;
              IsOverlay = true;
              DisplayInDataBox = true;
              IsSuspendedWhileInactive = true;
              PaintPriceMarkers = false;
              MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
              // Plots
              ShowTransparentPlotsInDataBox = true;
              #if DEBUG
              AddPlot(Brushes.Transparent, "CurBarNum"); plotCurBar = 0;
              #else
              plotCurBar = -1; // initialize if not DEBUG
              #endif
              
              AddPlot(Brushes.Transparent, "SwingPoints"); plotSwings = plotCurBar+1;
              AddPlot(Brushes.Transparent, "SwingLen"); plotSwingLen = plotSwings+1;
              AddPlot(Brushes.Transparent, "Histogram"); plotHistogram = plotSwingLen+1;
              AddPlot(Brushes.Transparent, "Divergance"); plotDivergance = plotHistogram+1;
              plotLastPlot = plotDivergance;
              
              break;
              #endregion
              Last edited by Shai Samuel; 06-28-2022, 06:38 AM.

              Comment


                #8
                Hello Shai Samuel,

                I don't see from the code what that may be. You can debug this by commenting out all of your OnBarUpdate/OnStateChange custom code and then commenting out the public properties. Make the script basically just a empty script like when you generate a new file, then test to make sure the error is gone. Then uncomment 1 property and its default at a time and repeat until you hit the error again.

                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Thank you Jesse for your kind replay.

                  I have solved all my indicators issues. They were of 2 nature:
                  1. One of the properties, didn't have a default value set in OnStateChange State.SetDefault
                  2. A few others, input properties had [XmlIgnore]. Once deleted error stopped.
                  ​​​​​​​I hope this could help others.
                  Last edited by Shai Samuel; 06-29-2022, 01:45 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