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

How to correctly copy/clone property objects when the assembly version has changed

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

    How to correctly copy/clone property objects when the assembly version has changed

    Hello,
    I have a property data class that I'm using in my indicator and would like those properties to be editable directly in the indicator dialog window. I've been able to get things to "mostly" work, however I keep getting an error when the code base is rebuilt.

    Error on getting/setting property 'X' for NinjaScript 'CopyProblemDemo': Object of type 'NinjaTrader.NinjaScript.Indicators.Foo' cannot be converted to type 'NinjaTrader.NinjaScript.Indicators.Foo'.

    My best guess is there is an assembly version mismatch happening here, but I'm not sure how to fix it.

    Does anyone know the correct approach here?

    Thank you,
    Mark

    -------------------------------------------------------
    To Reproduce:
    1) Compile CopyProblemDemo indicator code
    2) add CopyProblemDemo indicator to a chart
    3) Compile the indicator again
    4) on the chart with the indicator, press f-5 to refresh


    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
    [TypeConverter("NinjaTrader.NinjaScript.Indicators. FooTypeConverter")]
    public class Foo : ICloneable
    {
    public int A {set;get;}
    public int B {set;get;}
    
    public object Clone()
    {
    Foo x = new Foo();
    CopyTo(x);
    return x;
    }
    public void CopyTo(Foo other)
    {
    if(other==null){return;}
    other.A = A;
    other.B = B;
    }
    public override string ToString()
    {
    return string.Format("A={0} B={1}",A,B);
    }
    }
    
    public class FooTypeConverter : TypeConverter
    {
    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object component, Attribute[] attrs)
    {
    if (component == null) { return null; }
    
    Foo foo = component as Foo;
    PropertyDescriptorCollection propertyDescriptorCollection = base.GetPropertiesSupported(context) ?
    base.GetProperties(context, component, attrs) : TypeDescriptor.GetProperties(component, attrs);
    
    if (foo == null || propertyDescriptorCollection == null)
    return null;
    
    PropertyDescriptorCollection filtered = new PropertyDescriptorCollection(null);
    foreach (PropertyDescriptor property in propertyDescriptorCollection)
    {
    if (property.IsBrowsable) filtered.Add(property);
    }
    
    return filtered;
    }
    
    public override bool GetPropertiesSupported(ITypeDescriptorContext context)
    {
    return true;
    }
    }
    
    public class CopyProblemDemo : Indicator
    {
    public Foo X {set;get;}
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"";
    Name = "CopyProblemDemo";
    Calculate = Calculate.OnBarClose;
    IsOverlay = false;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;
    
    X = new Foo();
    }
    else if (State == State.Configure)
    {
    }
    }
    
    protected override void OnBarUpdate()
    {
    //Add your custom indicator logic here.
    }
    }
    }

    #2
    Hi Mark, thanks for posting. I will be happy to try out a test script if you can provide one and I will poke around to see what will fix this. Please Export your "CopyProblemDemo" script and post it here as a .zip file.



    Kind regards,
    -ChrisL
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris,

      Thanks for taking a look. I've exported the demo indicator per your instructions and attached the zip file.

      Cheers,
      Mark
      Attached Files

      Comment


        #4
        Thank you Mark. We are nearly at the end of office hours today. I will have a look tomorrow morning.

        Best regards,
        -ChrisL
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hi Mark, as source code I am not getting an error. How should I set this up to recreate the error you reported?
          Chris L.NinjaTrader Customer Service

          Comment


            #6
            Hi Chris,
            The problem happens when the indicator is attached and running on a chart, then recompiled and refreshed. I suspect that NT is passing the properties from the running indicator (aka the old version) to the "new" version during the refresh. The .NET error that a type can't be converted to itself is, quite often, an assembly version mismatch issue. I have seen code in the builtin NT indicators that seems to handle this problem with an AssemplyClone() function, but I haven't been able to get that solution working here.

            Even more curious, if I remove the implementation of ICloneable entirely, everything seems to work.

            I would definitely like to solve this without having to remove the ICloneable though. My property object may eventually be used in a NT CollectionEditor, and my understanding is that ICloneable is required in this case.

            To Reproduce:
            1) Compile CopyProblemDemo indicator code
            2) add CopyProblemDemo indicator to a chart
            3) Compile the indicator again
            4) on the chart with the indicator, press f-5 to refresh

            Thanks,
            Mark

            Comment


              #7
              Hi Mark, thanks for sending that. I would recommend first using Visual Studio debugging to see the exact line of code that is throwing this error. See here for details on turning on debug mode and attaching Visual Studio to the NinjaTrader process:


              We also have a collection of type converted examples here:


              Best regards,
              -ChrisL
              Chris L.NinjaTrader Customer Service

              Comment


                #8
                Hi Chris,
                Thanks for the link to the samples. Those look like exactly what I've been looking for, I'm going to study those in detail over the weekend.

                Also, I have been using VS for both writing and debugging my code. I love the level of integration and I'm super happy to finally see proper vs2022 support! However, this error is occurring somewhere in the NT code and is not able to be caught by me (with a standard release build of NT).

                Thanks again for the samples link.

                Have a great weekend!
                Mark

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Harry, 05-02-2018, 01:54 PM
                10 responses
                3,203 views
                0 likes
                Last Post tharton3  
                Started by cre8able, Yesterday, 01:16 PM
                3 responses
                11 views
                0 likes
                Last Post cre8able  
                Started by ChartTourist, Today, 08:22 AM
                0 responses
                6 views
                0 likes
                Last Post ChartTourist  
                Started by LiamTwine, Today, 08:10 AM
                0 responses
                2 views
                0 likes
                Last Post LiamTwine  
                Started by Balage0922, Today, 07:38 AM
                0 responses
                5 views
                0 likes
                Last Post Balage0922  
                Working...
                X