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

Type conversion

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

    Type conversion

    Hi Guys

    I started to derive a Rectangle class into a alertRectangle class in the DrawingTools namespace.

    Later on, in an indicator I try to get all the drawing objects where type match this alertRectangle class:

    However, I am facing an error about type conversion

    Indicator 'ZoneAlert': Error on calling 'OnBarUpdate' method on bar 0:

    [A]NinjaTrader.NinjaScript.DrawingTools.alertRectangl e can't be converted to [B]NinjaTrader.NinjaScript.DrawingTools.alertRectangl e.
    A Type comes from '9d4d7d2f09f04fe49d12ce01eabf724f, Version=8.0.23.0, Culture=neutral, PublicKeyToken=null' in the 'LoadFrom' context at 'E:\Documents\NinjaTrader 8\tmp\9d4d7d2f09f04fe49d12ce01eabf724f.dll'.
    B type comes from '3f856e8638064373b63654f121ff80ee, Version=8.0.23.0, Culture=neutral, PublicKeyToken=null' in the 'LoadFrom' context at 'E:\Documents\NinjaTrader 8\tmp\3f856e8638064373b63654f121ff80ee.dll'.

    I already delete the tmp folder, restarting ninjatrader several times.
    However this error still persist

    Here is the code (abstract)

    Code:
        public class alertRectangle : Rectangle
        {
            protected override void OnStateChange()
            {
                base.OnStateChange();
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Design an alert rectangle.";
                    Name                                        = "alertRectangle";
    
                    ...
                    blablabla
                    ...
                }
                else if (State == State.Configure)
                {
                }
            }
    
    
            //Condition condition
            #region Properties
            [NinjaScriptProperty]
            [Display(Name = "Condition", GroupName = "Alert Condition", Order = 0)]
            public Condition condition
            { get; set; }
    
            [Range(0, int.MaxValue), NinjaScriptProperty]
            [Display(Name = "ReactivationDelay", GroupName = "Alert Condition", Order = 1)]
            public int delayBeforeReactivation
            { get; set; }
    
            [NinjaScriptProperty]
            [Display(Name = "Keep alive", GroupName = "Alert Condition", Order = 2)]
            public bool stayAlive
            { get; set; }
            #endregion
    
            public bool            Notified;
            public DateTime?    NotifiedTime;
    
        }
    
    ​
    In the indicator I just check:

    Code:
    foreach (DrawingTool draw in DrawObjects.ToList())
                {
    
                    if (draw.GetType().Name == "alertRectangle")
                    {
                       // This is failing on thje line:
                        alertRectangle globalRectangle = (DrawingTools.alertRectangle)draw; ​
    I also trid this syntax, with no more success:

    Code:
    alertRectangle globalRectangle = draw as DrawingTools.alertRectangle;
    Can't see how to solve that.
    Can you help ?




    #2
    Hello Philippe56140,

    Thanks for your post.

    An obstacle arises with traditional typecasting in a compiled assembly since the NinjaScript type you attempt to cast will be present in both your DLL and NinjaTrader's Custom.dll assembly. If you plan to compile your code into a DLL, you will need to use the dynamic type to avoid this conflict by dynamically assigning the type at runtime, using the guidelines found on the help guide page linked below under the section 'Casting Types in a DLL (Using dynamic Types)'.

    Casting Types in a DLL (Using dynamic Types): https://ninjatrader.com/support/help...assemblies.htm

    Please let me know if I may further assist. zz0.50yu3ahs269zz
    Brandon H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by judysamnt7, 03-13-2023, 09:11 AM
    4 responses
    59 views
    0 likes
    Last Post DynamicTest  
    Started by ScottWalsh, Today, 06:52 PM
    4 responses
    36 views
    0 likes
    Last Post ScottWalsh  
    Started by olisav57, Today, 07:39 PM
    0 responses
    7 views
    0 likes
    Last Post olisav57  
    Started by trilliantrader, Today, 03:01 PM
    2 responses
    21 views
    0 likes
    Last Post helpwanted  
    Started by cre8able, Today, 07:24 PM
    0 responses
    10 views
    0 likes
    Last Post cre8able  
    Working...
    X