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

c# 6.0 default values

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

    c# 6.0 default values

    Hi,

    On my indicator, I would like to use default values like this...(c sharp 6.0 style)

    [NinjaScriptProperty]
    [Display(Name = "Alerts", Order = 1, GroupName = "Alert Settings")]
    public bool SendPushAlerts {get; set;} = true;

    The reason is because I want to use my indicator in a strategy. However,my indicator uses a few dozen inputs, and I rather just instantiate the indicator in my strategy and have it use its already defined default values...

    The above doesnt work. Is there a way to instantiate an indicator in a strategy where default values defined in the indicator are used, rather than having to explicitly set them in the instantiation?

    #2
    Originally posted by KhaosTrader View Post
    Hi,

    On my indicator, I would like to use default values like this...(c sharp 6.0 style)

    [NinjaScriptProperty]
    [Display(Name = "Alerts", Order = 1, GroupName = "Alert Settings")]
    public bool SendPushAlerts {get; set;} = true;

    The reason is because I want to use my indicator in a strategy. However,my indicator uses a few dozen inputs, and I rather just instantiate the indicator in my strategy and have it use its already defined default values...

    The above doesnt work. Is there a way to instantiate an indicator in a strategy where default values defined in the indicator are used, rather than having to explicitly set them in the instantiation?
    The [NinjaScriptProperty] attribute is what creates the indicator return method with the required parameters.

    MyIndicator( bool SendPushAlerts)

    If you do not want to specify the parameters and just use the default values, remove the NinjaScrptProperty attribute, which would allow you to just call:

    MyIndicator()

    And it would use whatever you had defaulted in SetDefaults.

    Please let me know if I misunderstood something.
    MatthewNinjaTrader Product Management

    Comment


      #3
      Ok, but that would require me to make another version of my indicator? I would think? or would I make a base indicator with no [ninjascript property] and then make one that has that [ninjascript property] that invokes the indicator version without the [ninjascript property], is that right?

      Comment


        #4
        Originally posted by KhaosTrader View Post
        Ok, but that would require me to make another version of my indicator? I would think? or would I make a base indicator with no [ninjascript property] and then make one that has that [ninjascript property] that invokes the indicator version without the [ninjascript property], is that right?
        Sorry I misread that you were trying to use the C# 6.0 Auto-Property Initializers...

        Our builds target .NET 4.5 so standard C# 6.0 features are not yet supported.

        But I do not believe that should prevent you from achieving your goal.

        To review:

        If you have a set of properties:

        Code:
        [NinjaScriptProperty]               
        public int MyInt
        { get; set; }       
        
        [NinjaScriptProperty]
        public double MyDouble
        { get; set; }
        
        // not included in the NinjaScript generated code instance return method        
        public bool MyBool
        { get; set; }
        MyDouble, MyInt are added as NinjaScript properties, but MyBool is not.

        Meaning you can call the indicator like:

        MyIndicator(1, 2.25)

        It will use the MyBool value you set as default in State.SetDefaults

        Code:
        if (State == State.SetDefaults)
        {
            MyBool = true;
            MyInt = -1;
            MyDouble = -1;
        }
        
        else if (State == State.Configure)
        {
            Print(MyBool); // true since that was called in SetDefaults
            Print(MyInt);  // 1 since that is the value called in the method
            Print(MyDouble);  // 2.25 since that is the value called in the method
        }
        In contrast, if you added the NinjaScriptPropertyAttribue to MyBool, you then must call

        MyBool(1, 2.25, true)

        And that will override whatever you set as default...

        If you are looking for a way to instantiate the indicator, by providing an option to configure "MyBool" from the strategy, you would need two versions of the same indicator. I image you could try creating a super-class, but I can't comment on exact implementation details without more knowledge on how you are trying to use it.
        MatthewNinjaTrader Product Management

        Comment


          #5
          Ok,thank you for your idea on the superclass.. but it would be best if ninja supported c# 6, as then i wouldnt need to make a superclass, does NinjaTrader plan to do this ?

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NRITV, Today, 01:15 PM
          2 responses
          9 views
          0 likes
          Last Post NRITV
          by NRITV
           
          Started by frankthearm, Today, 09:08 AM
          7 responses
          31 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Started by maybeimnotrader, Yesterday, 05:46 PM
          5 responses
          26 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by quantismo, Yesterday, 05:13 PM
          2 responses
          20 views
          0 likes
          Last Post quantismo  
          Started by adeelshahzad, Today, 03:54 AM
          5 responses
          33 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Working...
          X