No announcement yet.

Partner 728x90


best practices clarification

  • Filter
  • Time
  • Show
Clear All
new posts

    best practices clarification

    I know this should be an easy one for Ninja support or the Ninjascript c# gurus.

    In the Ninjatrader 8 Help Guide Ninjascript Best Practices you have the following section that I would like some clarification on.

    ================================================== ================================================
    Setting class level variables

    Do not set variables at the class level unless they are constant.
    You should delay setting or resetting variables until the State has reached State.Configure.
    You can use const keyword to differentiate values which do not change from variables which do change.
    Why: Waiting to set up and define resources until the object has been configured
    ensures that values not set up and declared prematurely.
    ================================================== ================================================

    In the above section when you say delay setting or resetting class variables until State.Configure
    do you mean we shouldn't set or reset variables :

    1) defined within custom classes like Var1 in class A as shown below

    2) defined in the indicator class like Var2 shown below

    3) when creating the reference thingy in class B and
    instantiating the object A as shown below

    4) defined in the indicator class like when creating
    the reference object1 to class B and instantiating
    the object B as shown below

    Or if it's something else please explain.

    namespace NinjaTrader.NinjaScript.Indicators
    public class example : Indicator
    public class A
    int Var1 = 0;

    public class B
    A thingy = new A();

    B object1 = new object1();
    int Var2 = 0;

    The only reason this advice exists is imo because property values will only receive the values from UI after at least State.Configure. Before that point they'll have the value in State.SetDefaults.
    Also everything else that accesses bars data should be initialized in State.DataLoaded

    Other variables that don't use UI properties or access bars data directly can be initialized at any point without having to worry.

    Code where waiting matters:

        public class Example : Indicator {
            private double deviationPts;
            private SMA sma;
            protected override void OnStateChange() {
                if (State == State.SetDefaults) {
                    DeviationTicks                                = 10;
                } else if (State == State.Configure) {
                    deviationPts                                = DeviationTicks * TickSize;
                } else if(State == State.DataLoaded) {
                    sma                                = SMA(20);
            #region Properties
            [Range(1, int.MaxValue)]
            [Display(Name="Deviation Ticks", Order=1, GroupName="Parameters")]
            public int DeviationTicks
            { get; set; }
    Last edited by MojoJojo; 05-30-2020, 05:30 AM.



      Thanks for feedback MojoJojo --

      I've always assumed that variables that don't use UI properties or access bars data directly could be set or reset at any point without having to worry, until I read this section in the best practices.

      The best practices section is "State Management" and the title used for their statement is "Setting class level variables" and then the first line of the statement says "Do not set variables at the class level unless they are constant." And then they give a best practices example code box where they show that simply setting the value of a variable at the (example : Indicator) class level is said to be a poor practice.

      This is directly contrary to what you've said about setting variables and not having to worry about it, except of course when UI or bars dependent. And this is also contrary to what I've thought until now even after I've written dozens of custom indicators and have been setting and resetting class level variables the way I've shown in the code fragment I've provided without any issues or crashes etc.

      So, I'm confused and would like for whoever wrote this section of the "best practices" or someone who really knows why they wrote it the way they did to explain further what might be going on in the background of Ninjatrader that require us to set or reset variables in the manner they are describing in this section.

      To me this seems so strange that I'm asking for clarification on what they meant by saying "Class level variables" and as you can see I give 4 examples of different levels of variables within classes - embedded classes and not.


        Hi dtl-saw, thanks for your question.

        We say you should not initialize anything outside of State.Configure because when State==State.Configure is always going to be the time when its appropriate to set up custom variables/classes. When you tie into NinjaTrader with a script we don't use a constructor or have any control over the object initialization, so it's better to set things up when the time is right. With all this said, if you can set up your variable at the class level and it's set to the right value every time the script is used, this is fine. If you run into something that doesn't work (whatever it may be), then we would tell you to initialize it in State.Configure and see if it works. If it does not work in State.Configure, there would be an issue on our end, hence the best practice suggestion.

        Kind regards,

        Chris L.NinjaTrader Customer Service


        Latest Posts


        Topics Statistics Last Post
        Started by Kirk Thorns, Today, 05:34 PM
        0 responses
        Last Post Kirk Thorns  
        Started by JobDjassem, Today, 05:24 PM
        0 responses
        Last Post JobDjassem  
        Started by Kirk Thorns, Today, 05:06 PM
        0 responses
        Last Post Kirk Thorns  
        Started by CyberNate, Today, 03:11 PM
        5 responses
        Last Post CyberNate  
        Started by NinjaTrader_ChrisL, Today, 03:44 PM
        0 responses
        Last Post NinjaTrader_ChrisL