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

NT8 Best Practices Setting Variables Question

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

    NT8 Best Practices Setting Variables Question

    Hello,

    My question refers to the NT8 manual on the best practices section as seen below.

    I understand why variables which reset during the life of the script should be set in State.Configure; however in this example, is it showing that the script is moving the "changing values" completely out of the class level and into the State.Configure section? Or are they still kept in the class level and written again in the State.Configure?


    #2
    Hello Don22Trader1,

    Thank you for your post.

    Unfortunately, your image did not load properly. Are you referring to the following section of the NinjaScript Best Practices page regarding class level variables?

    Click image for larger version

Name:	classLevelVariables.png
Views:	108
Size:	58.4 KB
ID:	1219194




    If you are referring to something else, please try attaching the image again in your reply or paste it into the body of your reply so I may better understand which best practice you are referring to.

    Thank you for your patience. I look forward to your reply.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      I apologize for that,

      Yes, I am referring to the section of the NinjaScript Best Practices page regarding class level variables.

      Hopefully the image appears this time below.

      Click image for larger version  Name:	ninjatrader best practices question.png Views:	0 Size:	85.2 KB ID:	1219199

      Comment


        #4
        Hello Don22Trader1,

        Thank you for confirming; the image did appear this time.

        In the example from the help guide, there are three variables being declared at the class level (multiplier, counter, and myList). The variable multiplier is also initialized at the class level with a value of 5 that will not change. The variables counter and myList are not moved completely out of the class level; they are declared at the class level and the value is set in State.Configure. This value may change (set, reset) throughout the life of the script. Here is another way to look at that example with different comments to make it more clear where in the code everything is happening:

        Code:
        // three variables being declared at the class level
        private const int multiplier = 5;     // this variable uses the const keyword to signal that it will not change.  it is initialized here (declared and set) with a value of 5
        private int counter;     // counter is being declared at the class level
        private List<int> myList;     // myList is being declared at the class level
        
        
        protected override voide OnStateChange()
        {
             // setting the values for counter and myList in State.Configure
             if (State == State.Configure)
             {
                  counter = 0;
                  myList = new List<int>();
             }
        }

        Please let us know if we may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Ninjatrader_Emily,

          Awesome, thank you for your clarification.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by samish18, Today, 11:26 AM
          0 responses
          1 view
          0 likes
          Last Post samish18  
          Started by Trader146, 03-29-2024, 01:22 PM
          2 responses
          14 views
          0 likes
          Last Post Trader146  
          Started by tsantospinto, 04-12-2024, 07:04 PM
          7 responses
          126 views
          0 likes
          Last Post aligator  
          Started by futtrader, 04-21-2024, 01:50 AM
          5 responses
          56 views
          0 likes
          Last Post NinjaTrader_Eduardo  
          Started by PeakTry, Today, 10:49 AM
          0 responses
          2 views
          0 likes
          Last Post PeakTry
          by PeakTry
           
          Working...
          X