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

Dynamically set NinjaScriptProperty (ies) by calling a method?

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

    Dynamically set NinjaScriptProperty (ies) by calling a method?

    To ensure consistency across properties for various indicators (e.g. names, default values, etc., and to minimise code) I'd like to have a method that sets NinjaScriptProperty attributes based upon the inputs supplied to the method, e.g. if I pass 1 to the method, we create NinjaScriptProperty #1, if I pass 2 to the method, we create NinjaScript Property #2, etc. This allows me to have a method that I can apply to all of my indicators for their setup and call this as necessary. Could you provide an example of this approach please?

    #2
    Hello ellington,

    For what you are describing you would very likely need to create a method that accepts an indicator as its parameter and then use reflection to detect what type the indicator is and then cast to the correct type so you can access its properties. This is not necessarily a NinjaScript concept, this would be general C# and working with types/reflection. I would suggest using online C# resources related to using Types or passing objects to further expand the sample below.

    Code:
    public void MyMethod(Indicator indicator)
    {
        if(indicator is SMA)
        {
             SMA myIndciator = indicator as SMA; 
             myIndicator.Period= 10; 
        }
    }


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

    Comment


      #3
      Thanks for the quick reply Jesse. I get the principle of what you're saying but I may not have been clear enough in the requirement. This isn't to set parameters for existing indicators. If we have multiple indicators in a single cs file, I'm looking to have a method that I can call from each indicator that checks what the indicator is, then sets the input parameters, plots, etc., based upon what the indicator is (or a value that's been passed in). I'm trying to avoid having a source file that has duplicate source code for each indicator when each indicator has a number of settings, parameters, plots, etc., that are the same as other indicators. For example, if in a given indicator we were saying:

      [NinjaScriptProperty]
      public int Lookback
      { get; set; }

      and I have 5 indicators in the same source file that use this Lookback parameter (with the same Display attributes, etc, etc) then I'd much rather have a method I can call from each indicator to 'create' the relevant NinjaScriptProperty attribute (and display attributes) in each indicator than have the same code appearing 5 times.
      Last edited by ellington; 08-15-2019, 09:21 AM. Reason: clarification of example.

      Comment


        #4
        Hello ellington,

        Based on the new details it sounds like you are trying to generate actual source code and not just set values for code you already created, is that correct?

        If so, there would be nothing I could suggest here. You will need to type out the code you want to use in the file and set defaults for the properties using SetDefaults. If you have 5 indicators that are all similar and all need a Lookback property, each file will have a public property named Lookback that is used in its logic in some way. There is nothing I can suggest to avoid duplicating this type of code.

        C# does allow for inheritance patterns for this type of use case however using complex inheritance or custom base classes in NinjaScript is not something we can support. You are free to look at C# solutions to this problem and test them in NinjaScript, however I would only suggest having the full code needed in the file. The previous suggestion can still be used to set up defaults if you didn't want to include them typed out in each file, however there are certain properties/methods in OnStateChange which should not rely on variable logic. With that being said it is still the best solution to have all code required present and all defaults present in the file its self.


        Please let me know if I may be of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          All clear, thanks.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by ScottW, Today, 06:09 PM
          0 responses
          2 views
          0 likes
          Last Post ScottW
          by ScottW
           
          Started by Board game geek, 10-29-2023, 12:00 PM
          14 responses
          244 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by Waxavi, 04-19-2024, 02:10 AM
          4 responses
          56 views
          0 likes
          Last Post sonia0101  
          Started by cmtjoancolmenero, Today, 03:58 PM
          0 responses
          9 views
          0 likes
          Last Post cmtjoancolmenero  
          Started by Segwin, 05-07-2018, 02:15 PM
          11 responses
          1,778 views
          0 likes
          Last Post aligator  
          Working...
          X